23

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)

d0g
  • 3,902

6 Answers6

51

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
udondan
  • 123
  • 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?

    – d0g Oct 28 '15 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 '15 at 13:53
  • 2
    @Ze'ev Or you evaluate the risks involved and get the version I just compiled out of my Dropbox https://dl.dropboxusercontent.com/u/3188370/SwitchAudioSource – nohillside Oct 28 '15 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 – Niko Jan 30 '18 at 17:30
  • brew install switchaudio-osx! – d0g Jan 30 '18 at 23:10
  • On my Mac the list of output devices is not in sync with what I see in a top bar or in a Sound preferences. I am using Boom 3D which incorporate itself into sound subsystem. – Denis The Menace May 13 '19 at 15:54
  • 1
    Also available from macports www.macports.org. – MichaelR May 25 '19 at 07:03
  • still working in monteray, thanks! – Radu Ursache Dec 06 '21 at 17:28
  • 1
    I didn't have to do any weird xcode stuff to install this, in 2022 on Monterey. So try doing the brew install before worrying about the xcode stuff. – XP84 May 10 '22 at 00:03
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)
2

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
nohillside
  • 100,768
  • 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 '20 at 06:28
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 &quot;Output&quot; # change me to &quot;Input&quot; or &quot;Output&quot;!

tell application &quot;System Preferences&quot;
    run
    set current pane to pane &quot;com.apple.preference.sound&quot;

end tell

tell application &quot;System Events&quot;

    tell application process &quot;System Preferences&quot;

        repeat until exists tab group 1 of window &quot;Sound&quot; # prevent script from executing before Sound load

        end repeat

        tell tab group 1 of window &quot;Sound&quot;

            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 '22 at 17:02
1

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

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
    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 May 15 '22 at 00:04