Introducing AirControl: AirPlay Mirroring from your terminal
AirControl is a tool to control AirPlay Mirroring from the command line.
TL;DR: it’s open source and available in our GitHub repo.
If you’re interested in the details of how we got it working, read on.
Once a month, the Engineering team has a Hack Day. Anybody can work on anything they like, be it discovering a new programming language, speeding up our test suite, or, in the case at hand, building a terminal tool to control AirPlay Mirroring.
The only constraint we put on these Hack Days is to work with other people on your project.
I use vim, Vimium, have custom mappings on my keyboard, wrote a Chrome extension to log you out of websites with a keyboard shortcut: in short, I like typing more than moving my mouse and clicking around. That’s why a couple of months ago I realized that having to use the mouse to activate AirPlay Mirroring was inconvenient… nay! Barbaric! Something needed to be done.
I recruited Eric and Chris to work on this last Friday.
We worked through the problem in stages:
Activate AirPlay Mirroring programmatically
First, we got a short AppleScript to activate the AirPlay menu in the menu bar:
You can open AppleScript Editor, paste this and run it, if you have an AirPlay device on your network, it should connect you.
Changing item 4
to item "Living-room"
works too.
Now we needed to have this AppleScript run from the terminal and take an argument.
To the terminal!
The easiest way for that is to use osascript
. osascript
lets you run any Open Scripting Architecture script and in particular AppleScript.
So, instead of using AppleScript directly, we let bash
invoke osascript
with the above script filled in with the first argument passed in for AirPlay device name:
You can run the above as aircontrol 2nd\ Floor\ Lounge
and you’ll connect to the AppleTV with that name.
NB: you’ll need to enable your terminal for Accessibility settings as shown below:
Picking up the tab
At this point, we have something that does what we wanted: starting AirPlay Mirroring from the terminal. But we want more! We want need to tab-complete this thing!
DNS Discovery
The first thing is to find the available device names. I had previously gathered some references that led us towards dns-sd
to find out what AirPlay devices are available.
Namely dns-sd -B _aiplay._tcp
will print out all the AirPlay-enabled devices on the network:
Using cut
would trim that down to the useful info.
(and make sure you check out this post on non-terminating bash processes)
You complete me
With that list in hand, we needed to tell bash
how to tab-complete the command.
The short story is that you run complete -o nospace -F _aircontrol aircontrol
where _aircontrol
is a function that builds an array called COMPREPLY
with the completion candidates.
Here’s our whole script to activate the tab-completion:
To wrap it up, we added an option to stop the mirroring: -k
.
Here it is in action:
The final result is available here: https://github.com/AdRoll/AirControl
As you can see, not that much code at all.
Starting from this, one could build a workflow for Alfred or a plugin for Spotlight. A Spotlight extension would be an interesting hack since it’s designed for files, but that will have to wait for another Hack Day…