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:
SwitchAudioSource
- 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:
SwitchAudioSource -a
- 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.
Hi! Great tool, but it doesn’t list AirPlay devices. So you cannot change sound output to them. How come?
Thanks a bunch!
Your script was the exact thing that I needed to switch sound outputs on the fly with a keyboard shortcut using Alfred.
Thanks for the tip! Unfortunately I’ve noticed a bug in regards to having the “selected sound output device” chosen for the “Sound Effects” output as a result of this:
If switching the sound output, the mac “Sound Effects” output will automatically remain as the previously selected source. For example, I have chosen “selected sound output device” as the “play sound effects through:” option under the “Sound Effects” tab in “Sound” otpions. I am currently outputting sound to my “Internal Speakers” and can verify that via the “Output” tab in “Sound” options. If I run the script to switch the audio source to say “Headphones”, upon going back to the “Sound Effects” tab I see “Internal Speakers” now chosen as the “play sound effects through:” option.
Hey Eric,
I’m running into the same issue. Did you ever figure it out?
Unfortunately I never did… just learned to live with it
Seems like it’s issue with switchaudio-osx package itself, there’s already an issue since 2015: https://github.com/deweller/switchaudio-osx/issues/1
I have checked your website and i’ve found some duplicate content, that’s why you don’t rank
high in google, but there is a tool that can help you to create
100% unique articles, search for; Boorfe’s tips unlimited content