Switch Audio Source

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.

But installing https://github.com/deweller/switchaudio-osx via homebrew and then using this shell script (modified from @ccstone to use SwitchAudioSource and my own Sources) as a KM action worked:

export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;

currentOutput=$(SwitchAudioSource -c); echo $currentOutput

if [ "$currentOutput" = 'MacBook Air Lautsprecher' ]; then

	SwitchAudioSource -s 'Externe Kopfhörer'

else

	SwitchAudioSource -s 'MacBook Air Lautsprecher'

fi

BTW – I've also tried the following script to switch between all available outputs, but that also comes with an annoying message that I have to close.

export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;

SwitchAudioSource -n

Hey Johannes,

What version of macOS are you using?

-Chris

Big Sur 11.2.2
on an M1

What message?

Can you provide a screenshot?

-Chris

That's what it looks like.

Also:

How might I add an if else statement that checks whether the output I want to switch to is available / in the -c list?

You have the Execute a Shell Script action set to display the result in a window.

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.
  • Then use Keyboard Maestro's If Then Else or Switch or Case action to branch as desired.

I prefer to keep the whole bit in one Execute a Shell Script action for simplicity and speed, but then I know how to use Bash for such things.

-Chris

Thanks for the help Chris.

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

Hey Johannes,

Please post the actual results of:

SwitchAudioSource -c

-Chris

➜ ~ 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

It helps when you use the correct syntax:

if [ "$currentOutput" = 'Scarlett Solo USB' ] || [ "$currentOutput" = 'MacBook Air Lautsprecher' ]; then

:sunglasses:

-Chris

Hi all,

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?

TIA,
other chris

Hey Chris,

No can do.

There might be a sound-switching utility that can, but Apple's stock sound menu can't.

-Chris

Hey Chris,

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.

SoundSource 5 by Rogue Amoeba

They have a demo version so you can see if you like it or not.

I don't see anything about it being scriptable...

-ccs

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.

1 Like

In case it helps, Rogue Amoeba have just added Shortcuts support to SoundSource. My first attempt at automating this is here...

1 Like

I was thrilled to see this new feature when they rolled it out this week and immediately started using it.

Your macros look very well designed, I’ll have to check them out more when I’m back at my computer!

1 Like

Me too! :nerd_face: It's taken me a few days to have the time to write it up.

And that's kind of you to say. As ever, I'd really welcome your expert feedback, Chris - it's always much appreciated :slight_smile:

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

2 Likes