Switch Audio Outputs with a Keyboard Shortcut on OS X

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:

Menu bar: Volume

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:

  1. 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.
  2. 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.

  3. Once Homebrew is installed, copy & paste the following into Terminal and hit enter:
    brew install switchaudio-osx
  4. After it installs, enter this into a Terminal window to see the description of each available command for this utility:
    SwitchAudioSource
  5. 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.SwitchAudioSource Command Line
  6. To see all your audio sources (inputs and outputs), type:
    SwitchAudioSource -a

    SwitchAudioSource Show Devices

  7. 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 .SwitchAudioSource Show Selected Devices
  8. 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
  9. 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.

  10. 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.

6 Comments

  1. 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.

  2. 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.

  3. 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

Leave a Reply

Your email address will not be published. Required fields are marked *