20

I would like to change sound output device (like option-clicking the speaker icon in the menubar) via script, but NOT through UI scripting, e.g., launching the System Pref panel.

UPDATE: Using @Mateusz solution below, here is an Applescript that, when launched, will give you a popup to select the desired audio output device:

set devices to do shell script "/Applications/Audiodevice/audiodevice output list"
set answer to choose from list (paragraphs of devices) with title "Audio Output" with prompt "Select a device" without multiple selections allowed
if answer is false then return
do shell script "/Applications/Audiodevice/audiodevice output " & quoted form of item 1 of answer

(Note: you need to put the audiodevice file in /Applications)

6 Answers 6

6

There's audiodevice command available from whoshacks.

Usage:

  • audiodevice list devices for input, output, and system audio
  • audiodevice <port> display the audio device for the selected port
  • audiodevice <port> list list available audio devices for the selected port
  • audiodevice <port> <device> set the selected port to use the designated device ("internal" will select Internal Speakers or Headphones, whichever is active)
1
  • 1
    Seems it has stopped working under Catalina due to the drop of 32bit apps.
    – Jerry T
    Sep 21, 2021 at 0:36
44

There's switchaudio-osx, command which needs to be compiled from sources or can be installed via homebrew (Xcode installation required):

brew install switchaudio-osx

Usage:

SwitchAudioSource [-a] [-c] [-t type] [-n] -s device_name
  • -a : shows all devices
  • -c : shows current device
  • -t type : device type (input/output/system). Defaults to output.
  • -n : cycles the audio device to the next one
  • -s device_name : sets the audio device to the given device by name
9
  • I'm trying to install this using homebrew with brew install switchaudio-osx but it reports: switchaudio-osx: A full installation of Xcode.app is required to compile this software. Installing just the Command Line Tools is not sufficient. Xcode can be installed from the App Store. Error: An unsatisfied requirement failed this build. Any way around this?
    – Dan
    Oct 28, 2015 at 10:42
  • The build procedure of switchaudio-osx relies on dependencies etc. defined in Xcode (the application). Somebody who knows his way around Xcode better than I do could probably extract them into a Makefile.
    – nohillside
    Oct 28, 2015 at 13:53
  • 2
    @Ze'ev Or you evaluate the risks involved and get the version I just compiled out of my Dropbox dl.dropboxusercontent.com/u/3188370/SwitchAudioSource
    – nohillside
    Oct 28, 2015 at 13:57
  • 1
    @Ze'ev I've made a shellscript that automates the compilation/installation by downloading mas, then xcode, then switchaudio-osx. This way you benefit from updated source-code and not a pre-compiled binary that potentially is unsafe or falls out of date. See: install-switchaudio.sh Jan 30, 2018 at 17:30
  • 1
    Also available from macports www.macports.org.
    – MichaelR
    May 25, 2019 at 7:03
2

https://github.com/deweller/switchaudio-osx

To install open terminal and run brew install switchaudio-osx

To list all the sources for input / output run SwitchAudioSource -a

This will produce output similar to

External Microphone (input)
MacBook Pro Microphone (input)
External Headphones (output)
MacBook Pro Speakers (output)

To switch microphone to External Microphone issue following command

SwitchAudioSource -t input -s 'External Microphone'

https://appsnomad.com/blog/how-to-change-audio-output-input-from-terminal-in-osx/3

1
  • 3
    This answer isn't any different from the highest voted answer by Mateusz Szlosek
    – Allan
    Feb 16, 2020 at 5:35
2

I needed a keyboard shortcut for changing my sound output -- I liked the simplicity of the AppleScript answers I've seen on the Stack Exchange forums, but I don't like the idea of having to hardcode the output/input device name I want to change. Also I couldn't find an elegant solution for swapping between 3 audio outputs using if-else. Not to mention if the source is not always in the output list (e.g. bluetooth headphones).

So I wrote the below which will simply loop through all of the options and select the next source in the list / the first source in the list if the last option is selected. Just change the sourceToChange variable from "Output" to "Input" if you'd like to make another script for swapping microphone sources. One downside to this approach is that you have to grant "Control your computer" access for any app that calls this workflow / quick action. I'm guessing it has something to do with calling the "System Events" application but I'm not 100% on that. Also not 100% sure on the security Nevertheless, I hope it helps some folks. It works for me!


# loop thru all sound outputs in "System Preferences" > "Sound" > "Output"/"Input"

on run {input, parameters}
    
    set sourceToChange to "Output" # change me to "Input" or "Output"!
    
    tell application "System Preferences"
        run
        set current pane to pane "com.apple.preference.sound"
        
    end tell
    
    tell application "System Events"
        
        tell application process "System Preferences"
        
            repeat until exists tab group 1 of window "Sound" # prevent script from executing before Sound load
            
            end repeat
            
            tell tab group 1 of window "Sound"
                
                click radio button sourceToChange
                set soundOutputs to a reference to every row of table 1 of scroll area 1
                set activateSound to false
                set exitMe to false
                
                repeat with i from 1 to count of soundOutputs
                    
                    if activateSound is true then
                        
                        set exitMe to true
                        set selected of row i of table 1 of scroll area 1 to true
                        
                        # if attempted to set the line break before AirPlay devices to true, set the first row to true and exit
                        if (not selected of row i of table 1 of scroll area 1) then
                            set selected of row 1 of table 1 of scroll area 1 to true
                            
                        end if
                        
                    end if
                    
                    if (selected of row i of table 1 of scroll area 1) and (activateSound is false) and (exitMe is false) then
                        set activateSound to true
                        
                    end if
                    
                    if exitMe is true then
                        return
                        
                    end if
                    
                end repeat
                
                # Input doesn't suffer from the AirPlay linebreak issue so this catches an attempt to exit at the last row before setting selected to the first item
                set selected of row 1 of table 1 of scroll area 1 to true
                
            end tell
        end tell
    end tell
end run

1
  • To scratch exactly this itch, the switchaudio-osx util has been updated to now support cycling to the next audio device. After installing with homebrew, just call SwitchAudioSource -n to cycle to the next device. This avoids the need to hard core in device names but also avoids the need to maintain complicated scripted UI interactions in AppleScript.
    – Chris
    Apr 21 at 17:02
1

Here is an AppleScript to do it with switchaudio-osx (just replace the XXXXXXX with the device name you can get from executing "SwitchAudioSource -a"):

on run {input, parameters}

    do shell script "/usr/local/bin/SwitchAudioSource -s 'XXXXXXXX'"

    return input
end run
2
  • 1
    I've replaced the path to point to /usr/local/bin instead of the versioned Homebrew directory. Otherwise the script will no longer work if switchaudio-osx gets updated.
    – nohillside
    Jan 29, 2020 at 6:28
  • Thanks @nohillside!!! Jan 30, 2020 at 14:22
0

There is aloe a paid $5 app called Ears that will do the job and can be used with the command line https://retina.studio/ears/

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 15 at 0:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .