MACRO: Set Sound Output, v7.1


PURPOSE

This macro/subroutine, when executed from another macro using the Execute a Macro action, 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 (Monterey or earlier) or System Settings (Ventura+). See Automating the User Interface (Mac Automation Scripting Guide: Automating the User Interface) for more information.

If this subroutine is called using Execute a Subroutine action, additional features are provided that can be useful if the macro is combined with Set Sound Input. See PARAMETERS below.

SUBROUTINE PARAMETERS

Sound Output Name :
Note that any spaces and punctuation marks must exactly match the entries in the System Preferences (or System Settings) > Sound > Output. For example, Jim’s AirPods Pro includes a smart apostrophe ( ).

Save Initial State :
0 (No); 1 (Yes)
This saves, in a persistent Dictionary, the Open|Closed state and, if Open, the Displayed Pane. This value can then be retrieved by another instance of Set Sound Output (SSO) or the sister macro Set Sound Input (SSI).

Exit Action :
0 (Initial State, i.e., the state when SSO started; i.e., Open|Closed, if Open, which Displayed Pane)
1 (Saved State, i.e., the state saved in the Dictionary by SSO or SSI)
2 (Open–to the Sound pane)
3 (Close)

This value specifies the action to be taken with the System Preferences (or System Settings) after the Sound Input is set.

CREATING A CALLING MACRO

If the macro is executed directly (e.g., from the Keyboard Maestro Editor), a dialog will appear that presents an option to create a calling macro. The dialog will prompt for the Sound Output Name. Note that any spaces and punctuation marks must exactly match the entries in the System Preferences (or System Settings) > Sound > Output. For example, Jim’s AirPods Pro includes a smart apostrophe ( ).

If the Call As a Subroutine dialog checkbox is selected, the created macro will have the flexibility to use the SUBROUTINE PARAMETERS described above.

LOCALIZATION

By default, the AppleScript User Interface Scripting is set for English. If another Language is set under System Preferences (or System Settings) Language & Region, the variable local_Pane needs to be set to Sound|Geluid|Ton (English, Nederlands, or Deutsch). Other languages could be easily added.

LIMITATIONS

This macro requires Keyboard Maestro v10.0+.

TESTED WITH

• Keyboard Maestro 11.0.2
• Sonoma 14.2.1 (23C71)/MacBookPro18,2
• Sonoma 14.2.1 (23C71)/VariableMacBookPro16,1
• Mojave 10.14.16/Macmini6,2
• High Sierra 10.13.6/iMac11,1445

VERSION HISTORY

( expand / collapse )

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.

4.1
a) If the System Preferences (or System Settings) is already open to a pane, the macro returns to that pane.
b) Updated the TESTED WITH information.

5.0
a) To set the sound output, this macro now requires that it is executed by another macro using the Execute a Macro action. The Sound Output Name is set using the Execute a Macro With Parameter.
b) If the macro is run directly (e.g., from the Keyboard Maestro Editor), a dialog will appear that provides a option to create a calling macro.

5.1
a) For the created calling macros, no longer includes the Global Macro Trigger. It was a bit annoying to include this trigger, particularly for users that don’t use the Global Macro Trigger for other macros.
b) For the created calling macros, the method used to update the macro icon has been made more robust.

5.2
a) If the Sound pane does not open and properly populate, open the General pane, then open the Sound pane.
b) If AppleScript intially fails, wait 1.0 second and make another attempt.
c) Updated the error message dialog.

6.0
a) Compatible with Set Sound Input, v1.0 or later.
b) Changed to a subroutine with parameters: Sound Output Name, Save Initial State, Exit Action. The macro can still be run by another macro using Execute a Macro specifying the Sound Output Name in the With Parameter field.
• Save Initial State : 0 (No); 1 (Yes); This saves the Running State and the Displayed Pane (if Running) in a Dictionary. This value can then be retrieved by another instance of this macro or the sister macro Set Sound Input.
• Exit Action : 0 (Initial State, i.e., the state when SSO started; i.e., Open|Closed, if Open, which Displayed Pane); 1 (Saved State, i.e., the state saved in the Dictionary by SSO or SSI); 2 (Open–to the Sound pane); 3 (Close). This value specifies the action to be taken with the System Preferences (or System Settings) after the Sound Input is set.
c) If this macro is triggered from the KM Editor, a dialog will appear that provides an option to create a calling macro that will use the Execute a Subroutine action.
d) Sound Output changes are made more quickly and are less likely to fail. For example, if AirPods are specified, but they are in the case when the macro is launched, the macro will continue searching for a few seconds as the AirPods are removed from the case.

7.0
a) Support for macOS 14 (Sonoma): new AppleScript UI script compatible with Sonoma (slightly different than Ventura).
b) For the local__Exit Action 0I 1S 2O 3C = 2 condition, removed the Cancel Just This Macro action as I discovered that this does not function as expected when used within a subroutine. This action might be changed in a future version of Keyboard Maestro (reference).

*7.1
a) In one of the If Then Else actions "Sound" was hardcoded. This has been changed to local_Pane to properly accommodate the macro localization.
b) For some unknown reason, with Sonoma 14.2.1 (23C71), the gui hierarchy can differ on some some macs; the AppleScript within this macro now accommodates both. For more information, see: Why would UI scripting vary on two Macs with the same version of macOS? - AppleScript - Late Night Software Ltd. Thanks to @OneEyedJack for reporting the issue and helping test the revised AppleScript.
c) For all of the AppleScripts, to improve performance, changed fixed delays to 'repeat until exists'.
d) For Ventura and Sonoma AppleScripts, changed 'starts with' to "is' when checking for a matching Sound Output.


If upgrading from an earlier version of Set Sound Output, please read the following before downloading.

Upgrading from versions prior to Version 5.0
  1. Keep your current Set Sound Output macros, but rename them by adding a suffix of "- OLD".

  2. If you previously downloaded and installed the Toggle Sound Output, delete it.

  3. Download and install the latest version of Set Sound Output.

  4. Open one of your "- OLD" macros and copy the local_SoundOutputName to the clipboard.

  5. From the Keyboard Maestro Editor, run the new version of Set Sound Output. You will see a dialog that will prompt you for your Sound Output Name. Paste the value you copied to the clipboard.

  6. Click OK to dismiss the dialog. A simple macro will be created that executes Set Sound Output using the value you pasted. Optionally change the triggers.

  7. Repeat Steps 4 to 6 for all of your "- OLD" macros.

  8. Optionally download the latest version of Toggle Sound Output. Open it and set local_Sound Output Name 1 and local_Sound Output Name 2 as needed. Optionally change the macro triggers.

  9. Once you test the new macros, delete the macros you renamed in Step 1.


Upgrading from Version 5.0 or later

From within the Keyboard Maestro Editor, delete your existing copy of Set Sound Output before you download and install the new version. On the other hand, do not delete your calling macros.


Download: Set Sound Output.kmmacros (178 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 14.2.1 (23C71)
  • Keyboard Maestro v11.0.2
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 (v5.0) of Set Sound Output.


Download: Toggle Sound Output.kmmacros (10.0 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.3.1 (22E772610a)
  • Keyboard Maestro v10.2

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.