I was hoping there'd be an easy way to have Keyboard Maestro run an Apple Script to select an audio source from the menu bar. I'd love to be able to assign a keyboard shortcut or other trigger to select "Henge Audio" from my volume menu bar. I've tried to do this myself but to no avail.
The best way I've found to automate switching audio sources like this is with a command line utility called switchaudio-osx. You can install it with homebrew fairly easily, and once you do, you can have KM run a shell script to select "Henge Audio" like this:
Thanks for the suggestion. I was kind of hoping to not have to install anything else to get this to work. I think your option sounds like a very viable source for sure. I have been using an AppleScript that went into system preferences to change the sound output but have found it to be somewhat unreliable. I thought a simple script to select it from the menu bar might be easier. But I don't pretend to understand a whole lot when it comes too scripting.
I can understand your reluctance not to install a new piece of software just for this one task, but if this is something you care enough about to automate, as it clearly is, I really do recommend it. I'm no expert scripter myself, but I can tell you that, unfortunately, there's nothing simple about using AppleScript to accomplish this task via the menu bar. That requires GUI scripting, which is both harder to write and more prone to failing. In contrast, once this utility is installed, the shell script needed to use it is both very simple (as far as shell scripts go, admittedly) and much more reliable.
Selecting status menu items is difficult primarily because there is no easy way of specifying them - they don't have names so its hard to identify them.
You can use a Click at Found Image, although even that can be difficult as they often animate or change icons based on various states. If the icon does not animate or change state (or has a known state when you want to select from it), then this can be a good solution, followed by Insert Text by Typing "Menu Name%Return%"
To click relative to an image, you use the Click Mouse action, configure it to be relative to a found image, and take a screenshot of the desired area of the screen using Command-Control-Shift-4, and paste it into the image well on the action. The image has to be unique (which includes not being visible in the action if the image is small enough not to be shrunk in the image well) otherwise Keyboard Maestro will not know where to click. The Display option in the action will allow you to see where Keyboard Maestro is matching.
The only reason I thought this would be doable is because I came across and AppleScript to connect my AirPods without going into the bluetooth menu via Alfred.
It works flawlessly & just assumed something like this would work for the sound menu in Keyboard Maestro?
The script is below if that's helpful. If not, I will consider adding switchaudio-osx utility.
Thanks again!
on alfred_script(q)
activate application "SystemUIServer"
tell application "System Events"
tell process "SystemUIServer"
-- Working CONNECT Script. Goes through the following:
-- Clicks on Bluetooth Menu (OSX Top Menu Bar)
-- => Clicks on Aaron’s AirPods Item
-- => Clicks on Connect Item
set btMenu to (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
tell btMenu
click
tell (menu item "Aaron’s AirPods" of menu 1)
click
if exists menu item "Connect" of menu 1 then
click menu item "Connect" of menu 1
return "Connecting..."
else if exists menu item "Disconnect" of menu 1 then
click menu item "Disconnect" of menu 1
return "Disconnecting..."
else
click btMenu -- Close main BT drop down if Connect wasn't present
return "Connect menu was not found, are you already connected?"
end if
end tell
end tell
end tell
end tell
end alfred_script
You are correct that some menus on the Apple Menu Bar / Status Menu can be scripted. Some can be a challenge that have dynamic menus, that are NOT populated until the main menu is clicked on.
Chris @ccstone is a master of UI scripting. If it can be done, he can do it. Perhaps he will jump in here soon to offer a solution or comment.
Great suggestion, but I can't seem to get it to work. Can you help?
I've got switchaudiosource-osx installed, and issuing -a gives me the list of devices. When I try to have KM issue -s device_name (with spaces, with underscores, whatever) the macro fails. What am I doing wrong?
It's hard to say from here, I'm afraid. At a glance, it doesn't look like you're doing anything wrong. Have you tested the same script in Terminal and seen if it works there? Does it fail when trying to switch to any audio device, or just USB Audio?
You can give this AppleScript a try. It works fine in High Sierra, but may need adapting to Mojave if it has changed System Preferences’ UI tree:
use Prefs : application "System Preferences"
use sys : application "System Events"
property pane : a reference to pane id "com.apple.preference.sound"
property anchor : a reference to anchor "output" of my pane
property process : a reference to application process "System Preferences"
property window : a reference to window "Sound" of my process
property tab group : a reference to tab group 1 of my window
property scroll area : a reference to scroll area 1 of my tab group
property table : a reference to table 1 of my scroll area
property row : a reference to row 1 in my table
to select audioOutput as text
tell (a reference to (my row where the value ¬
of text field 1 = audioOutput)) to if ¬
it exists then return missing value ≠ ¬
(select it)
false
end select
on zzz(x, |ξ| as boolean)
local x, |ξ|
repeat 20 times -- 20 x 0.2s = 4s max. wait
if |ξ| = (x exists) then return true
delay 0.2
end repeat
false
end zzz
on quit e as boolean
tell Prefs to quit
return e
end quit
quit 1
if zzz(my process, no) = false then return quit 0
reveal my anchor
if zzz(my row, yes) = false then return quit 0
select "Apple TV Bedroom" -- name of audio output device to switch to
quit result
Trying your macro returned the following with any device name.
"System Events got an error: Can’t get menu bar item 1 of menu bar 1 of application process "SystemUIServer" whose description = "system sound volume". Invalid index.