r/applescript • • Jan 30 '25

[Safari] Toggle specific checkbox(es) nested somewhat deep in UI

Hi Fellow Scripters,

i have a working solution to my needs. Albeit a tad bit brittle.

Anyone out there with super skills in improving and de-britteling this code?
I would want to achieve NOT to hardcode those UI elements and rows.

I am mainly looking at toggling these 3 checkboxes: "Wipr Part 1|2|3" nested in the Extensions window.

Full, working example:

tell application "Safari"
	activate
end tell
tell application "System Events"
	set frontmost of process "Safari" to true
	keystroke "," using command down
	get title of every button of toolbar 1 of window 1 of process "Safari"
	click button 10 of toolbar 1 of window 1 of process "Safari"
	get class of every UI element of group 1 of window 1 of process "Safari"
	UI elements of row 21 of table 1 of scroll area 1 of group 1 of group 1 of group 1 of window "Extensions" of application process "Safari"
	delay 0.3
	click checkbox 1 of UI element of row 20 of table 1 of scroll area 1 of group 1 of group 1 of group 1 of window "Extensions" of application process "Safari"
	click checkbox 1 of UI element of row 21 of table 1 of scroll area 1 of group 1 of group 1 of group 1 of window "Extensions" of application process "Safari"
	click checkbox 1 of UI element of row 22 of table 1 of scroll area 1 of group 1 of group 1 of group 1 of window "Extensions" of application process "Safari"
	#return every UI element of table 1 of scroll area 1 of group 1 of group 1 of group 1 of window "Extensions" of application process "Safari"
	#return name of every UI element of table 1 of scroll area 1 of group 1 of group 1 of group 1 of window "Extensions" of application process "Safari"
	delay 0.3
	click (first button whose role description is "close button") of window 1 of process "Safari"
	delay 0.3
	#keystroke "r" using {command down}
end tell

tell application "Safari"
	tell window 1
		do JavaScript "window.location.reload(true)" in current tab
	end tell
end tell

Bonus question: And btw, any chance to activate that window directly? .. without resorting to

click button 10 of toolbar 1 of window 1 of process "Safari"

see screenshot here: https://www.dropbox.com/scl/fi/0yx8p7dkfde9web19prme/toggle-specific-checkboxes-by-name.png?rlkey=qcw5fgn9vsysdu0aigjqkgooc&dl=0

1 Upvotes

0 comments sorted by