« My View of Red Sox Spring Training 2005 | Main | In Baltimore for MedBiquitous »

April 4, 2005

AppleScript to Get Around Unregistered Airfoil

Over the weekend I was listening to the audio from a series of presentations available as streaming Windows Media. When I've done this in the past I've plugged my laptop into the home stereo so I'm not glued to a headset somewhere.

It seemed like foolishness to have to plug in, when the Airport Express was already there and plugged in. With a little searching around I found Airfoil. Airfoil is a small application that will let you stream any audio (not just iTunes) to your Airport Express. It works very well, but is not free. If you don't pay the $25 registration fee, Airfoil will stream 10 minutes of audio and then add static into the stream to make the stream inaudible.

At first I thought about paying the $25, but since it's rare that I need to stream anything other than iTunes I decided it was a bit over my price range. I decided that for the few hours I was listening, I could just stop and start the stream every 10 minutes. This got kind of annoying too as I was often not at my laptop when the static started up.

After a handful of manual restarts I decided this was a job for AppleScript. What I needed was a script to press the button twice every 10 minutes, once to stop the stream and another to start. Having never written an AppleScript before I did some poking around and found that there are a nice collection of sample scripts installed with OS X that I could pull from to do just what I needed. I ended up with this short script that activates Airfoil, holds down the command button and presses "t" (for transmit off), holds down the command and presses "t" again (for transmit on) and then waits for 10 minutes before looping back to do the same thing:

tell application "Airfoil"
end tell
tell application "System Events"
if UI elements enabled then
tell process "Airfoil"
set frontmost to true
end tell
keystroke "t" using command down
keystroke "t" using command down
end if
end tell
delay 600
end repeat
This works really well, if you are willing to have a few seconds of silence every 10 minutes while Airfoil stops and starts it's broadcast. If that's not acceptable . . . it's probably worth your $25 to register Airfoil.

Posted by mike at April 4, 2005 2:55 PM