Switch Audio Input/Output Devices with Keyboard Shortcuts on OS X
— 2 min read
As part of the recent switch to work from home, I researched ways to improve my audio quality when talking in meetings and interviews. As a result, I invested in a wireless headphone with attached microphone. Other options included dedicated microphones, but I didn't feel those were as necessary.
Now it felt a bit cumbersome to have to manually toggle between output devices. So this weekend I spent some time to set up keyboard shortcuts to help switch between all the options. Here are the 3 steps needed (using switchaudio-x and Automator), along with a sample of what my Automator setup looks like.
I was surprised that my operating system (OS X) actually had a lot of the pieces ready to go -- Automator and Keyboard Shortcuts are well-integrated, for example -- though I still needed to install a small piece of software to bring everything together.
1. Create Automator services, one for each input/output set that I want to quickly access
- Install switchaudio-x
- Run
$ SwitchAudioSource -a
to see the device names recognized by the tool - Create a series of Automator services (I made one for each device I wanted to switch to) that will run the above as shell commands
- Automator has several modes. We'll use "Quick Action" and change the "Workflow receives" dropdown to
no input
:
The "Run" button helped me test that each of the services works as intended, so I knew I was on the right track!
Note that my SwitchAudioSource
directory might not be the same as yours. Run
$ which SwitchAudioSource
to customize the path as is appropriate for you. In
my case, it's /usr/local/bin/SwitchAudioSource
.
1" Service 1: 'Audio/ Switch to Laptop'2/usr/local/bin/SwitchAudioSource -t input -s 'Built-in Microphone'3/usr/local/bin/SwitchAudioSource -t output -s 'Built-in Output'4
5" Service 2: 'Audio/ Switch to Monitor'6/usr/local/bin/SwitchAudioSource -t input -s 'Built-in Microphone'7/usr/local/bin/SwitchAudioSource -t output -s 'DisplayPort'8
9" Service 3: 'Audio/ Switch to Headset'10/usr/local/bin/SwitchAudioSource -t input -s 'HyperX Cloud Flight Wireless Headset'11/usr/local/bin/SwitchAudioSource -t output -s 'HyperX Cloud Flight Wireless Headset'
Source: Switch Audio Outputs with a Keyboard Shortcut on OS X
2. Assign hotkeys to those Automator services
Next, I went to OS X's System Preferences \ Keyboard \ Shortcuts
to set up keyboard-based
activation of each service.
In my case, I used Ctrl-Shift-1, Ctrl-Shift-2, and Ctrl-Shift-3 for each script:
An alternative is to use a single hotkey to initiate a menu, and use a mouse/numbers to select one of the configurations. However, I opted for the faster option without a menu.
Source: How To Assign Keyboard Shortcuts To AppleScript and Automator Actions
3. (Workaround for a Mac OS X bug) Manually invoke those services from the Finder menu
Finally, everything looked like it was ready to go, but the hotkeys don't seem to do anything.
It turned out that I needed to open Finder \ Services
first before the keyboard shortcuts
could activate.
Source: Keyboard shortcuts not working for services
Another workaround is to use hotkeys that have the Cmd modifier key. See more on that in this StackExchange answer.
4. (Optional) Configuring for AirPods
In the case of AirPods, if they're not yet paired (e.g. because they're currently paired to a phone), then it's possible that the Automator script will fail. They can be instead toggled via a bluetooth-based AppleScript, as seen from this Reddit answer:
1-- Reminder that this is an AppleScript instead of a Shell Script, so2-- make sure to select the proper option from Automator before pasting this3
4use framework "IOBluetooth"5use scripting additions6
7set AirPodsName to "AirPods"8
9on getFirstMatchingDevice(deviceName)10 repeat with device in (current application's IOBluetoothDevice's pairedDevices() as list)11 if (device's nameOrAddress as string) contains deviceName then return device12 end repeat13end getFirstMatchingDevice14
15on toggleDevice(device)16 if not (device's isConnected as boolean) then17 device's openConnection()18 return "Connecting " & (device's nameOrAddress as string)19 else20 device's closeConnection()21 return "Disconnecting " & (device's nameOrAddress as string)22 end if23end toggleDevice24
25return toggleDevice(getFirstMatchingDevice(AirPodsName))
Z. Appendix: what I Googled
- "mac hotkey change output device": the apetronix link was the second result, though I also read through several
other results from macrumors, stackexchange, and personal blog posts.
- A stackexchange article (third result) showed me how to use Automator as an easier approach to AppleScript: Is it possible to make a hotkey for switching audio output?
- A personal blog post, Switching your Mac’s audio output device with a keyboard shortcut, showed an example of having a single hotkey with number-based menus to toggle between separate devices, though I ended up not doing this
- The keyboard-to-Automator setup was directly mentioned from the apetronix link. However, it's a bit buried.
- "osx services hotkey not working" helped surface the final bug workaround