Switching your Mac’s audio output device with a keyboard shortcut

Here’s a typical scenario:

I’m writing an article, and I’m listening to music on Spotify over a bluetooth speaker that’s connected to my MacBook Pro. Suddenly, both my iPhone and Mac start to chime1. I have an incoming phone call.

When the phone rings I need to to two things under duress: put my headphones on and switch the audio output from the bluetooth speaker to the headphones. And I need to do this all before the call goes to voicemail. Seems simple enough. But it’s tougher than it sounds.

I know there’s a fast way to switch audio devices by Option-clicking on the volume icon in the menu bar and then choosing the audio device from the dropdown menu, and this sounds easy enough. But those menu bar icons are pretty small and there’s something about mousing under pressure that shoots my aim to hell. So this shortcut just isn’t a solution for me.

If I could create a simple keyboard shortcut that let me switch between all of my audio devices, that would be the best solution. But the question is, can I quickly switch between audio devices with a keyboard shortcut?

Using an applescript to set the audio device on a Mac

After a little googling, I found a simple, short applescript here, edited it a little to suit my purposes, and assigned it a keyboard shortcut to switch between the three audio output devices.

The applescript goes into the system preferences and picks the output device you indicate from the list of devices currently available. I’m sure that, in more skilled hands, this could all be done from a single instance of the script, but I chose to use 3 different versions of the script, each one indicating one of the three audio devices that I want to switch between.

#!/usr/bin/osascript

tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"

end tell

tell application "System Events" to tell process "System Preferences"

tell table 1 of scroll area 1 of tab group 1 of window 1

select (row 1 where value of text field 1 is "Name-of-thing")

end tell

end tell

quit application "System Preferences"

Just edit “Name-of-thing” to the name of the device as it appears in the Sound preference pane, run it, and it will set that device as the current one.

Creating an audio output switcher shortcut with Keyboard Maestro

Next, I needed to set up keyboard access to this script. There are a number of ways to do this, Automator, Better Touch Tool, and others, but I chose to use Keyboard Maestro because it’s very powerful, very flexible, and I already use it to manage dozens of my shortcuts.

To keep the shortcut memory overhead to a minimum, I assigned all three scripts the same keyboard shortcut. In Keyboard Maestro, this pops up a palette with each of the actions listed. Just type the number that corresponds with the action’s position in the list. To make this easier, I number each of the items in the list. I then assigned CMD, OPT, CTRL, Shift + S (for ‘sound’) to this palette.

Step-by-step

Here’s a quick step-by-step walk thru of creating an audio output switcher with Keyboard Maestro.

  1. In a text editor, edit the applescript included in this article. Replace “Name-of-thing” with the name of the device exactly as it appears in the Sound Preferences pane.
  2. Create a new group in Keyboard Maestro called Audio Switcher
  3. Create a new shortcut in this group. Name it “1. name of your device”. Using a numeral will make this item appear first in the list, and will be a helpful visual cue for what number to hit when you summon the palette.
  4. Click the New Trigger symbol and enter your desired hot key trigger
  5. Click the New Action Symbol and search for Execute Applescript in the list of actions
  6. Keep the default options for the new action.
  7. Paste the edited applescript into the script field.
  8. Test the script.

Repeat for each device you have. I recommend using the same hot key trigger and using the palette to reduce the number of shortcuts you need to keep track of.

Links

Easy Audio Output Toggle Using AppleScript, Growl and Quicksilver

Keyboard Maestro

  1. I use Continuity to make and take calls on my computer. Since my co-workers use a range of technologies for voice and visual communication, it’s incredibly convenient to use a single device, my Mac, for all of it. A single device, a single set of headphones with a mic.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s