Fixing a Broken iTunes Library with Applescript

18 April 2007

Borked iTunes Genres

Last night I opened iTunes to find that all my genres had disappeared.

One Minute Shaking

After the initial shock wore off it occurred to me that since the genre info was stored in the id3 tags of each file it was unlikely that they were completely gone. Getting info on a couple of tracks confirmed that the data was all still there, and interestingly, once the info window had been opened the genre info appeared for that track. It seems that, somehow, iTunes had simply forgotten the genre of every song in my library.

Bring Em Back Alive

Surely there was an easy way to ask iTunes to simply refresh my library by reading the id3 tags of each file? Apparantly not. A bit of googling suggested that you could do this by selecting all the tracks (Command - A), getting info (Command - I), then pressing OK. You need to make sure that none of the fields were checked before you hit ok, or iTunes will go through and actually edit the tags, making things even worse. This seemed promising, but unfortunately it didn’t work. After a few minutes of processing, nothing had changed.

Since the data was all there in the music files I knew that I could just ditch my library and start again by re-importing everything, but that would be a last resort. I would loose all my play-counts, playlists, ratings, etc, and probably have to re-sync the whole lot to my ipod.

One thing I noticed while experimenting was that the info window didn’t even need to be closed for iTunes to update - I could open it, hit ‘next track’, and iTunes would pick up the info from each track as I paged through the songs. That gave me an idea.

10AM Automatic

It’s a very simple, and slightly silly applescript, but it works. To use it, you need to enable GUI scripting in System Preferences under Universal Access (check ‘Enable access for assistive devices’). Then open Script Editor and enter the following applescript (or just download it).

tell application "System Events"
    tell process "iTunes"
        set frontmost to true
            keystroke "i" using {command down}
            repeat 10000 times
                keystroke "n" using {command down}
            end repeat
    end tell
end tell

You could change the repeat line to ‘repeat while true’, but I wanted to be sure that it would actually stop.

Open iTunes, select the first track, switch back to Script Editor, and hit run. Applescript will switch to iTunes, get info on the selected track, then hit the next button 10,000 times. On my machine applescript seems to be too quick for iTunes so it doesn’t actually go through 10,000 tracks. When it stops, just scroll down and select the first song with a missing genre and start the script again.

Future Proof

Obviously this would have been much easier if I’d had a recent backup, and I’ll definitely be backing up a bit more frequently now. It would be nice to see a re-build option in iTunes though. The data is all there in the files, iTunes just needs to be told to look for it.

Disclaimer: If you try this, back up first. It shouldn’t cause any problems, but I won’t be held responsible if something goes wrong… or if you end up with 10,000 empty playlists.

There are no comments so far. Comments are open and you can add one here.

Posted under Applescript, Mac, Music.

Leave a comment:




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Recently

Upgrading to WordPress 2.6
17 July 2008

Blogtender was hacked yesterday, resulting in a handfull of posts getting a list of invisible links added to the bottom, I assume in the hope that Google will pick them up. After changing my passwords (probably unnecessarily) I decided to upgrade to WordPress 2.6. The upgrade process is easy …

Incredibly Strange Film Festival iCalendars
12 July 2008

“I am rarely happier than when spending an entire day programming my computer to perform automatically a task that it would otherwise take me a good ten seconds to do by hand.”Douglas Adams, Last Chance to See

The Incredibly Strange Film Festival is back …

Last.fm Charts
6 July 2008

This is a fun little script I wrote up that reads your weekly top albums chart from Last.fm, gets the cover art for each album, then displays the top albums on your blog. Scroll to the bottom of the page to see how it looks. …

Sharing a Canon laser printer between OSX and XP
5 January 2008

In case anyone else runs into this problem in the future, here are the steps needed to share a Canon Imageclass 4140 laser printer between OSX and XP. I’ve only tested this under Leopard, but it should work for Tiger as well.

First, the problem. While …

Archives: 2008 2007 2005

Music

Photos