MACRO: Set Sound Output, v2.2

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

PURPOSE

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.

SETTINGS

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 ( ).

MULTIPLE INSTANCES OF SET SOUND OUTPUT

This macro can be copied to create macros that set the Sound Output to other devices. For example:

  1. On my Mac mini (Mojave), the default is Internal Speakers. This macro can renamed to Set Sound Output - Internal Speakers.
  2. On my MacBook Pro (Big Sur), the default is MacBook Pro Speakers. This macro can be copied and renamed to Set Sound Output - MacBook Pro Speakers. The local_SoundOutputName variable will need to be adjusted.
  3. I have an HomePod in our home office. This macro can be copied and renamed to Set Sound Output - HomePod - Office. The local_SoundOutputName variable will need to be adjusted.

LOCALIZATION

By default, the AppleScript User Interface Scripting is set for English. If another Language is set under System Preferences Language & Region, these values will need to be changed within the first Execute AppleScript action.

LIMITATIONS

This macro requires Keyboard Maestro v8.0.3+.

TESTED WITH

• Keyboard Maestro 10.0.2
• Monterey, v12.3.1/MacBookPro16,1
• Mojave 10.14.16/Macmini6,2
• High Sierra 10.13.6/iMac11,1445

VERSION HISTORY

1.0 - Initial version
1.1 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.
2.0 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.
strong text
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.


DOWNLOAD Macro File:
Set Sound Output.kmmacros (34 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.

Macro-image

6 Likes

I've updated the original post with version 1.1. This 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 succcessfully set.

1 Like

This is fantastic and so very useful, thank you very much

1 Like

@DDuser, you're welcome. Thanks for the message.

I would love to use this macro but I cannot get it to work on my Mac mini with macOS Monterey
Versie 12.3 beta (21E5206e) installed. Could the fact that I'm on a beta be the problem?

Hi @mgmheck, welcome to the forum.

I normally us this on my Mojave mac (also a mini) to a HomePod, but this weekend (traveling now) I’ll try on Monterey and see if I can duplicate your issue. Do you mind sharing more information? What type of device are you attempting to use?

Hi @mgmheck. I downloaded macOS 12.3.1 this evening and tested the macro again. I was able to successfully switch to the Build-in and all AirPlay devices. In the screenshot below, you can see I was able to set the Sound Output to my HomePod - Office.


If you give me more information regarding the issue you are observing, I'll do my best to help you troubleshoot.

Apologies, I could not get back sooner to you.

I'll provide more information:

  • KM Version 10.0.2

I've uploaded a screenshot of both my sound output (it is in Dutch) and a screenshot of the macro adjusted to the name of my sound output.
The error message I get is:
ERROR: The following was not found in the Sound Output list: Externe koptelefoon

I notice that the macro opens the system preferences window. I don't know if that is the correct way to go?
2022-04-03_CleanShot

1 Like

For any Alfred users among us, this is quite nice and sets the sound output in the background.

http://ricardosampayo.com/en/Audio-Switch-Alfred-WorkFlows/

1 Like

Hi @mgmheck. No problem regarding the delay.

Please go back to the first post in this thread and download v2.0. I've added localization for English, Nederlands, and Deutsch.

Once you download the v2.0, go to the first Execute AppleScript action and uncomment (remove the -- prefixes) for the two Nederlands values.


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

  • Checks the running state of System Preferences before running the AppleScript that changes the Sound Output.

  • In the AppleScript: i) removed unnecessary System Preferences activate, ii) added localization locWindowSound and locButtonOutput.

Great _jims!! It works!

1 Like

Thank you! This works perfectly! But I can't modify it so that I can toggle between speakers and Airpods that I often use. Could you please help?

1 Like

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

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

Hi, @fom. You are welcome.

I just updated the macro to Version 2.1. Please download it and try again. This version should be somewhat more robust.

If you are still having a problem, please capture a screenshot of the dialog that appears and share it in this thread. For example, with the below screenshot I forced the macro to fail by setting local_SoundOutputName to Internal Speakers1.

2022-05-07 15.39.06