The internals

Carrying forward from yesterday, let’s take a behind-the-scenes peek at the “Now Playing in iTunes” album art feature you see in the sidebar.

Here’s what you’ll need: 500 gms Mac OS X (sorry Windows people), 50 gms AppleScript, 2 tbspn Shell Script, 1 tbspn FTP, 1 Amazon developer ID, 250 gms PHP and 1/2 tbsp Cron skills.

Here’s how it functions. We schedule a AppleScript to run every 5 minutes. This AppleScript passes the name, artist and album of the song currently playing in iTunes to a shell script. This shell script FTPs this information to your webserver. The PHP script reads this file, queries the Amazon database using Amazon Web Services and gets the corresponding album art (if any). Simple, isn’t it?

Le’ts dig into the code, shall we?

The AppleScript

tell application "iTunes"
	if player state is paused or player state is playing then
		copy current track to currTrack
		copy artist of currTrack to currArtist
		set currArtist to """ & currArtist & """
		copy name of currTrack to currName
		set currName to """ & currName & """
		copy album of currTrack to currAlbum
		set currAlbum to """ & currAlbum & """
		set myCommand to "~/uploads " & currArtist & " " & currName & " " & currAlbum
		do shell script myCommand password "ADMINPASSWORD" with administrator privileges
	end if
end tell

It’s rather self explanatory isn’t it? The “”” bit is to enclose the string within quotes – just to be sure. We then call a shell script called “uploads” with 3 parameters. I store both the AppleScript and the shell script in my home (~) folder.

The shell script

#!/bin/sh
echo $1 > ~/np.txt
echo $2 >> ~/np.txt
echo $3 >> ~/np.txt
ftp -n yourwebsite.com < 

Again, rather simple. Store the information in a text file and upload it to the remote directory.

Now to querying Amazon and parsing XML. And adding the crontab entry. Nah, all that later, a big Champions League night beckons!

PS - This is perhaps the simplest of approaches with a LOT of disadvantages - this is nowhere near the best way of doing this. There's something in each of the steps that can be improved - I'll cover all that and more next time.

One thought on “The internals

  1. Nice work! Like i told you a couple weeks ago, I think i’ll put up something similar, except that it’ll be for winamp (on windoze) and instead of querying amazon for album art, it’ll create dynamic images (php). Maybe somehting like pick different background images based on genre or something? What do u think? All i need now is some decent hosting and some decent background images (and some photoshop skills ;)). If I get stuck with getting it to work, I’ll know who to ask 🙂

    True to my word, i’ll stay on ur case.. now playing down, comment editing to go 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.