Using Apple Script to Manage Sound Output Selection
I am brand new here and I have an additional clarification or need additional help for an issue from this thread - Applescript: "can't get tab group 1 of window" (El Capitan)
In my preference pane, in addition to Headphones and Digital out, I also have 2 apple monitors and would like the option of selecting one of them as out put (namely the first one in the list).
Basically I need help with adjust his solution to be able to select the third row or first monitor as sound output. Or a way to accomplish this without knowing how to edit AppleScript for a different setup.
With a little experimentation on Arthur Hammer's script I discovered that the lines set deviceselected to "xyz"
appear to be just visual feedback whilst in Script Editor & don't seem to be part of the actual functionality.
The actual switching appears to be just between 2 chosen lines in the Control Panel; so, if you just need to switch between any 2 outputs, then you can just use the row numbers.
In this example I've lifted them out to be variables, so you don't need to delve in the script to change the required lines…
As you have more than 2 outputs, the first time you switch it will not necessarily go from/to the correct device, but will after that.
For example, if you wanted to switch between outputs 3 & 4, yet 2 was currently selected, the first change would be from "not 3" as opposed to "is 3" [hope you understand that convoluted explanation]
(*
Applescript to toggle between two sound outputs by Line number, ¬
as they appear in the Sound Control Panel. Based on code by ¬
Arthur Hammer http://apple.stackexchange.com/a/209434/85275
*)
set outputA to 3 --change this to the actual 'line number' of your first desired output
set outputB to 4 --change this to the actual 'line number' of your second desired output
--the rest of the script will use these vales as a switch
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
end tell
tell application "System Events"
tell application process "System Preferences"
repeat until exists tab group 1 of window "Sound"
end repeat
tell tab group 1 of window "Sound"
click radio button "Output"
if (selected of row outputA of table 1 of scroll area 1) then
set selected of row outputB of table 1 of scroll area 1 to true
else
set selected of row outputA of table 1 of scroll area 1 to true
end if
end tell
end tell
end tell
--tell application "System Preferences" to quit
--remove the comment '--' tag above to make the control panel quit afterwards, leave for testing.
Maybe a little pictorial clarification - using the above script with outputA = 3 & outputB = 4, I can now switch between Digital Out & TonePort UX2.
One more note - the line numbers do not have to be consecutive, I could just as easily switch between Internal Speakers & TonePort by using lines 1 & 4.
@Tetsujin - thanks for this solution. Works perfectly. I'm using it for switching between my built in audio (for when I'm coding for work) and my Universal Audio Apollo card (for when I'm doing music production). Since the UAudio device also provides a superior input mic option, I added the obvious input switch as well after your "Output" section:
click radio button "Input"
if (selected of row outputA of table 1 of scroll area 1) then
set selected of row outputB of table 1 of scroll area 1 to true
else
set selected of row outputA of table 1 of scroll area 1 to true
end if
In Mojave, I usually manually switch the output device from the Volume Control in the menu, so I automate the same thing using the AppleScript below which does the toggling based on what is checked/unchecked.
If you use this w/ some launchers, then this approach doesn't need permissions to the Preferences.
ignoring application responses
tell application "System Events" to tell process "SystemUIServer"
click menu bar item 5 of menu bar 1 -- May need to change the index as needed
end tell
end ignoring
-- Optional, but just to avoid the 5-6sec delay
delay 0.25
do shell script "killall System\\ Events"
tell application "System Events" to tell process "SystemUIServer"
tell menu bar item 5 of menu bar 1
set device1 to menu item "Name of Device 1" of menu 1
set device2 to menu item "Name of Device 2" of menu 1
if (value of attribute "AXMenuItemMarkChar" of device1) as string is "✓" then
click device2
else
click device1
end if
end tell
end tell
I'm not able to guess as to a general AppleScript, but I do like and use Automator to record the steps needed.
Open that tool and make a new workflow. Then press the record button. You can then record the sequence used to open System Preferences, select the pane you wish, select the audio you want and then end recording.
You can usually look over the actions and trim them or add delays if needed using Automator and you won't need to know how to program the rows and items in Applescript.