AppleScripting Airfoil

Airfoil supports a full AppleScripting suite, so that just about anything you can do with the graphical user interface you can 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.

Airfoil's AppleScript Dictionary

Accessing and controlling speakers

The primary object in Airfoil, is the speaker. It represents an Airport 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 available 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 its 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, device source, and system source.

Application sources represent an application on the hard disk. To target an application source, you create a new instance, set its 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/Music.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

You can use the System Audio source 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

Source remote control

As well as the built-in AppleScript dictionary, Airfoil also uses AppleScript for handling Remote Controls (from the AppleTV or Keyspan Express). When Airfoil receives a command from the remote control, it calls a given handler in a script. The following handlers are defined:

remote_play --Play/Pause
remote_stop
remote_pause
remote_previous_item
remote_next_item
remote_begin_seek_backward --Always followed by a call to remote_end_seek
remote_begin_seek_forward --Always followed by a call to remote_end_seek
remote_end_seek
remote_volume_up
remote_volume_down
remote_mute
remote_shuffle
remote_other --Currently unused

Example Remote Control scripts can be found and modified inside of Airfoil at: Airfoil.app/Content/Resources/RemoteControl/. Custom remote control scripts can be placed in ~/Library/Application Support/Airfoil/RemoteControl/.

Get in touch

That's all for now. If you do something interesting with this, let us know! As well, if you have need for more AppleScript control, get in touch and we'll consider adding it.


← Back to Airfoil for Mac Support Center