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
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'"
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
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.
Just as a matter of personal preference, I found it more convenient to run SwitchAudio by way of a shortcut, and I’ve included that below. The “Run Shell Script” action will need to be modified to whatever is desired.