MACRO: Set Sound Output, v7.1

Why do you need to open the system settings sound pane in a loop? When I run it once, it just selects the sound pane.

1 Like

@ikenassi, good question!

This is a general practice I use because it makes such steps more robust and the wait can be minimized. (I take a similar approach when a sequence of actions that depend on an application being in the foreground.)

Bottomline: it never hurts and sometimes helps.

Good idea. Better than dealing with variable delays.

1 Like

By the way, here’s a little script to convert the OS version from (potentially) i.j.k to i.j so you can use numeric comparisons.

Set OS Version.kmmacros (3.96 KB)

1 Like

Thanks, @ikenassi. Yes, I've seen a few techniques on the forum. In Set Sound Output, I use:

Keyboard Maestro Export

Please ignore my poorly named local variable. :face_with_open_eyes_and_hand_over_mouth:

1 Like

For a macro that sets the Sound Input as well as the Sound Output, see Set input and output sound devices in MacOS Ventura by @ikenassi.

Although the one I wrote works just fine, I've cut over to SwitchAudioSource.

As was said earlier, it's free, fast, and works well. Install with the Homebrew package manager by:


brew install switchaudio-osx


Shell script examples:


echo "All Sound Devices:"
SwitchAudioSource -a

echo "Input:"
SwitchAudioSource -t input -c

echo "Output:"
SwitchAudioSource -t output -c
1 Like

Hi @ikenassi. Does it now work for HomePods and other AirPlay devices? When I tested it, it did not. That was my entire motivation for creating Set Sound Output.

1 Like

I have neither, so not tested.

1 Like

Registered to say many thanks for this excellent Macro. It works extremely well on Ventura 13.0.1. Thank you @_jims

2 Likes

@OneEyedJack, you’re welcome. Glad you are finding it useful.

1 Like

Unfortunately SwitchAudioSource doesn't work for me, as switching the output randomly switches the sound effects source if it's set to "Selected sound output device", and I need to keep it set to that.

This macro looks very useful, but it's annoying to have to wait for System Preferences to open and change the sound output. (If it was already open, the macro doesn't return to the pane it was on, either…)

It's also extremely visually distracting to have something that size pop up — ideally, I'd be able to change the sound output without anything coming up on screen, but the Sound menu would at least be acceptable as it's small.

Is there a way to use Select or Show a Menu Item to control Sound icon in the menu bar? The Menu dropdown doesn't seem to offer menu bar icons, only apps. I tried this

image

but got "Select Menu Item failed to find menu item Sound≥Dell AC511 USB SoundBar" :frowning:

I have Bartender 4, so I have managed to get the menu open with this

image

…but how do I tell AppleScript or KM to click an item in the currently open menu?

(Using Click at Found Image to open the menu seems awkward, as there are four possible icons depending on the current volume level…)

As you've found, menu bar icons aren't proper menus so can't be accessed that way.

OS version and a screen shot of the open Sound menu will help a lot -- but the AppleScripts in the GitHub link you posted should get you most of the way.

Unfortunately, I just get "System Events got an error: Can’t get menu bar item 1 of menu bar 1 of process "SystemUIServer" whose description contains "volume". Invalid index."

I don't know enough AppleScript to be able to work around this. Trying to find AppleScript documentation using search engines just returns a load of articles that never seem to quite tell you what you need…

Things change. Without an OS version we won't know how to target the Sound menu bar icon.

For instance -- in Ventura (and maybe before) "Sound" was moved into Control Center's purview. So (assuming "Sound" is in the menu bar!) you should be able to simulated-click it with

tell application "System Events"
	tell process "ControlCenter"
		set theMenu to item 1 of (get every menu bar item of menu bar 1 whose accessibility description is "Sound")
		click theMenu
	end tell
end tell

And things are particular to devices. Without a screen shot of the choices you have it'll be difficult to target the right one.

Post the info required and (hopefully!) something can be sorted.

Still on Mojave, unfortunately — but I was planning to go ahead with the Ventura upgrade at the weekend, so I'll try your suggestion then.

Thanks!

Cooked the graphics on my Mojave iMac at the weekend -- but VNC to the rescue!

tell application "System Events"
	tell process "SystemUIServer"
		set theMenu to item 1 of (get every menu bar item of menu bar 1 whose description starts with "volume")
		click theMenu
	end tell
end tell

Odd. get every menu bar item of menu bar 1 whose description starts with "volume" comes up with a list of 0 items on my machine…

If I leave out the whose description starts with "volume" I get a 1 item list which just contains the Time Machine menu item.

I wonder if Bartender 4 is interfering with things?

Maybe -- and remember that the "Sound" menu bar icon needs to be visible for the AppleScript to be able to target it.

You could, of course, use the AS that targets Bartender and opens the menu to show the options and then select the appropriate one via System Events. But -- again -- without a screen shot of the open menu we can't be much more help.

I've updated the OP with Version 4.1 of Set Sound Output:

  • If the System Preferences (or System Settings) is already open to a pane, the macro returns to that pane.

  • Updated the TESTED WITH information.

1 Like