How do I automate clicking a button in an application's window with AppleScript?

8

8

Let's say I want to click a button that appears somewhere within a window, using AppleScript. click button works, but you have to know which button exactly you want to press.


For example: Scanning is a bit cumbersome when you always have to wait for the scanner to finish, then switch to Image Capture.app, then click Scan again.

So, I want to automate clicking this button here …

enter image description here

Well, I thought that would be relatively easy, but in the beginning this is as far as I got:

tell application "System Events"
    tell process "Image Capture"
        click button "Scan" of window "Image Capture"
    end tell
end tell

Of course, the button is buried deeper than that (i.e. in some Split Group).

With the Accessibility Inspector I can see where it is, but do I really have to go down the UI element tree to find the button? Is there any shortcut? If not, what am I missing in click button "Scan" of window "Image Capture"?

enter image description here

More general: Where do I find a button in the UI element hierarchy?

slhck

Posted 2011-09-16T21:15:01.393

Reputation: 182 472

hey man, similar question here > http://superuser.com/q/1172526/234141, can you please help out?

– abbood – 2017-01-28T07:37:14.160

Answers

9

If the button is not available directly, you might have to guess.

What works in High Sierra is:

tell application "System Events"
    tell process "Image Capture"
        click button "Scan" of group 2 of splitter group 1 of window "Image Capture"
    end tell
end tell

On older macOS releases, group 1 may have to be used.

Another workaround would be to activate the window and press the space bar:

tell application "Image Capture"
    activate
    tell application "System Events" to key code 49
end tell

You can actually try to near it down by running commands like:

UI elements of window 1
UI elements of splitter group 1 of window 1
UI elements of group 1 of splitter group 1 of window 1

or, as @Lri said in the comments:

properties of UI elements of window 1
properties of UI elements of UI elements of window 1

This will present you with a list of elements contained, and you can guess your way from there.

slhck

Posted 2011-09-16T21:15:01.393

Reputation: 182 472

2UI Browser also shows the integer indexes used by System Events, and can display all UI elements of an app in a column view. It's $55 though. ▪ A possibly faster way to narrow down elements manually: properties of UI elements of UI elements of window 1 – Lri – 2011-09-18T19:41:11.057

1

In MacOS High Sierra 10.13.6, and Image Capture 7.0, the above answer worked with this slight modification:

tell application "System Events"
    tell process "Image Capture"
        click button "Scan" of group 2 of splitter group 1 of window "Image Capture"
    end tell
end tell

Jim Hickstein

Posted 2011-09-16T21:15:01.393

Reputation: 11

Thanks, good to know. You could have also suggested an edit to my answer. I fixed my post. – slhck – 2018-10-26T06:49:13.163

0

This has worked for me:

entire contents of window 1

when this came up short:

UI elements of window 1

Joel

Posted 2011-09-16T21:15:01.393

Reputation: 1