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 :-)

The various UI items can have names, but they always have indexes (their position in the list), for example the front window would be window 1. Apple includes an Accessibility Inspector application in Xcode, or you can manually spelunk the hierarchy by using System Events to get UI elements. GUI scripting is not for the faint of heart.

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.

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

On macOS Monterey, I'm using the following script to set my MacBook Pro's built-in display brightness (in a multiple display set-up).

set myBrightness to 0.71

-- # Start with System Preferences closed

if running of application "System Preferences" then
	try
		tell application "System Preferences" to quit
	on error
		do shell script "killall 'System Preferences'"
	end try
end if
repeat while running of application "System Preferences" is true
	delay 0.2
end repeat

-- # Open System Preferences to the target pane

tell application "System Preferences"
	reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
	activate
	repeat while name of window 1 is not "Displays"
		delay 0.2
	end repeat
end tell

-- # Change the Brightness

tell application "System Events"
	try
		tell process "System Preferences"
			-- Open the Display Settings sheet
			click button "Display Settings…" of window 1
			repeat until sheet 1 of window 1 exists
				delay 0.2
			end repeat
			-- Select the built-in display
			select row 1 of outline 1 of scroll area 1 of sheet 1 of window "Displays"
			repeat until slider 1 of sheet 1 of window 1 exists
				delay 0.2
			end repeat
			-- Set the built-in display brightness
			set value of slider 1 of sheet 1 of window 1 to myBrightness
			repeat until value of slider 1 of sheet 1 of window 1 > (myBrightness - 0.05) and value of slider 1 of sheet 1 of window 1 < (myBrightness + 0.05)
				delay 0.2
			end repeat
			-- Close the Display Settings sheet
			click button "Done" of sheet 1 of window 1
			repeat while sheet 1 of window 1 exists
				delay 0.2
			end repeat
		end tell
	end try
end tell

-- # Close System Preferences

quit application "System Preferences"
applescript script stopped working after 12.0.1 (Monterey) upgrade
 
 
Q