AppleScript - How to identify buttons and press them?

Solution 1:

Here's a little trick:

Obtain the screen coordinates of that button. You can do this by initiating a screen capture with crosshairs, usually by pressing 4 (⟨Cmd⟩⟨Shift⟩⟨4⟩). Navigate the cross hairs over the button within its clickable region.

I did a similar thing with a dialog box in Script Editor:

Crosshairs in macOS

You can just about make out the coordinates of my button are {896,674}.

Next, in Script Editor, enter this line of code, inserting your coordinates as appropriate:

    tell application "System Events" to click at {896, 674}

I can assume you already have the right accessibility privileges granted from the context of your post. Therefore, you can simply run this script.

Just ensure the button is visible before you run the script, and that it hasn't changed position on the screen. Make sure there are no windows obscuring or overlying it.

All being well, two things will happen:

  1. The system will issue a click and it'll click that button;
  2. In the results pane at the bottom of the Script Editor window, you'll see something like this:
    button "Done" of window "Open" of application process "Script Editor" of application "System Events"

That is the reference to the button object, which you can copy and paste into your script.

It works for any GUI item on the screen: just isolate its coordinates, issue a click, and it will return the object reference.

UPDATE:

Having just acquired a copy of TechTool Pro version 8, I have learned that the application is not GUI-scriptable. Therefore, this explains why the trick I described failed to return an object reference (because there is none), and unfortunately, you won't be able to automate the running of TechTools using AppleScript.

Solution 2:

Sikuli

While this is possible with AppleScript, consider using Sikuli for this task:

Sikuli automates anything you see on the screen. It uses image recognition to identify and control GUI components. It is useful when there is no easy access to a GUI's internal or source code.