husk.org / blog. chaff. occasional witterings.

2007-07-23

Remote Playlists with iTunes

computing 23:08:41

A few weeks into my time with a MacBook Pro, I finally opened up Front Row, Apple's "ten foot" interface for media viewing. Being mildly obsessed with iTunes library sharing, I thought I'd see if it was possible to connect these days (when I tried with Front Row 1.0, way back when, it wasn't). I was surprised to see, upon swapping back to the normal UI, that not only had it worked, but that there was a playlist connected to my remote library.

iTunes, you see, only supports the creation of playlists for your primary (local) library. (There's a bit of half-hearted support for playlists of iTunes Store previews, but let's ignore that.) Everyone says creating playlists from shared libraries is impossible. So how was Front Row managing to create this playlist? I suspected AppleEvents (the infrastructure underlying AppleScript), and a bit of command-line fu from Tom Insam proved that not only was it right, but that it was possible to replicate the effect.

It turns out that the process is pretty straightforward. Front Row selects the tracks that you're viewing, sets up an XML file in a temporary directory referencing the tracks, and then tells iTunes to add that file as a playlist to the shared library.

Unfortunately, there were a couple of setbacks that stopped me releasing a version sooner. Firstly, the XML file requires persistent IDs, but these are returned by iTunes as a double, but AppleScript itself couldn't handle the resulting data; it was too long. However, it's not a problem with the underlying infrastructure, so I moved to Ruby and rb-appscript. Once again, though, I was foiled; I had no joy getting the portion of the script that adds the playlist to work, even with some convoluted workarounds.

After all that, I ended up leaving the script for a couple of months, always intending to return to it. When I did, I found that the persistent ID property has changed; since iTunes 7.2 it returns a string, not a double. This let me return to AppleScript, where I had working code for adding the playlist, and the result is a working application (well, script) to Add Remote Playlist.

It's easy to use the application. You can either run it from the desktop or put it in your iTunes Scripts folder (in the iTunes folder in your Library; you can open it with the Script Menu, if iTunes is frontmost). Once run, if you have a selection of shared tracks, a remote playlist will be created (called, imaginatively, Remote Playlist). You can rename the playlist, and create as many as you like by re-running the script.

There are a number of limitations. You can't reorder the tracks, and they're added in the order they're shown, not the order you select them. The remote playlist only persists as long as the library is connected, too. However, hopefully this is still useful - and it's certainly better than the nothing there was before. Although I'm distributing this as an application (oddly, it's saved as a PowerPC native binary), you can look at the source by dragging it to Script Editor. Enjoy.

navigation.