I'm building an Alfred workflow with switchaudio-osx so I can change system audio input/output to specific source(s). Pretty new to shell script writing, and I conjured up this:
#!/bin/bash
#Command path
cmd="/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource"
#Available sources
source1="Built-in Output"
source2="USB Audio CODEC "
source3="Built-in Microphone"
#Switch output source to
systemOut="$cmd -t output -s $source1"
usbOut="$cmd -t output -s $source2"
#Switch system sound source to
systemSys="$cmd -t system -s $source1"
usbSys="$cmd -t system -s $source2"
#Switch input source to
usbIn="$cmd -t input -s $source2"
systemIn="$cmd -t input -s $source3"
case "{query}" in
"systemOut")
$systemOut
$systemSys
;;
"usbOut")
$usbOut
$usbSys
;;
"systemIn")
$systemIn
;;
"usbIn")
$usbIn
;;
*)
echo "No source detected"
;;
esac
After running, the script returns an error message of:
Could not find an audio device named "Built-in" of type output. Nothing was changed.
Could not find an audio device named "Built-in" of type system. Nothing was changed.
So somehow $source1
was not read in full. From search I learned about IFS setting and using {}
with string variables so I tried those. However error messages persist.
When I change $source1="Built-in Output"
to "'Built-in Output'"
, error message returns:
Could not find an audio device named "'Built-in" of type output. Nothing was changed.
When I change case statement command $systemOut
to ${systemOut}
or "$systemOut"
, both return with error message:
/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -t output -s 'Built-in Output': No such file or directory
/bin/bash: line 25: /usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -t system -s 'Built-in Output': No such file or directory
What does this error message mean? It looks like String variables that contains commands in case statement were not executed the right way.
I also tried using $()
and ``
to contain the string variable that has my command, but getting the same type of error messages.
What's the syntax for embedding shell commands in case statement, or in bash script in general? Is it not a string whose value are the commands? Super newbie question. Sorry.