5

I am using external monitors. Because of this, whenever my mac goes to sleep, it automatically redirects output to the monitor. This is annoying to fix, as I have to move my mouse and click to fix it every time. Is there a way to set a hotkey to toggle the sound output for me? something like command+shift+s

enter image description here

  • See my answer to http://apple.stackexchange.com/questions/217148/using-apple-script-to-manage-sound-output-selection/218223#218223 - you could use this in Automator, saved as a Service. – Tetsujin Oct 20 '16 at 19:46
  • Thanks @Tetsujin. I am having trouble implementing that for my problem. I am new to applescript. – kilojoules Oct 20 '16 at 20:08

2 Answers2

5

Since you only have the two output devices, the following code will toggle between the two.

tell application "System Preferences" to ¬
    reveal anchor "output" of pane id "com.apple.preference.sound"

tell application "System Events"
    tell application process "System Preferences"
        repeat until exists tab group 1 of window "Sound"
            delay 0.1
        end repeat
        tell tab group 1 of window "Sound"
            if (selected of row 2 of table 1 of scroll area 1) then
                set selected of row 1 of table 1 of scroll area 1 to true
            else
                set selected of row 2 of table 1 of scroll area 1 to true
            end if
        end tell
    end tell
end tell

quit application "System Preferences"

I test this as an Automator Service using a Run AppleScript action and then assigned it a hot-key combo and it works on my system having only two sound outputs. The tricky part is going to be finding a hot-key combo that isn't assigned to any or all apps already.

As an example commandshifts is Save As... in Safari, but if the Desktop has focus when pressed it works to trigger the Service.

  1. Open Automator.
  2. File > New > Service, with setting as shown in the image.
  3. Add a Run AppleScript action an replace the default code with the code Above.
  4. Save the Service as, e.g.: Toggle Sound Device
  5. Assign it a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services

Toggle Sound Device

user3439894
  • 58,676
  • @kilojoules, See updated answer. – user3439894 Oct 20 '16 at 20:53
  • 1
    @kilojoules, Thanks for accepting the answer. BTW I've updated the AppleScript code to a better version to do the same thing only now you don't have to see the System Preferences window show and it opens directly to the target tab of the target pane and introduces error handling so the switch can be made when the objects are available. I'd use the updated code over my original code. – user3439894 Apr 02 '20 at 17:44
0

This is a great solution if you only have two audio output options. Unfortunately, my mac is counting my monitors as outputs and placing them as #1 & #2. Nonetheless, thanks for sharing.