GUI Scripting With Panther

in macOS edited January 2014
You too can do GUI Scripting

OK, I have had such fun with this that I will post some to explain it. With Jaguar and Panther, Apple introduced GUI scripting. In a nutshell, this means you can script applications that aren't scriptable, or script parts of the OS that don't have the scripting action that you want. It uses the Accessibility API, under Universal Access, so it is clean and not like QuicKeys making the mouse click a certain spot on the screen - essentially you tell the app's buttons to click themselves, menus to select themselves, etc.

You can do this just coding AppleScript, but it is a royal pain because you have to find out what the name or index is of buttons and dropdowns and etc. that you want to activate.

For learning the way that UI elements are referred to in the GUI scripting, you can use PreFab's UI Browser. »

This utility will let you open an app, point to parts of the app's interface, select what you want to do with that part of the interface, and it will generate the AppleScript code and even paste it into Script Editor for you. You can also test out the action that you want to perform, and see if the app does the right thing before you put the code into a script. Also, there is a check box to make the selected user interface element highlight in yellow. Attached is a screenshot of UI Browser being used to determine how to reference the third button in the Startup Disk pane of System Preferences:

I have included the script below that changes the startup disk. To load the script into your Script Editor, copy and paste the code

tell application "System Preferences"


set current pane to pane ""

end tell

tell application "System Events"

tell process "System Preferences"

set value of attribute "AXFocused" of UI element 2 of radio group 1 of scroll area 1 of group 1 of splitter group 1 of window 1 to true

end tell

-- you would put "restart" here if this was actually going to restart the computer.

end tell

Sign In or Register to comment.