Showing Your Current Playing iTunes Track On The Web
From SifWiki
OUTDATED This content is here for historical reasons only now, to see an updated and better version of the below please visit Alexander M Kasprzyk's site at http://kasprzyk.homeip.net/~alex/scripts/iTunesTrack.php OUTDATED
This little bodge I knocked up in about 10mins during my lunchbreak - it uses a combination of AppleScript (which I've barely used - I nicked most of the code from an existing script) a standard bit of bash scripting and PHP to upload my currently playing tune in iTunes onto the web.
This is the first bit of code you'll need, it's just a little AppleScript and should be copy and pasted into ScriptEditor and then saved as a .scpt file somewhere useful (I have a ~/bin/ directory for any scripts or applets I create).
tell application "System Events" to set doit to (exists process "iTunes") tell application "iTunes" to set doit to doit and (player state is playing) if doit then tell application "iTunes" set trk_arts to the artist of the current track set trk_name to the name of the current track set trk_albm to the album of the current track set trk_rating to the rating of the current track end tell set the_command to "~/bin/upload_current_itunes_track \"" & trk_arts \ & " - " & trk_name & "|" & trk_rating & "\"" do shell script the_command end if
You may need to make sure the "set the_command to"-etc line is all on one line in the script editor! And also make sure that "~/bin/upload_current_itunes_track" points to the path of the bash script you're about to create which contains:
#!/bin/bash if [ "$1" != "" ] then ssh siftah@web1 "echo \"$1|Home\" > /home/siftah/public_html/current_itunes_track.txt" fi
Obviously you'll need to replace "siftah@web1" with the correct username and address for your webspace - public key authentication is required, and of course the script will need chmod +x'ing... The "Home" bit of text is just to let me track from where this song was listened too... All that's required now on the host machine is to setup the cronjob to run this little script:
*/1 * * * * /usr/bin/osascript /Users/siftah/bin/get_current_itunes_track.scpt &>/dev/null
That's basically it! Use a little bit of PHP on the website in order to put the data into the format you want, at the moment I'm using:
$filename = "current_itunes_track.txt";
$info = explode("|",file_get_contents($filename));
$current_track = $info[0];
$track_rating = (($info[1])/20);
$played_from = $info[2];
$played_at = date("g:ia D jS M", filectime($filename));
The funky thing about this is it only uploads new info when there's actually something playing, so if like me, you have iTunes at work and at home you can run the script from both places and it'll always show the latest track info.
The AppleScript portion of this code is based almost entirely on the one found at: http://www.macosxhints.com/article.php?story=20040810091734861&mode=print which is well worth a read if you fancy having the track info displayed on screen. :)

