Toggling color filters - Inconsistent Behavior

All I want is to toggle the ‘color filters’ checkbox. This exact code ran once, but now I get System Events got an error: Can’t get window “Accessibility” of process “System Preferences”.. Can someone give me a hint? I’ve been at this for two days. Such a simple task.

Notable information
• granted accessibility access to script editor, terminal, everything that has requested it.
• M1 mac on 12.0.1 Monterey :expressionless:

tell application "System Events"
	tell process "System Preferences"
		click checkbox "Enable Color Filters" of tab group 1 of group 1 of window "Accessibility"
	end tell
end tell

Model: 16" M1 pro
AppleScript: 2.8
Browser: Safari 605.1.15
Operating System: macOS 12

Maybe this will work for you.

if application "System Preferences" is running then do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
	delay 0.1
end repeat

tell application "System Preferences"
	reveal anchor "Seeing_ColorFilters" of pane "com.apple.preference.universalaccess"
	repeat while not (exists of anchor "Seeing_ColorFilters" of pane "com.apple.preference.universalaccess")
		delay 0.1
	end repeat
end tell

tell application "System Events"
	tell process "System Preferences"
		repeat while not (exists of checkbox "Enable Color Filters" of tab group 1 of group 1 of window "Accessibility")
			delay 0.1
		end repeat
		click checkbox "Enable Color Filters" of tab group 1 of group 1 of window "Accessibility"
	end tell
end tell

tell application "System Preferences" to quit

Wow, that does work! Thank you so much. So my question now is how do you look up the pane and anchors you need? or is that something you just pick up with experience?

Cheers :smiley:

Ask for them —first the panes— using ‘get’, which returns a list of the panes for your system. Then reveal the chosen pane and get its anchors. Then reveal the chosen anchor. Once you know the desired anchor, you can omit everything before the last command.

tell application "System Preferences"
	
	get panes
	--> {pane id "com.apple.preference.universalaccess" of application "System Preferences", …
	reveal pane id "com.apple.preference.universalaccess"
	
	get anchors of pane id "com.apple.preference.universalaccess"
	--> {anchor "Seeing_Display" of pane id "com.apple.preference.universalaccess" of application "System Preferences", …
	reveal anchor "Seeing_Display" of pane id "com.apple.preference.universalaccess"
	
end tell

I’m not exactly sure how experience fits in there but it is in the documentation for applescript. Note as well that the word ‘get’ is optional in the above cases (i.e. ‘panes’ will return the same information as ‘get panes’).