The Code Train

Where Neil Crosby talks about coding on the train…

RSS Entries

Displaying your current remote iTunes Playlist on your local Mac’s Dashboard

Posted on November 14th, 2009 by Neil Crosby. Filed under Uncategorized.

In my last blog entry, I wrote about “Controlling iTunes across multiple computers with the keyboard” (snappy title, huh?). In it, I promised that the next step was to work out how to get the current playlist displaying on the dashboard. So, here goes…

Remote viewing the currently playing iTunes playlist

The first thing to be aware of is that if Safari can display something on a webpage, then Dashboard can display it as well. If you visit a page in Safari and right-click somewhere on the background of the page then you should see a menu item titled Open in Dashboard.... If you select this then you’ll be able to drag to select an area of the page to turn into a dashboard widget. Click the Add button at the top of the window once you’re done, and you’ll magically have created a widget. Now, any time you open up your Dashboard that widget will be reloaded and you’ll see fresh data.

So, that’s how we’ll display the playlist as a Dashboard Widget – we’ll create a webpage that contains the data and then use that as the widget.

If you read the last entry, you’ll know that I like to use AppleScript as a quick glue language on my Mac. It’s pretty easy to use (once you know what you’re doing), and you can hook into many of the apps you use on a day to day basis and do interesting things with them. So, it should be no surprise to hear that I’ll be using AppleScript to get the playlist data out of iTunes. Given that I’ll be displaying this data on a webpage though I’ll be using PHP (because it’s what I know) to write a quick and dirty page that displays the data that the AppleScript provides.

The AppleScript is a little more involved than the one liners we used last time. We need to grab the current playlist and then grab information about its UID, Title, Artist and Album, and before finally finding out what the currently playing track is. Here’s how we do that.

set tracks_list to {}

tell application "iTunes"
    tell current playlist
        tell tracks
            set tracks_list to get {persistent ID, name, artist, album}
        end tell
    end tell

    set tracks_list to tracks_list  {persistent ID of current track}
end tell

get tracks_list

In this script we first we create an empty list to put all our data in, then we add lists of track IDs, names, artists and albums to it, then we add a single item of the currently playing track’s ID. It’s not the most elegant of data structures to use once we get to play with it in PHP, but it’ll do. Running this from the commandline using osascript -s s playlist.scpt (the -s s means “print the output in a recompilable form”) gives us something like the following (respaced for clarity):

{
    {
        "0D7D716D221B623B", 
        "688E193B9F7F7EE3", 
        "05BFA94137C9691F"
    }, 
    {
        "Fashion Is Danger", 
        "Fabulous", 
        "Never Gonna Give You Up"
    }, 
    {
        "Flight of the Conchords", 
        "High School Musical Cast", 
        "Rick Astley"
    }, 
    {
        "I Told You I Was Freaky", 
        "High School Musical 2", 
        "Fantastic 80s"
    }, 
    "688E193B9F7F7EE3"
}

Now that we have a data structure, we can actually do something with it in PHP. Unfortunately, as far as I’m aware there’s nothing built into PHP that’ll read this structure. Fortunately though, this structure is pretty damn close to JSON. If we were to replace the twiddly brackets with square ones then PHP’s json_decode would return a data structure and we could do something with the data. Hooray. It’s a hack, but it works, and it’s what I did. A simple str_replace later and I had data. You can see the code, such as it is, in my github repository.

The eagle eyed amongst you will notice that I’m not directly calling osascript from my PHP script though, and am instead going through an intermediary shell script that nukes DYLD_LIBRARY_PATH before running the AppleScript. The reason for this is that on the machine I’m running the script on I’m using MAMP for my web stack, and that does funny things with DYLD_LIBRARY_PATH under Snow Leopard apparently. So, I have to nuke it.

And that’s all there is to it. Visit the webpage you’ve created to display the data, let it show you what’s playing and then turn it into a dashboard component using Safari. I get to control iTunes on a remote machine and I don’t have to keep a VNC connection on it. This makes me happy.

If you enjoyed this post, subscribe to The Code Train and read more when I write more.

2 Responses to “Displaying your current remote iTunes Playlist on your local Mac’s Dashboard”

  1. Excellent tutorial. I want to make similar application to show playlist. though i will have to go through this tutorial step by step to understand the code.

    One question I have is what if we let users select the song from the iTunes playlist on a web page, and it queues up in the iTunes and starts when the current song finishes playing. (what script do we need to write for that.)

  2. They usually hang some accessories which they think will turn their home into something elegant and beautiful.. Instead you should use lights that are designed for bathroom use only and they should be inset into the ceiling or the walls.. A wrought iron candle chandelier is a great addition to your living room; a hand forged wrought iron chandelier looks lovely in the kitchenette area..

Comments RSS

Leave a Reply

TheCodeTrain Theme by Neil Crosby, Powered by WordPress