Using applescript and BTT to make the AirPods experience a little bit better

Ian Gloude
2 min readJun 7, 2017

Let’s be clear off the bat, this post is not about me trying to justify buying AirPods. There are enough of those posts out there already. I like the little guys. I describe them in the same way as I do my Apple Watch — it doesn’t change my life, but its very convenient and I’ve grown to like it. It’s a gadget. And I like gadgets.

At work I find myself constantly switching the AirPods between my laptop and phone. This requires manually going into the bluetooth menu and connecting to the device. I cannot be more clear on this point: THIS IS HOW IT SHOULD BE. I have seen countless forum posts of people lamenting this extra step. Your laptop and/or phone are not smart enough to know when you want to switch audio sources.

We can come up with a way to make manually switching sources easier. Unfortunately, there is no API to directly interact with bluetooth devices in OSX. But, we can engineer a creative/hacky workaround using applescript.

Create an .applescript file with the code below. I chose to make my script a dotfile, so it would be invisible to basic file searches. For simplicity’s sake, I made my filepath ~/.airpod-connect.applescript. Location and visibility doesn’t matter in the end.

tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "YOUR_DEVICE_NAME") of menu of bt
click
tell menu 1
if exists menu item "Connect" then
click menu item "Connect"
else
click bt
end if
end tell
end tell
end tell

You’ll need to replace YOUR_DEVICE_NAME with your device’s name. Then make sure to enable the bluetooth icon in your menu bar.

Now, the script should be working from the command line. You can test it by running osascript your/file/path/file.applescript. For me, this command would look like: osascript ~/.airpod-connect.applescript. You may need to give security permissions to the terminal and/or BTT.

The final step is creating a shortcut with BTT to execute a terminal command. Open BTT and create a new Keyboard shortcut. Under Trigger Predefined Action select Execute Terminal Command and paste in the osascript command with your path to the .applescript file.

All done! Now you have a keyboard shortcut that pairs your AirPods to your Mac.

--

--