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
From 2021, a big thank you, sincerely ❤️
Working in 2021 on my M1 mac mini. Just a few notes\missed steps..
– After installing homebrew, there’s a lot going on in terminal and it’ll take a while to complete, but there’s two lines of code you need to run manually at the end, which it tells you you need to run, but you might miss. If you don’t do these, you’ll get a brew command not found error, so watch out for these. The two lines start with echo and eval and it says so right at the end of the install of homebrew.
– When editing the script make sure line two which says “built in audio output” is the same output device as the one you put on line 5. So you actually have to make 3 edits in the script, not just the one on line 3 and 5.
– Finally, my script wouldn’t run – the path I had to use in the script was “/opt/homebrew/Cellar/switchaudio-osx/1.1.0/SwitchAudioSource” (in place of what’s in the script on this page. Also note the version number of switchaudiosource has changed to 1.1.0.)
After doing this, I put the whole script into Automator and saved it as an application, now I can use it from my dock or assign it to a key on my keyboard.
Thanks heaps!
How do you assign the key shortcut to the script in automator at the end? I am newbie and it’s not specified in the article. I am confused For the rest you are right. I’ve got a M1 Mac too and I had to change the things you said.
Follow up – on Monetary the location of Brew and SwitchAudioSource is changed to
/usr/local/Cellar/switchaudio-osx/1.1.0/SwitchAudioSource
Also on my MacBook Pro’s ‘default audio output’ is called ‘MacBook Pro Speakers’ as I think ‘Built-in Output’ is no longer used.
I purchased Shortery on App Store (https://apps.apple.com/nz/app/shortery/id1594183810?mt=12) so when I plug my HP TB3 Dock on my desk in it switches to the speakers I have connected to that and when I unplug it switch the audio output back to the MacBook Speakers.