applescript script stopped working after 12.0.1 (Monterey) upgrade

I have simple script to turn down the brightness of my MacBook Pro via an AppleScript. This worked fine in BigSur, but has stopped working after I upgraded to Monterey 12.0.1.

The script is as follows:

tell application "System Preferences"
  reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
  set value of value indicator 1 of slider 1 of tab group 1 to 0.0
end tell

..error is as follows in script editor:

error "System Events got an error: Can’t set window \"Built-in Retina Display\" of process \"System Preferences\" to 0.0." number -10006 from window "Built-in Retina Display" of process "System Preferences"

Any suggestions on what to modify to get it working again?

  • Can anybody help?

  • You will need to take a look at the various items to see if the UI hierarchy is the same - for starters you might try using an index instead of a title for the widow.

  • Thanks for the response. How do I go about any of the above? I'm not particularly technical :-)

Replies

A similar script of mine has just broken with Monterey as well:

--Check if System Preferences already open?

--Don't want to quit it on exit if it's already running

set appName to "System Preferences"

if application appName is running then

	set appRunning to true

else

	set appRunning to false

end if



--Toggle Automatic Display Brightness

tell application "System Preferences"

	set current pane to pane id "com.apple.preference.displays"

	tell application "System Events"

		tell process "System Preferences"

			tell checkbox "Automatically adjust brightness" of tab group of window 1

				click

				--Display a matching notification

				if value = {0} then

					display notification "Automatic Brightness Disabled: Manual Override" with title "Manual Display Brightness" 
				end if

				if value = {1} then

					display notification "Automatic Brightness Enabled: Sensor Engaged" with title "Automatic Display Brightness" 
				end if

			end tell

		end tell

	end tell

end tell



--Quit System Preferences if this script opened it up

if appRunning = false then

	tell application "System Preferences" to quit

end if

This one executes with no errors reported, but does not click the button, and does not fire either notification.

  • That just looks like none of the comparisons match. Note that the checkbox value wouldn’t be a list.

  • After much fiddling with Accessibility Inspector, this script finally works:

    --Don't want to quit it on exit if it's already running set appName to "System Preferences" if application appName is running then set appRunning to true else set appRunning to false end if --Toggle Automatic Display Brightness tell application "System Preferences" set current pane to pane id "com.apple.preference.displays" --Wait a bit for the pane to cold load if appRunning = false then delay 2 end if tell application "System Events" tell process "System Preferences" tell checkbox "Automatically adjust brightness" of group of window 1 click --Display a matching notification if value = {0} then display notification "Automatic Brightness Disabled: Manual Override" with title "Manual Display Brightness" end if if value = {1} then display notification "Automatic Brightness Enabled: Sensor Engaged" with title "Automatic Display Brightness" end if end tell end tell end tell end tell --Quit System Preferences if this script opened it up if appRunning = false then delay 1 tell application "System Preferences" to quit end if```
  • Sorry about the formatting. No amount of tabs, spaces, backticks etc. are doing any good!

I am having a similar problem. I ended up using "UI Browser" to determine the path to element. Here is what I have so far. As proof it is navigating properly in that window, I have included the line to click on the radio button "Scaled".

activate application "System Preferences"
tell application "System Events"
       tell process "System Preferences"
       click button "Displays" of scroll area 1 of window "System Preferences"
       delay 2

# click radio button 2 of radio group 1 of group 1 of window 1

		set value of value indicator 1 of slider 1 of group 1 of window 1 to 0.25
      end tell
end tell