# GeekTool with iTunes and Pandora

I’ve gone back to listening to Pandora while I’m at work. No matter how big your iTunes library is, it’s nice to get away from it and listen to unfamiliar music for a while. I’m enjoying Pandora’s Ska/Rocksteady/Early Reggae genre station as I write this.

I’ve been connecting and listening through PandoraBoy rather than my browser so I don’t have to worry about accidentally shutting off the music as I move and close browser windows throughout the day. Although PandoraBoy’s project home page mentions intermittent crashes, I haven’t had any so far. In fact, interacting with Pandora seems better in PandoraBoy than in Safari, where I’ve occasionally been unable to create a new “station” because I couldn’t type anything into the song/artist field.

One thing I miss when listening to Pandora instead of iTunes is being able to glance at my Desktop and see the name and artist of the song that’s playing. Over a year ago, I configured GeekTool to print that information out in the lower right corner of my screen when iTunes is playing (I wrote about it here and here, but today’s post will supersede that work). I’d like to have that same information from Pandora if that’s what I’m listening to.

Here’s what I want: if iTunes is playing, print the song information in the lower right corner of the Desktop; if Pandora is playing, do the same.

First, I need to rejigger my old iTunes scripts. I used to have “iTunes off” printed if iTunes wasn’t running and “Not playing” printed if iTunes was running but paused. I don’t want either of these printed when Pandora is playing because they’ll collide with the Pandora song info. So here’s my new version of “itunes-playing.scpt,” which is saved in the bin folder of my home folder.

on run
set info to ""
tell application "System Events"
set num to count (every process whose name is "iTunes")
end tell
if num > 0 then
tell application "iTunes"
if player state is playing then
set who to artist of current track
set what to name of current track
set onwhat to album of current track
set stars to (rating of current track) / 20 as integer
set info to quote & what & quote & " by " & who & " (" & stars & ")" & return & "from " & onwhat as string
end if
end tell
end if
return info
end run


The default value of the variable info is the empty string. Only if iTunes is running and playing does info contain any text. The stars variable in line 12 holds the song’s rating; I display this because I have some Quicksilver triggers that allow me to increase or decrease the rating of the current track, and I need to know what the rating is before deciding whether to change it.

I set GeekTool to run this script, via osascript ~/bin/itunes-playing.scpt every 10 seconds and display the output in the lower right corner of my screen.

My “pandora-playing.scpt” was easy to write because PandoraBoy has an AppleScript library with several of the same commands and properties as the iTunes AppleScript library. It’s also saved in the bin folder of my home folder.

on run
set info to ""
tell application "System Events"
set num to count (every process whose name is "PandoraBoy")
end tell
if num > 0 then
tell application "PandoraBoy"
if player state is playing then
set who to artist of current track
set what to name of current track
set onwhat to album of current track
set info to quote & what & quote & " by " & who & return & "from " & onwhat
end if
end tell
end if
return info
end run


Apart from not having a stars rating and referring to PandoraBoy instead of iTunes, this script is identical to “itunes-playing.scpt.” Its GeekTool setup is also identical:

This goes a long way toward providing an equivalence between Pandora and iTunes. One thing missing is a Play/Pause hotkey for PandoraBoy. The AppleScript for such a hotkey is trivial—it’s basically one line—but I’d like to use the same hotkey for both iTunes and PandoraBoy, and that may be a bit tricky. I’ll look into it this weekend and see what I come up with.

Update
Making a multi-use hotkey was easier than I thought.

Further update
I should mention that although GeekTool’s page doesn’t say that it works under Leopard, I’ve found that it does. I have the setup described on this page running on both a G4 iBook with OSX 10.5.1 and an Intel iMac with OSX 10.4.11.