I'm trying to toggle the "Announce the time:" checkbox on the "Dock & Menu Bar" pane of System Preferences (in Big Sur).
I've gotten this far:
tell application "System Preferences"
activate
reveal anchor "Clock" of pane id "com.apple.preference.dock" -- pane "Dock & Manu Bar"
-- nothing I've tried here works
end tell
tell application "System Events"
-- nothing I've tried here works
end tell
That reveals the correct pane with this checkbox:
I've tried using global coordinates with click at {x,y}
, but that's ignored or I get a timeout error. (I'm able to click the buttons on the main Sys Prefs screen, so I know I have the coordinates correct.) I've also tried clicking the element named Announce the time:
, but that didn't work either.
How can I toggle the Announce the time:
checkbox?
Best Answer
Using Accessibility Inspector, a part of Xcode, it shows that some of the time the Announce the time: checkbox reports "Parent does not report element as one of its children" and as such will cause AppleScript code to fail under certain conditions, and under other conditions not fail. In other words, writing my own code I can get it to check that checkbox sometimes and not other times.
In trying to ascertain a pattern to see how to workaround this situation I came to the conclusion that using fully visible UI Scripting was the most reliable way. It's also unfortunately an ugly way and requires letting the script finish without interruption once started, otherwise it will fail for sure. Such are the pitfalls of fully visible UI Scripting.
The example AppleScript code, shown below, was tested in Script Editor under macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
This script requires that the Use keyboard navigation to move focus between controls checkbox is checked on the System Preferences > Keyboard > Shortcuts tab, and as coded, the script checks its status and toggles the checkbox, as necessary, based on its current status.
Example AppleScript code:
end toggleKeyboardNavagition
, so make sure you highlight all of it when copying and pasting to Script Editor for testing.Notes:
In the
tell application "System Events"
block in with there is therepeat 9 times
loop that does the tabbing in order to toggle the Announce the time: checkbox, the value of thedelay
command may need to be adjusted.If the normal state of the Use keyboard navigation to move focus between controls checkbox is unchecked, then do not run the script immediately back to back as it takes a second or two for the value of the
property list item "AppleKeyboardUIMode"
in the users global preferences file to update the change. I'm mentioning this mainly for when doing testing more so than when in normal production use, as it shouldn't be an issue then.Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g.
delay 0.5
, with the value of the delay set appropriately.