Fixing a Broken iTunes Library with Applescript
18 April 2007

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.


