Press button by name
Presses a button (or increments, decrements, cancels or opens the menu of a control) in the front window by its name or accessibility id.
Press the button name in app
Keyboard Maestro: PressButton
Details #
Finds a control in the app's front window by its title or description, or by its accessibility identifier, and performs an action on it. This works through accessibility, so it needs no screen position and keeps working when the window moves. Use it for dialog buttons such as "Save" or "Don't Save".
action says what to do:
press (the default) presses the control.
showMenu opens its menu.
increment and decrement step a stepper or slider.
cancel sends the cancel action.
The name is compared ignoring case and surrounding space. By default the step keeps looking for up to 2 seconds, so a button that appears a moment later is found. With waitEnabled it also waits, up to the step's timeout, for a greyed-out button to become enabled.
The output is the button title that was used.
Parameters #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
action | one of | press showMenu increment decrement cancel | press, showMenu, increment, decrement or cancel. The default is press. Any other value fails. | |
app | text | The app to look in. Without it, the front app. | ||
id | text | The control's accessibility identifier. Give this or name. | ||
name | text | The button's title or description. Give this or id. | ||
waitEnabled | yes / no | When true, wait for a disabled button to become enabled for as long as the step's timeout allows, instead of 2 seconds. |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
A dry run only simulates the press:
- Press the “Cancel” button
- Show “Button pressed”
Gotchas #
Fails with "pressButton needs a name or an id" if you give neither.
When nothing matches, the failure reads "There's no button "X" in the front window." and lists the controls it saw, with a "Did you mean" suggestion when one is close.
A greyed-out button fails with "X is greyed out (waited N s)". Add
waitEnabled, or wait for it first with Wait.Only the app's focused window (or its first window) is searched.
Needs Accessibility permission. A dry run only simulates this step; it presses nothing.
See also #
Choose menu item, Choose menu item by name, Click, Set field value