-- This AppleScript is intended to run in the background. It polls iTunes, -- and whenever the "current song" ID changes, it invokes a URL with some -- parameters taken from the song's info in iTunes. -- I first found this code through Matt Haughey's site, at this location: -- http://a.wholelottanothing.org/archives.blah/006627 -- It says it was written by Jerry Kindall but I couldn't find it -- at Jerry's site http://www.jerrykindall.com/ . -- I then modified it to extract the iTunes data I care about and to invoke -- the URL the way my server code expects to pull it it. -- To get this work you minimally need to set the desired URL of your server, -- in the "pingURL" property just below. -- interval at which to poll iTunes, in seconds property pollInterval : 10 -- base URL of CGI or Servlet interface on server -- we will append parameters to this URL before invoking it, so it ends in a question mark property pingURL : "http://your.host.com/your_servlet_or_cgi_path?" -- set to true to get an alert box showing the URL before invoking it property debug : false global lastID on run set lastID to -1 end run on urlEncode(theString) set hexDigits to "0123456789ABCDEF" set encodeChars to "%&?=+' " -- must escape apostrophe for shell though it is not strictly necessary for URLs repeat with i in every character of encodeChars set k to ASCII number of i set j to character ((round (k / 16) rounding down) + 1) of hexDigits set j to j & character (k mod 16 + 1) of hexDigits set AppleScript's text item delimiters to i set theString to every text item of theString set AppleScript's text item delimiters to "%" & j set theString to theString as text end repeat set AppleScript's text item delimiters to {""} return theString end urlEncode on idle try tell application "Finder" to get every process whose name is "iTunes" if result is not {} then -- only update when iTunes running tell application "iTunes" if player state is playing then -- only update when playing tell current track if database ID is not lastID then -- only update on track change -- get the track info if artist is not missing value then set artist_name to artist else set artist_name to "" end if if name is not missing value then set song_name to name else set song_name to "" end if if album is not missing value then set album_name to album else set album_name to "" end if if comment is not missing value then set asin to comment else set asin to "" end if -- ping the server with the song information set trackBack to pingURL & "artist=" & my urlEncode(artist_name) & "&song=" & my urlEncode(song_name) & "&album=" & my urlEncode(album_name) & "&asin=" & my urlEncode(asin) if debug then tell me activate beep display dialog trackBack end tell tell me to do shell script "curl --silent '" & trackBack & "'" -- remember this track's ID so we can tell a new track starts set lastID to database ID end if end tell end if end tell end if on error m number n if n is -128 then error number n else if debug then tell me activate beep display dialog m & " (#" & n & ")" end tell end if end try return pollInterval end idle