Browse the docs

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 #

ParameterTypeRequiredValuesWhat it does
actionone ofpress showMenu increment decrement cancelpress, showMenu, increment, decrement or cancel. The default is press. Any other value fails.
apptextThe app to look in. Without it, the front app.
idtextThe control's accessibility identifier. Give this or name.
nametextThe button's title or description. Give this or id.
waitEnabledyes / noWhen 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 Cancel in the front dialog
Do
  1. Press the “Cancel” button
  2. Show “Button pressed”
Open in Switchboard to try it

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