MACRO: Set Sound Output, v4.0

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

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.

LOCALIZATION

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.

LIMITATIONS

This macro requires Keyboard Maestro v8.0.3+.

TESTED WITH

• 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

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.

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.

3.0
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.

4.0
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.

Macro-image

8 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

Hey,

Cool macro!

Have you thought about creating a keystroke that would toggle between two outputs? I am constantly changing between my external speakers and my headphones and want to be able to hit a keystroke to switch between them.

I found an Alfred workflow that does that really well, BUT for some reason now, my outputs change order in my Sound Preferences all the time.

I have two monitors with speakers that show up in there even though I never use them. I wish I could just remove them!

The Alfred workflow switches it based on the order in that pref panel rather than by name (like yours does).

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

  • Added a check of %TriggerValue%. If it exists, it is used rather than the value hardcoded into local_SoundOutputName.

  • 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.

1 Like

Hi @BCBoarder. Thanks for the feedback. I'm glad that you are finding the macro useful.

Here's a macro that you can use to toggle between two values. Note that you will also need the latest version (v3.0) of Set Sound Output.


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

1 Like

Used this all the time, but can confirm that with Ventura 13.0 released version, it still does not function under Ventura. Keep up the great work!

1 Like

Hi, @spaceavian. Welcome to the Keyboard Maestro forum. Thanks for your report.

I'll add that it is still broken with macOS beta 13.1 (22C5033e).

Googling around I see that others have discovered this problem.

Interestingly, this AppleScript on a pre-Ventura system:

tell application "System Preferences"
  reveal pane id "com.apple.preference.sound"
end tell

gets converted (on Ventura in the Script Editor–when compiled) to:

tell application "System Settings"
  reveal pane id "com.apple.preference.sound"
end tell

(The conversion also occurs in a Keyboard Maestro Execute an AppleScript action.)

Although Preferences is automatically changed to Settings, the tell block fails on Ventura.