Scripting Airfoil via AppleScript

Scripting via AppleScript

Beginning with version 2.0, Airfoil supports a full AppleScripting suite. Just about anything you can do with the graphical user interface, you can now also script. The following is a brief overview of the object model that Airfoil exposes. For a complete reference, see Airfoil's Scripting Dictionary in Script Editor. If you are new to AppleScript, we recommend you pick up a copy of AppleScript : The Definitive Guide by Matt Neuburg, before proceeding.

  1. Accessing and Controlling Speakers
  2. All About Audio Sources
  3. Audio Sources Types
  4. Putting it all Together

Accessing and Controlling Speakers

The primary object in Airfoil, is the speaker. It represents an Aiport Express on the network, which you can start and stop and set the volume of. Before that though, we need to actually retrieve a speaker instance on which to operate on. Airfoil maintains a list of avaliable speakers on the application object.

tell application "Airfoil" get first speaker --speaker id "0011245FA40D" get every speaker --{speaker id "0011245FA40D", speaker id "001124000AE3"} get every speaker whose name is "Kitchen" --{speaker id "001124000AE3"} end tell

Once we have a speaker, we can set it's volume, or connect/disconnect from it. Here's an example script that toggles the connection to the first speaker found:

tell application "Airfoil" set aSpeaker to first speaker if not (connected of aSpeaker) then set (volume of aSpeaker) to 0.5 connect to aSpeaker else disconnect from aSpeaker end if end tell

All About Audio Sources

Beyond controlling the speakers, Airfoil's AppleScript dictionary gives you complete control over the audio source used. The five types of audio sources are all represented: application source, widget source, device source, radio source, and system source.

Application sources represent an application on the hard disk. To target an application source, you create a new instance, set it's application file property to the full POSIX path of an application, and then set the application's current audio source to it:

tell application "Airfoil" set aSource to make new application source set application file of aSource to "/Applications/iTunes.app" set current audio source to aSource end tell

Device sources represent hardware audio devices such as microphones or line-in ports. Airfoil maintains a list of device sources from which you can select, and then target. The following example script displays a "choose from list" dialog with every audio device in it:

tell application "Airfoil" set listOfDevices to name of every device source set selectedDevice to choose from list listOfDevices with prompt "Choose an audio device:" set aSource to first device source whose name is (first item of selectedDevice) set current audio source to aSource end tell

radio sources represent hardware AM/FM radios such as the RadioSHARK or D-Link DBR-100. Like device sources, Airfoil maintains a list of connected radio devices that you can choose from. The radio device can be tuned by using the band and frequency properties:

tell application "Airfoil" set aSource to first radio source set band of aSource to FM set frequency of aSource to 770000 -- 770 AM set frequency of aSource to 89900000 -- 89.9 FM set current audio source to aSource end tell

If you have the Soundflower extra installed, you can use the system sources to transmit all audio from the computer. Naturally, there is only ever one System Audio source:

tell application "Airfoil" set aSource to first system source set current audio source to aSource end tell

Putting it all Together

Finally, heres a complete script showing a typical usage of Airfoil. We find a target application, create a new application source for it, and then transmit it to a speaker.

tell application "Airfoil" --Find QuickTime Player set pathToApp to (POSIX path of (path to application "QuickTime Player")) --Create a new source and set it to QT set newSource to make new application source set application file of newSource to pathToApp --Set the source set (current audio source) to newSource --Set the speaker volumes set (volume of every speaker) to 0.5 --And finally start transmitting connect to every speaker end tell