If you frequently switch between your Mac’s speakers and headphones (or any multiple outputs), you know the first-world problem of having to open System Preferences.app, click on Sound, and change the output device.
You could also switct outputs by pressing ⌥ (option key) + mouse click on the volume icon in the menu bar, which displays all your inputs and outputs, like so:
Or, if you want to get fancy, map it to a keyboard shortcut or set of keys. We’ll be using a combination of shell scripting and AppleScript. Here we go:
- Unfortunately, there isn’t a native command line utility for this in OS X, so you need to download and install switchaudio-osx. You will also need to download and run Xcode and the Xcode Command Line Tools from the Mac App Store.
- In my opinion, the easiest way to install switchaudio-osx is to first install Homebrew. Open Terminal, located in Applications>Utilities, and run this:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
For more information, please check out the Homebrew site.
- Once Homebrew is installed, copy & paste the following into Terminal and hit enter:
brew install switchaudio-osx
- After it installs, enter this into a Terminal window to see the description of each available command for this utility:
- You’ll see the list of commands for the switchaudio-osx utility. Explore the commands if you want to get familiar, but it’s not necessary for this tutorial.
- To see all your audio sources (inputs and outputs), type:
- Find the names of the two audio outputs you’d like to switch back and forth with a keyboard shortcut. For this example, I will pick the outputs named Built-in Output and USB Audio CODEC .
- Next, we will create an AppleScript that will execute a shell script. We will be using the SwitchAudioSource -s command, as this switches between audio outputs. Copy and paste this into the Script Editor app, located in Applications>Utilities:
set the currentAudioSource to (do shell script "/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -c") if currentAudioSource is equal to "Built-in Output" then do shell script "/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -s \"USB Audio CODEC \"" else do shell script "/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -s \"Built-in Output\"" end if
- You must change the name of the outputs in line 3 and 5, as they will most likely not match mine. For example, let’s say one of your outputs was named Soundflower. Line 3 in the script would be:
do shell script "/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -s \"Soundflower\""
You would follow the same step for line 5. Save your script file (.scpt) once you’ve made these changes.
- Im my humble opinion, the simplest way to map scripts to a keyboard shortcut is to use BetterTouchTool, which I’ve covered here. It’s not necessary to use BTT, as any mapping software would work. You can always do it the free, native way on your Mac by following these instructions.