r/applescript • u/scaredbyninjas • Mar 05 '26
Help creating a script to set sound output
Newbie here. I'm trying to set up a keyboard shortcut to set my audio output. Here is what I have currently:
set deviceName to "MacBook Pro Speakers"
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
tell application "System Events"
tell process "System Preferences"
tell table 1 of scroll area 1 of tab group 1 of window 1
select (row 1 whose value of text field 1 is deviceName)
end tell
end tell
end tell
quit application "System Preferences"When I run it, I get a syntax error: System Settings got an error: Can’t get pane id "com.apple.preference.sound".
Tried searching this sub, but am feeling lost. Any help would be appreciated. I'm on Tahoe 26.3.
3
Upvotes
6
u/yosbeda Mar 05 '26 edited Mar 05 '26
The real issue here is that Apple renamed System Preferences to System Settings back in Ventura, and the old pane IDs like
com.apple.preference.sounddon't work the same way anymore. So that whole approach of poking around in System Settings via UI scripting is pretty fragile on newer macOS, Tahoe included.Honestly the cleanest fix I've found is to skip the System Settings approach entirely and use a small CLI tool called switchaudio-osx. You install it with
brew install switchaudio-osx, and then your AppleScript becomes basically one line:No opening System Settings, no fragile UI element traversal, nothing. Just a direct shell command. The path might differ depending on Intel vs Apple Silicon (on Intel it's usually
/usr/local/bin/SwitchAudioSourceinstead), so probably worth runningwhich SwitchAudioSourcein Terminal first to confirm. And to see what your devices are actually named (the name needs to match exactly), you can runSwitchAudioSource -a -t outputin Terminal.For the keyboard shortcut part, I think the easiest approach is to wrap that
do shell scriptline in an Automator Quick Action set to receive "no input", then assign it a shortcut through System Settings > Keyboard > Keyboard Shortcuts > Services. There are other ways to do it too but that's probably the most native option without installing anything extra beyond the audio tool itself.I'm not sure if there's a clean pure-AppleScript way to do this on Tahoe without any third-party tools, maybe there is, but every native approach I've seen relies on UI scripting that tends to break with OS updates. The switchaudio-osx route has been way more reliable in my experience. Mentioned it briefly in another thread a few days ago in a different context, but same idea applies here.
4
u/porpoisepurpose42 Mar 05 '26
You need to find the correct ID for the pane you're trying to modify. Running
tell application "System Settings"end tellwill print the current IDs of all the System Settings panes.
1
u/Limitedheadroom Mar 05 '26
If you download Xcode it includes accessibility inspector which will help you find the right path to the elements you want to control
1
u/JollyRoger8X Mar 06 '26
UI scripting is always going to be problematic. Not only does it require the user to refrain from using the interface while it's running, but if UI elements get moved or renamed, your script breaks.
Instead, I recommend you do it the way I've done it for the past decade or two:
I use an open source command-line tool called switchaudio-osx to do the audio switching programmatically without UI scripting. I run this command-line tool from my AppleScript scripts. I have a set of AppleScripts: one for each audio input/output I use.
Here's a really basic one called "Switch to Line Out":
That one is very simple and assumes the device_name you set exists.
Here's a better one called "Switch to Display Audio" that I use more often to switch the audio input and output to the display (where it automatically finds the input/output device with "Display" in the name):
I run these scripts via LaunchBar, but you could run them any number of ways that fit your workflow.