Perhaps it helps someone, so I described how I got it to work:
I've tried the instructions from Jun 2016 from @ccstone
But I was always told AudioSwitcher not found.
There is no list generated by the -c switch – that only displays the current device.
Run:
SwitchAudioSource -h
In the Terminal.app to see SwitchAudioSource's help.
To list all available devices:
SwitchAudioSource -a
To discover if a certain device exists in Bash:
deviceExists=$(SwitchAudioSource -a | awk '/Soundflower \(2ch\) \(output\)/ { print }')
if [ "$deviceExists" != '' ]; then
echo "Device $deviceExists Exists!"
else
echo "Device $deviceExists Does NOT Exist!"
fi
You could also do this by:
Running SwitchAudioSource -a and capturing the output to a variable.
Then use Keyboard Maestro's own search to look for your device.
My goal is the following:
In my office I want to switch between A/B.
At home I want to switch between A/C
(B always exists because it's the internal speaker, C is only connected at home)
Here is the script, that makes sense to me (but doesn't work):
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
currentOutput=$(SwitchAudioSource -c); echo $currentOutput
deviceExists=$(SwitchAudioSource -a | awk '/C \(output\)/ { print }')
if [ "$currentOutput" = 'C' || "$currentOutput" = 'B' ]; then
SwitchAudioSource -s 'A'
else
if [ "$deviceExists" != '' ]; then
SwitchAudioSource -s 'C'
else
SwitchAudioSource -s 'B'
fi
➜ ~ SwitchAudioSource -a
Background Music (input)
Background Music (UI Sounds) (input)
MacBook Air Mikrofon (input)
USB Audio (output)
Background Music (output)
Background Music (UI Sounds) (output)
Externe Kopfhörer (output)
MacBook Air Lautsprecher (output)
Screen Record w/ Audio (output)
as you can see I use Background Music.app which complicates things a little. but even without the script doesn't work.
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
currentOutput=$(SwitchAudioSource -c); echo $currentOutput
deviceExists=$(SwitchAudioSource -a | awk '/Scarlett Solo USB \(output\)/ { print }')
if [ "$currentOutput" = 'Scarlett Solo USB' || "$currentOutput" = 'MacBook Air Lautsprecher' ]; then
SwitchAudioSource -s 'Externe Kopfhörer'
else
if [ "$deviceExists" != '' ]; then
SwitchAudioSource -s 'Scarlett Solo USB'
else
SwitchAudioSource -s 'MacBook Air Lautsprecher'
fi
Thanks for a great thread. I've got switching working using switchAudioSource. I'd like to take another step and add an additional feature that'll make this even more valuable, at least for me.
Hoping I can change the sound icon in the menubar to indicate which output is selected. I'm toggling between 'USB Audio Device' & 'SDAC ', so maybe in my case I could use 'USB' & 'SDAC'.
This action could happen within the existing if/then/otherwise structure, wondering if anyone has an idea how to implement?
I don’t know if you ever found a workaround for your case use, but Rogue Amoeba’s SoundSource allows to to see individual menu bar icons for all apps that are currently outputting audio, as well as see what the current output device is. This would let you see what device is your current output selection.
Yea I was only ever able to script it using System Events and GUI, but it would at least let him see unique icons for his audio settings in the menu bar. Perhaps he could use it in conjunction with his switchAudioSource setup.
For those that don’t have Rogue Amoeba’s SoundSource but want to switch to any output listed in the Sound Preferences (including AirPlay sound outputs, e.g., HomePod, Apple TV), consider trying this macro: Set Sound Output