Hey @JMichaelTX, assuming Mojave hasn't intentionally deleted System Preference's AppleScriptability, it would be much more advisable to navigate your way through to the appropriate place using panes
and anchors
. It would also bypass the issue that @cvc8445 found where the wrong UI element was being clicked.
Another tip regarding System Preferences: you don't need to activate
it, and it's a much more pleasant user experience not to do so. The UI tree can still be navigated perfectly well despite System Preferences being kept in the background and out-of-sight.
For anyone still trying out various options, here's how I would do it (for an English-language system):
use sys : application id "com.apple.systemevents"
use scripting additions
property process : a reference to process "System Preferences"
property window : a reference to window 1 of my process
property system version : (system info)'s system version
property HighSierra : the system version starts with "10.13"
--------------------------------------------------------------------------------
-- Open preferences pane and wait for it to load
tell application id "com.apple.systempreferences"
set UniversalAccess to pane id "com.apple.preference.universalaccess"
reveal anchor "Seeing_Display" in UniversalAccess
repeat until the current pane = UniversalAccess
delay 0.1
end repeat
end tell
set object to a reference to my window
-- A recursive descent through the UI elements to look
-- for checkboxes in case non-High Sierra systems have
-- a different structural hierarchy
if not HighSierra then repeat until the ¬
(object's checkboxes exists) or ¬
not (the object exists)
set the object to a reference to the object's UI elements
end repeat
if not (the object exists) then return Q(false)
click (the object's checkboxes whose name = "Increase contrast")
Q(true)
--------------------------------------------------------------------------------
on Q(e)
quit application id "com.apple.systempreferences"
return e
end Q
And, likewise, one can simply replace this line in my script:
click (the object's checkboxes whose name = "Increase contrast")
with this one:
click (the object's checkboxes whose name = "Reduce transparency")
to target that checkbox instead.
Testing in High Sierra appears successful. It takes about 3-5 seconds on my system to complete, but that's faster than doing the more UI-heavy approach.
I don't have Mojave, but I have incorporated a conditional block that runs if the system isn't High Sierra, seeing as I don't know the UI tree layout, so decided to perform a quick search for the first checkbox element it finds with a particular name. If it works in Mojave first time, I'll be gobsmacked. If Mojave System Preferences doesn't respect its own AppleScript dictionary, however, I apologise in advance for time-wasting and would understand why navigating through its UI would be necessary.