Script applications
Avoid UI scripting when possible
Recognize when Accessibility-driven clicks are brittle and replace them with files, URLs, dictionaries, Shortcuts actions, or APIs.
UI scripting finds windows and controls, then simulates user actions. Through System Events, AppleScript can click any button and choose any menu item. It sounds like the universal automation tool. It is, right up until you have to maintain one of these scripts.
Here is what it looks like:
tell application "System Events"
tell process "Notes"
set frontmost to true
click menu item "New Note" of menu "File" of menu bar 1
end tell
end tell
Nothing in that script expresses intent. It encodes where things happen to be. A menu named “File”. An item named “New Note”. A process that must be frontmost when the click lands.
What breaks it
Labels, timing, focus, localization, and layout. Any of them.
A macOS update renames a menu item. A slow launch makes the click arrive before the window exists. A French system where the menu is “Fichier”. All fatal, all silent until they strike.
What it costs
UI scripting also demands the broadest permission on the system. System Events needs Accessibility access, under System Settings → Privacy & Security → Accessibility.
A tool holding that grant can drive any application, not just the one you meant. Think about what that means for a script that keeps clicking after something unexpected appears.
Look for the real interface first
Before enabling Accessibility, check for one of these, in order:
- the app’s AppleScript dictionary
- a Shortcuts action
- a URL scheme
- a command-line interface
- a documented API
These express intent instead of screen position. For Notes, the dictionary command make new note does what the click script above does. One line, and it survives redesigns and localization.
Most of the time you find something on that list. Then the UI script was never needed.
If you truly have no choice
Contain it.
Narrow the Accessibility grant to one trusted tool. Verify the active application before sending events. Add timeouts. Stop on an unexpected window.
A UI script that keeps clicking after a surprise dialog is typing into the wrong app, with your permissions. That’s the scenario the containment rules prevent.
Then treat every UI script as debt with a review date. Note the app version it was built against. Retest it after each update of that app or of macOS. I keep that note as a comment at the top of the script, so I see it every time I open the file.
Try this: take one UI script you have, or the Notes example above, and find the dictionary command or Shortcuts action that replaces it. Then delete the click.
Lesson completed