Getting an error when I activate System Preferences at the start of the AppleScript

Hello all,

I have an AppleScript that sets the Input, Output and Sound Effects in System Preferences.

It runs fine, if System Preferences is already activated.

tell application "System Preferences" to reveal anchor "Input" of pane id "com.apple.preference.sound"
tell application "System Events" to tell process "System Preferences"
	tell tab group 1 of window "Sound"
		set selected of (row 1 where value of text field 1 is "Yeti Nano") of table 1 of scroll area 1 to true
	end tell
end tell
tell application "System Preferences" to reveal anchor "Output" of pane id "com.apple.preference.sound"
tell application "System Events" to tell process "System Preferences"
	tell tab group 1 of window "Sound"
		set selected of (row 1 where value of text field 1 contains "Max") of table 1 of scroll area 1 to true
	end tell
end tell
tell application "System Preferences" to reveal anchor "Effects" of pane id "com.apple.preference.sound"
tell application "System Events" to tell process "System Preferences"
	tell tab group 1 of window "Sound"
		click pop up button 1
		click menu item "Selected sound output device" of menu 1 of pop up button 1
	end tell
end tell

If I add code to activate System Preferences…

tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.sound"
end tell
tell application "System Preferences" to reveal anchor "Input" of pane id "com.apple.preference.sound"
tell application "System Events" to tell process "System Preferences"
	tell tab group 1 of window "Sound"
		set selected of (row 1 where value of text field 1 is "Yeti Nano") of table 1 of scroll area 1 to true
	end tell
end tell
tell application "System Preferences" to reveal anchor "Output" of pane id "com.apple.preference.sound"
tell application "System Events" to tell process "System Preferences"
	tell tab group 1 of window "Sound"
		set selected of (row 1 where value of text field 1 contains "Max") of table 1 of scroll area 1 to true
	end tell
end tell
tell application "System Preferences" to reveal anchor "Effects" of pane id "com.apple.preference.sound"
tell application "System Events" to tell process "System Preferences"
	tell tab group 1 of window "Sound"
		click pop up button 1
		click menu item "Selected sound output device" of menu 1 of pop up button 1
	end tell
end tell

…I get this error:

System Events got an error: Can’t set window "Sound" of process "System Preferences" to true.

Any ideas on what I mistake(s) I am making?

Hey Jim,

Sometimes AppleScript UI-Scripting has timing issues similar to the ones Keyboard Maestro can have.

Your activate script fails on my Mojave system if the system prefs are not already running.

That’s a pain, and I don’t see an immediately obvious reason.

Script Debugger just locked up on me, and I don’t want to bother with timeouts and such – so here’s my fix:

tell application "System Preferences"
   
   if not running then
      run
      tell application "System Events"
         set bailOut to 20
         repeat until (exists of application process "System Preferences") = true
            set bailOut to bailOut - 1
            if bailOut ≤ 0 then error "Timeout on Run System Preferences!"
         end repeat
      end tell
   end if
   
   activate
   
   reveal anchor "Input" of pane id "com.apple.preference.sound"
   
   set bailOut to 20
   repeat until name of front window = "Sound"
      delay 0.1
      set bailOut to bailOut - 1
      if bailOut ≤ 0 then error "Timeout on Reveal Anchor Input of Sound Preference!"
   end repeat
   
end tell

-Chris

1 Like

Your fix works well. Thank you @ccstone !

1 Like