The aim: To select "Office" speakers using Applescript.
The OS: Mac Sonoma 14.3.1
The code:
tell application "System Settings"
activate
reveal pane id "com.apple.Sound-Settings.extension"
end tell
tell application "System Events"
tell process "System Preferences"
repeat until exists window "Sound"
delay 0.5
end repeat
tell window "Sound"
repeat until exists tab group 1
delay 0.5
end repeat
tell tab group 1
click radio button "Output"
repeat until exists table 1 of scroll area 1
delay 0.5
end repeat
tell table 1 of scroll area 1
-- Get the position of the target row
set targetRowPosition to position of row 1 where value of static text 1 is "Office"
-- Click on the target row
click targetRowPosition
end tell
end tell
end tell
end tell
end tell
The rather frustrating result: Opens the preferences sound window then keeps running...That's it. Does not select input or output, does not select "Office" from the list of audio sources
The code stops at
tell application "System Settings"
activate
reveal pane id "com.apple.Sound-Settings.extension"
end tell
As I am using an old mac that doesn't have 'system settings' and I don't have any external speakers, I will provide what works on my system to choose an alert sound, from Sound's Sound Effects. You should be able to map it to your setup and the output.
Now you have the result you need to create a more focused script:
If you find that the syntax differs from a more modern OS version, let me know.