I found a post, Program to Switch Audio Output, as I was searching the Keyboard Maestro Forum for a method to quickly switch the macOS Audio Output. When I tried using switchaudio-osx as mentioned in the post, it didn't function for my HomePod or other AirPlay devices. Thus I created a macro that completes this task using AppleScript that automates the macOS User Interface.
Set Sound Output
This macro sets the Macintosh Sound Output to a specified name. This can include Apple HomePods and other AirPlay devices. Note that this shortcut uses AppleScript that automates the User Interface, specifically the System Preferences. See Automating the User Interface (https://apple.co/3lj3PQB) for more information.
By default, this macro will set the Sound Output to the Internal Speakers. This can be changed using the local_SoundOutputName variable. Note that this value is case sensitive and punctuation marks must exactly match the entries in the System Preferences > Sound > Output. For example, Jim’s Airpods Pro includes a smart apostrophe ( ’ ).
Alterntively, this macro can be executed by another macro. If a sound output parameter is passed, it will be used rather than the hardcoded value in local_SoundOutputName. For an example, refer to the Toggle Sound Output macro.
By default, the AppleScript User Interface Scripting is set for English. If another Language is set under System Preferences Language & Region, the variable local_Sound needs to be set to Sound|Geluid|Ton (English, Nederlands, or Deutsch). Other launguages could be easily added.
This macro requires Keyboard Maestro v8.0.3+.
• Keyboard Maestro v10.2
• Ventura Version 13.1 Beta (22C5033e)/MacBookPro16,1
• Mojave 10.14.16/Macmini6,2
• High Sierra 10.13.6/iMac11,1445
1.0 - Initial version
a) Succeeds even if it takes a few seconds for the System Preferences > Sound > Output list to populate with the available options.
b) Displays an error dialog if the specified Sound Output is not successfully set.
a) Checks the running state of System Preferences before running the AppleScript that changes the Sound Output.
b) In the AppleScript: i) removed unnecessary System Preferences activate, ii) added localization locWindowSound and locButtonOutput.
2.1 - The embedded AppleScript now delays until a sound output radio button exists before it attempts to select it. This is done using: repeat until exists radio button locButtonOutput and two delay 0.1
2.2 - Fixed typographical errors in this comment. No functional changes to the macro.
a) Added a check of %TriggerValue%. If it exists, it is used rather than the value hardcoded into local_SoundOutputName.
b) Added a check of macOS version. Unfortunately, with macOS Ventura (through 13.0 Beta 11—22A5373b), this macro will not function as expected. Hopefully this is a temporary issue during the beta period, but it could be an ongoing issue since in Ventura the macOS System Preferences have been moved the the System Settings.
a) Using the macOS version check added in v3.0, now conditionally runs a new AppleScript UI script compatible with Ventura. This was contributed by @ikenassi.
b) Based on a recommendation from @peternlewis, now opens the System Preference (pre-Ventura) or the System Settings (Ventura+) using the Open a System Preference Pane action rather than AppleScript.
c) When an error occurs, a message is displayed using Prompt For User Input rather than Display Text. With this change, the System Preferences (or System Settings) remain open when the error message is displayed.
d) Improved and simplified the localization in the AppleScript UI scripts: i) moved definition from AppleScript to the macro body, and ii) eliminated dependency on the word Output|Uitvoer|Ausgabe.
DOWNLOAD Macro File:
Set Sound Output.kmmacros (52 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.