Playlist Fixer

Now that I have plenty of hard disk space – something I haven’t had for years, but which is a very welcome change (albeit likely to be quite temporary, given how fast it’s being used up) – I decided to move my music back off my iPod. I had to delete the copy on my Powerbook years ago, which isn’t really a big deal, except as time goes on the odds of me getting a new iPod (or iPhone) are increasing, and I need the music back on a computer in order to easily work with new gizmos.

So anyway, I spent two days and a whole lot of irritation trying to do this. It’s obviously not something iTunes allows you to do, because then you could steal music. Pfft.

Aside: piracy is not theft, it’s copyright infringement… now, legally, that doesn’t help, because the penalties for copyright infringement these days can be worse than for murder and rape.

There’s heaps of 3rd party apps for doing this sort of thing – iPod.iTunes, iPod Access, etc. However, only three that I could find will maintain your play counts. I’m rather attached to my play counts, and I wasn’t going to move everything without keeping them. [technote: they’re stored in the iPod/iTunes databases, not the files themselves, so simply readding the song files is insufficient]

Of the three I found, one was free but didn’t work, and the other two were shareware. iPod.iTunes works well (albeit slowly), but is ridiculously expensive – $40 or something for what is really just a big AppleScript. No thanks. $10, for sure. Even $20 I might consider… but $40 is just greedy.

Anyway, luckily these apps are shareware, so you get some functionality even without paying. It took a very long time, given their limitations for unpaid copies, but I did eventually get all my music shifted over, with play counts!

But then, I happened to be looking through my music, double-checking, and found that I’d played some of my songs over a hundred times. That seemed very odd, because I was pretty sure I would have noticed the approach to 100 plays… last I remember I wasn’t that far beyond 50 or 60.

And I noticed that a lot of the play counts were even numbers… far more than seemed likely. So I wrote a simple AppleScript to count the number of songs with even playcounts and the number with odd… the result was that there were more than twice as many songs with even playcounts as odd… that’s surely not right.

Since I’d been playing with multiple apps, I assumed then that they’d stomped over each other and accidentally doubled the playcounts. But I wasn’t sure, and if that were true, anyway, why were a third of my songs still with odd playcounts? Had they not been doubled too? There were a couple with playcounts close to 100 that were odd… I was sure they had to have been doubled, but, then they should be even…

Luckily at this point I realised I had backups – I do an incremental backup of my entire home folder every day now, given the problems with FatMac, and when I looked I was very lucky to find that I had coincidentally captured a snapshot of the library file after adding my music back in, but before the playcounts got screwed.

So, now I was armed with a library file containing all my updated songs and whatnot, but bad playcounts, and one with correct playcounts (but mangled names and whatnot – the import process had been pretty dodgy in spots).

It seems like the most obvious answer would be to write a script to merge the two together, or something. Unfortunately, the only reliable way to modify iTunes’ library data is to AppleScript it. So that meant I’d be looking at AppleScript, which I have some on and off experience with, but have never managed to grasp solidly. Plus, it’s performance tends to suck, and the idea of comparing two lists of thousands of items with it, well, that sounded like too much for it.

Thus, this sounded like a job for Cocoa. So I fired up Xcode, laid out my main window in Interface Builder, and then got bored and left it…

I do that a lot.

But today my plans fell through, so I’ve got nothing better to do than sit around watching movies or going through my photos – which, while a necessary task, is really boring. So I picked the app up again and finished it – i.e. actually wrote code.

And the end result looks like this:

Not the prettiest girl at the dance, but perfectly functional. It reads the XML files you give it, compares them, works out which songs match but have different playcounts, and then gives you a list, as shown. Select the ones you want to fix, click Apply, and wait a while. Because it uses AppleScript still, and I couldn’t be bothered spending six years learning how Cocoa’s AppleEvents stuff works, I just make up a AppleScript source string and have NSAppleScript compile that. Slow, but easy.

And it seems to have worked – I’ve fixed up all my playcounts, and now things are back much closer to what I remember. I’ll have to run a few more tests to try to reassure that it all went correctly, but thus far it looks promising.

The end result of all this is that I can now listen to music again – while the playcounts were stuffed I didn’t want to listen to things, because I knew sooner or later I’d fix up the playcounts, so any additional plays in the interim would be lost.

Yes, I know, that’s pretty anal, but it’s taken me two years to get this play count information, and I just can’t bring myself to throw it away or taint it.

So now, my play time is a much more likely 14 weeks, 2 days, 8 hours, 31 minutes and 56 seconds. I’ve played 37,093 tracks. Woohoo!

Leave a Comment