This is a collection of bash scripts, an AppleScript, and PHP code which will automatically publish the currently playing iTunes track to your web server. The end result is a web element such as this:
|
|
This is a modification of Siftahs code, which is in turn an adaptation of Nevyns AppleScript. I found that several changes to Siftahs code were necessary. The main improvements are as follows:
/usr/bin/osascript: kCGErrorRangeCheck : Window Server communications from \ outside of session allowed for root and console user onlyIn order to prevent the console filling up with these error messages, the script checks that a user is logged in and running iTunes before attempting to execute the AppleScript. This has the advantage of speeding up the execution of the script - checking for iTunes via a bash script is significantly faster than processing an AppleScript.
The code comes in five files:
For the sake of example we will assume that the three files "CheckiTunes", "UploadiTunesTrack.scpt", and "UploadiTunesTrack" are stored locally on each machine in the folder "/path/to/scripts/", that the web server is known on the LAN as "Web-Server.local" and is accessible via the WAN at "www.mywebserver.com", and that the PHP script resides on the web server in the folder "/path/to/phpscript/".
First make the LaunchAgents plist file. This will tell the local machine to run "CheckiTunes" every three minutes whilst a user is logged in. Use your favourite text editor in the Terminal (vi, pico, etc.) and save the file on the local machine as "/Library/LaunchAgents/net.homeip.kasprzyk.checkitunes.plist" (note that you will need to use "sudo" to create and edit the plist file).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>net.homeip.kasprzyk.checkitunes</string>
<key>Program</key>
<string>/path/to/scripts/CheckiTunes</string>
<key>StartInterval</key>
<integer>180</integer>
</dict>
</plist>
Now make the script "CheckiTunes". This is the script which sets everything in motion. Again, use your favourite text editor in the Terminal. This time save the file on the local machine as "/path/to/scripts/CheckiTunes".
#!/bin/bash
# Check whether a user is logged in
LOGGEDIN=`/bin/ls -l /dev/console | awk '{print $3}' | fgrep -v root`
if [ "${LOGGEDIN}" != "" ]; then
# Check whether the user is running iTunes
if [ "`ps -x -u ${LOGGEDIN} | grep -v iTunesHelper.app | grep -c iTunes.app`" != "0" ]; then
# If so, launch the AppleScript
SCRIPT="/path/to/scripts/UploadiTunesTrack.scpt"
/usr/bin/osascript "${SCRIPT}" &>/dev/null
fi
fi
exit 0
Don't forget to "chmod +x" the script to make it executable.
Next we create the AppleScript. Simply paste the following code into the ScriptEditor and save it (on the local machine) as "UploadiTunesTrack.scpt" in "/path/to/scripts/".
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
end tell
set trk_desc to quoted form of (trk_name & "|" & trk_albm & "|" & trk_arts)
set the_command to "/path/to/scripts/UploadiTunesTrack " & trk_desc
do shell script the_command
end if
Now for the script which uploads the track information to the web server. Using your favourite text editor create the file "/path/to/scripts/UploadiTunesTrack":
#!/bin/bash
if [ "$1" != "" ]; then
# Set the file paths
LOGGEDIN=`/bin/ls -l /dev/console | awk '{print $3}' | fgrep -v root`
if [ "$LOGGEDIN" == "" ]; then
exit 0
fi
TEMPFILE="/tmp/${LOGGEDIN}_itunes_$$.txt"
TRACKFILE="/tmp/.${LOGGEDIN}_itunes_track.txt"
DESTFILE="/path/to/phpscript/${LOGGEDIN}_itunes_track.txt"
# Save the new file and remove any "missing value"s
TRACK=`echo "$1" | sed 's/missing value//g'`
echo "$TRACK" > $TEMPFILE
# Compare the new file with the old file -- is there anything to do?
if [ -s $TRACKFILE ]; then
cmp -s $TEMPFILE $TRACKFILE && (rm $TEMPFILE; exit 0)
fi
# The track has changed: replace the old file and update the webserver
mv $TEMPFILE $TRACKFILE
if [ "`hostname`" == "Web-Server.local" ]; then
cp $TRACKFILE $DESTFILE
else
# We are not running on the web server: use ssh to upload the changes
# Note that you'll need to have SSH Keys set up for this to work
# First try using the local network, then try over the WAN
scp -q $TRACKFILE name@Web-Server.local:$DESTFILE ||
scp -q $TRACKFILE name@www.mywebserver.com:$DESTFILE
fi
fi
exit 0
Once again you need to "chmod +x" the script to make it executable. Obviously you need to change "name@..." to the correct username for your web server. Public key authentication is required.
This is a good time to register your LaunchAgents plist file. In the Terminal, enter the command:
sudo launchctl load /Library/LaunchAgents/net.homeip.kasprzyk.checkitunes.plist
Finally you need to install the PHP script "UploadiTunesTrack.php" on your web server, in the folder "/path/to/phpscript/". You can view the PHP code here. Of course, you should modify the appearance using your own css styles.
To display the iTunes track data on a web page, use the following code:
<?php require_once( "/path/to/phpscript/UploadiTunesTrack.php" ); ?> ... <?php iTunesTrack( "username" ); ?>
where "username" is the name of the user whose iTunes data you wish to display. To ensure that all UTF-8 characters appear correctly, remember to include the <meta> tag:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
at the start of your HTML code, immediately after the <head>.