Under The Microscope

Scripting Airfoil for Windows 3.4 (And Up)

Developers have often asked for the ability to interface with Airfoil for Windows via some kind of scripting interface. There was very limited support for this in version 2.x of Airfoil for Windows, but there was always more we wanted to do with it.

Now in Airfoil for Windows 3.4 we have full out-of-process COM scripting support, which should allow 3rd party apps in just about any language (any language that supports COM that is), to interact with and control Airfoil for Windows.

For example, suppose you wanted to get a list of all recent sources using Windows JScript:


var airfoilApp = WScript.CreateObject("RogueAmoeba.Airfoil");

var recentSources = airfoilApp.GetRecentSources();
for(var i = 0; i < recentSources.Count(); i++)
{
	var audioSource = recentSources.Item(i);
	WScript.Echo("Recent source " + i + " is " + audioSource.Name());
}

Or maybe you'd like to see if a certain application is running, and if it is, tell Airfoil for Windows to start intercepting its audio:


var airfoilApp = WScript.CreateObject("RogueAmoeba.Airfoil");

var runningSources = airfoilApp.GetRunningSources();
for(var i = 0; i < runningSources.Count(); i++)
{
	var audioSource = runningSources.Item(i);
	WScript.Echo("Running source " + i + " is " + audioSource.Name());
	
	// The Id() method returns the full path to the source, so 
	// we have to look at just the ending.
	if(endsWith(audioSource.Id().toLowerCase(),"firefox.exe"))
	{
		airfoilApp.SetCurrentSource(audioSource);
	}
}

function endsWith(str, suffix) 
{
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

Interfacing with and controlling remote speakers is just as easy. This script enumerates the list of remote speakers that Airfoil for Windows can see, adjusts their volume, and then connects to any speaker that isn't currently connected:


var airfoilApp = WScript.CreateObject("RogueAmoeba.Airfoil");
var speakerCollection = airfoilApp.GetSpeakers();

for (var i = 0; i < speakerCollection.Count(); i++)
{
	var speaker = speakerCollection.Item(i);
	WScript.Echo("Speaker " + i + " is " + speaker.Name() + "\n");
	
	// Set the volume to half and connect if it isn't already connected
	speaker.SetVolume(0.5);
	if(!speaker.Connected())
	{
		speaker.Connect();
	}
}

Finally, you can also request to be notified of various events, including when source metadata changes:


var airfoilApp = WScript.CreateObject("RogueAmoeba.Airfoil", "AirfoilApp_");
var metadataProvider = airfoilApp.GetCurrentMetadata(); 

function AirfoilApp_OnMetadataChanged()
{
	WScript.Echo("Metadata Was Changed.");
	if(metadataProvider.ProvidesTrackMetadata())
	{
		WScript.Echo("Current track: " + metadataProvider.Title());
	}
}

There is plenty of other functionality provided by the scripting interface which isn't shown in the above examples. You can find the complete documentation of the API here:

Airfoil for Windows API Documentation and Examples

If you create a script or application to control Airfoil, be sure to let us know!

Leave a Reply

You must be logged in to post a comment.

Our Software