American in Spain

Applescript For Blogging Current iTunes Song

November 8, 2006

I finally have my current song blogging properly! It wasn't a short process...

Background

The absolute simplest way to blog your current song is to use Last.FM. They give you an application that you run on your computer (it stays on the dock), and it will "scrobble" (upload to their website) your currently playing song. They use this information to offer you other music and streaming radio to your tastes. There's a whole community aspect of their website that fits in with the new "social networking" internet fad. You can meet people that like the same music you do, for example. All in all, Last.FM is a pretty cool service, and I think they will be a huge success (the YouTube of 2007?). But I don't want another app open all the time.

Getting your Last.FM playlist into your blog is trivial via RSS and XML feeds. There are a handful of Wordpress plugins to faciliate that. But the feeds that do the recent songs don't provide album art (only the Top Albums feed provides album art, via Amazon). And the links on the songs link back to Last.FM. Selling music is, after all, their main source of revenue. Wouldn't it be cool if there was a way to link to a vendor that had an affiliate program, so I could get some kickbacks?

Then I found CG-PowerPack, by Chaitgear. CG-PowerPack is a collection of PHP scripts that allow you to do lots of useful things, mainly of interest to me: find album art on Amazon via artist, song, and album values, link to the product on Amazon with your Amazon Associates ID, and, crucially, record current track data from HTTP GET requests. This was exactly what I was looking for!

Now, all I had to do was figure out how to automatically send HTTP requests to my blog server with my current track information. Windows users have iTunesBlogger available to them to do this, but I could not find any equivalent for the Mac. [Note: I have since found Recently Tunes, which I think will do what I was looking for, including uploading html files via ssh, but it still seems a little flaky.]

The Script

Blog Current Song.scpt

The script looks at your current song in iTunes and, if it hasn't changed since the last time it notified the server, it sends an HTTP request to the server with the song information properly encoded. It keeps a file in your home directory called .currentTrack that contains the name of the song last sent to the server.

(Disclaimer: This is the first applescript I have ever written, so the code might suck.)

Script Screenshot

How To Run It

I have it set up as a cron task to run every minute. You will need the following entry in your crontab (all on one line!). To modify your crontab, run "crontab -e" in Terminal. This entry assumes that the script is in your home directory.

0-59 * * * * osascript ~/Blog\ Current\ Song.scpt > /dev/null &

And voila! My current song is sent to my blog!

Final Note

The formatting performed by CG-Amazon, one of the CG-PowerPack scripts, is disgusting, so I had to hack cg-amazon.php all to hell to make it fit my formatting standards. You're on your own with that.

Now go buy whatever I'm listening to!