Send Track Info to a Web Page, part 1
Frequent visitors to this site may have noticed that occasionally you will see a little blurb in the left column that says "Doug's iTunes is now playing such-and-such by so-and-so". It's what the iTunes on the "kMac"—my wife Natalie's name for the kitchen Mac—is currently playing.
This is acomplished with a Stay-Open AppleScript applet that monitors iTunes and writes the information about the current track to a local text file which is then FTP'd to my website via Fetch 3.0.3. The excellent trackReporter by Bret Van Horn does that very thing in OS X using the UNIX curl program to FTP the file. Unfortunately, OS 9 has no such ability, so what follows describes how I modified trackReporter to use the very scriptable Fetch instead.
First, by way of explaination: This site uses PHP to serve webpages and with PHP you can use include files (Perl CGIs can also use include files). An include file is just a text file that contains info that you wish to keep seperate from a page, but which can be loaded by that page. A common use for include files is when a site uses the same text and links in a footer; every page calls the one include file that has the footer info so that the same info needn't be written to every single page on the site.
Thus, every page on this site has a little piece of code that calls the "itunesisplaying.inc" include file. Whenever you load or refresh a page, the contents of the include file is displayed in the left column.
But before any of that happens we have to create the include file locally. So let's start scripting.
Our script will consist of two parts: an implicit run handler and an idle handler. (I explain how to create Stay-Open applets with idle handlers here, so pardon me if I gloss over any details.) The routines in the implicit run handler will run once, when the script is first activated, setting up variables for later use. The routines in the idle handler are run every few seconds by virtue of setting the idle handler's final return value to a number of seconds. The routines in the idle handler will check iTunes for a playing track. Whenever the track changes, the name and artist will be written to a text string. Here's how that looks:
-- declare variables for later use global sample_frequency -- how often we should check iTunes global itunes_active -- if iTunes is active or not global dbid -- stores current track's database id -- implicit run handler, runs at startup only -- initialize variables set sample_frequency to 15 -- seconds between checking iTunes set dbid to "" -- start of monitoring section on idle -- check if itunes is running set itunes_active to false tell application "Finder" -- "System Events" in OS X if (get name of every process) contains "iTunes" then set itunes_active to true end tell -- if itunes is running then we are go if itunes_active then tell application "iTunes" if player state is playing then try tell application "iTunes" set d to database ID of current track if d is dbid then -- if current track hasn't changed return sample_frequency -- check again shortly end if set dbid to d -- re-init dbid with current track's id set theTitle to name of current track set theArtist to artist of current track end tell set theMessage to ("Doug's iTunes is now playing \"" & theTitle & "\" by " & theArtist) end try else -- itunes is active but not playing so check back later set idle_time to 60 end if end tell set idle_time to sample_frequency else -- itunes is not currently active so wait before checking again set idle_time to 360 end if return idle_time end idle [upm] click to open in Script Editor
One would save that as an Application with "Stay-Open" checked in the Save dialog.
All it does so far is create a string called theMessage everytime the current track changes. We want to write that string to a text file. Here's a sub-routine from trackReporter that works very nicely:
--function to write a text string to text file on write_to_file(this_data, target_file) try set the target_file to the target_file as text set the open_target_file to open for access file target_file with write permission set eof of the open_target_file to 0 -- erase current contents write this_data to the open_target_file close access the open_target_file return true on error error_message number error_number return error_message end try end write_to_file [upm] click to open in Script Editor
Paste that into the script after what we've already written. We'll want to call this function in a couple of places in the script (you'll see where in the next section), like so:
my write_to_file(theMessage, data_file)
We'll also have to declare the global variable data_file and initialize it in the run handler. The data_file is the path to the text file that we are updating. The top of our script will look like this:
-- declare variables for later use global sample_frequency -- how often we should check iTunes global itunes_active -- if iTunes is active or not global dbid -- stores current track's database id global data_file -- path to text file to update -- implicit run handler, runs at startup only -- initialize variables set sample_frequency to 15 -- seconds between checking iTunes set dbid to "" set data_file to "path:to:itunesisplaying.inc" [upm] click to open in Script Editor
The path to the text file should be written as a string, like "Macintosh HD:Documents:itunesisplaying.inc".
Next, the routine that ships the text file off to your website using Fetch, and the finished script...