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
Asked
Active
Viewed 6,231 times
5

kilojoules
- 337
-
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 Answers
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.
- Open Automator.
- File > New > Service, with setting as shown in the image.
- Add a Run AppleScript action an replace the default code with the code Above.
- Save the Service as, e.g.: Toggle Sound Device
- Assign it a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services

user3439894
- 58,676
-
-
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.
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Aug 10 '22 at 17:16
-
Welcome to Ask Different! This is really a comment, not an answer. With a bit more rep, you will be able to post comments. – agarza Aug 10 '22 at 21:41