SwitchAudio, a command line tool to change the Audio input and output device

On the occasion of the topic Setting the input source in System Settings I extended the mentioned command line interface to be able to switch both input and output devices. Unlike the older AudioInput executable you have to provide the switches -i and -o for input and output

This required a new name. Now it’s called SwitchAudio, the man page is

SwitchAudio usage:
	input, -i [deviceName]		        switch to the specified input device
	output, -o [deviceName]	            switch to the specified output device
    toggle, -t [deviceName,deviceName]	toggle between the two comma separated output devices
	list, -l 			                list all input and output devices
	help, -h 			                show this list

The toggle command switches between two comma separated output devices.

An example

set returnValue to do shell script "/path/to/SwitchAudio -o Audio\ Out"

The return value is either a meaningful status or error message

Download: SwitchAudio
Minimum system: macOS 10.15 Catalina, compiled and code-signed for Apple Silicon and Intel

1 Like

Stefan. I tested your utility and it works great. Many thanks!

I have two output sources and wanted to write a script that would toggle between them. However, there doesn’t appear to be any way to get the current output speaker, except with the system_profiler shell command, which is slow. It’s a simple matter to write two scripts, one for each speaker, but I thought I would ask. Thanks again.

set currentSpeaker to do shell script "/usr/local/bin/SwitchAudio -l" --> 
(*
"Input Devices:

Output Devices:
DELL S2722QC
Mac mini Speakers"
*)

set theResult to do shell script "/usr/local/bin/SwitchAudio -o " & quoted form of "DELL S2722QC" --> "Switched audio output device to 'DELL S2722QC'"

set theResult to do shell script "/usr/local/bin/SwitchAudio -o " & quoted form of "Mac mini Speakers" --> "Switched audio output device to 'Mac mini Speakers'"

Please see the edit.

There was more effort to validate the input than the few lines to toggle the devices :wink:

1 Like

Thanks Stefan–that works great. My script FWIW:

set speakerOne to quoted form of "DELL S2722QC"
set speakerTwo to quoted form of "Mac mini Speakers"
set currentSpeaker to do shell script "/usr/local/bin/SwitchAudio -t " & speakerOne & "," & speakerTwo
display notification currentSpeaker -- if desired

hello,
I have a beginner question…

I put the SwitchAudio in my folder /Macintosh HD/applications/Filemaker Pro 19.

when I run the script step

set currentSpeaker to do shell script “/Macintosh HD/Applications/Filemaker Pro 19/SwitchAudio -l” →

I get the error

error “sh: /Macintosh: No such file or directory” number 127

what I am doing wrong

thank you

You have to escape the path because it contains space characters.

Either by inserting backslashes in front of any space character

set currentSpeaker to do shell script "/Macintosh\ HD/Applications/Filemaker\ Pro\ 19/SwitchAudio -l"

or by wrapping the entire path in single quotes

set currentSpeaker to do shell script "'/Macintosh HD/Applications/Filemaker Pro 19/SwitchAudio' -l"

or by escaping the path with quoted form of

set currentSpeaker to do shell script quoted form of "/Macintosh HD/Applications/Filemaker Pro 19/SwitchAudio" & space &  "-l"

or by using a relative path and quoted form of

set currentSpeaker to do shell script quoted form of (POSIX path of (path to applications folder) & "Filemaker Pro 19/SwitchAudio") & space & "-l"

You probably know this, but one can use any path with quoted form of, it doesn’t have to be a relative path. Just for other readers

1 Like

@jm1

Thanks, of course, I edited my answer.

1 Like

Great
I understand now…

My question is how do I extract the name of the input devices from the “list”, so I can I have the user select one.

First of all I consider to add the functionality to list only input or output devices.

But now you have to parse the output of the -l command, in case of the input devices

set listDevices to paragraphs of (do shell script "/usr/local/bin/SwitchAudio -l")
set inputDevices to {}

if item 1 of listDevices is "Input Devices:" then
	repeat with i from 2 to (count listDevices)
		set currentItem to item i of listDevices
		if currentItem is "" then exit repeat
		set end of inputDevices to currentItem
	end repeat
end if

if inputDevices is {} then return
set newInput to choose from list inputDevices
if newInput is false then return
set newInput to item 1 of newInput

The selected source is in the variable newInput or the script finishes silently.

I recommend to put the SwitchAudio executable into /usr/local/bin which is the default location for custom command line tools.

Perfect !

I can’t seem to discover where /usr/local/bin is?

It’s an invisible folder.
In Finder press ⇧⌘G and enter /usr/local/bin

great…
found it and thank you.