Browse the docs

Control browser

Controls Safari or Chrome: tabs, windows, the page address and title, links, forms and fields.

In the browser, command url

Keyboard Maestro: SafariControl, SafariControlText, SetSafariFieldToText, SetVariableToSafariField, SetSafariCheckbox, SetSafariRadio

Details #

One step for the common things you do in a browser. Pick the command, and the browser: safari, chrome, or front (the default). front uses the browser that is frontmost; if neither is, it uses Safari when running, otherwise Chrome.

Tab and window commands drive the real browser app:

  • newTab and newWindow: open one, going to url if given. They open a window first if none is open.

  • nextTab and previousTab: move through the front window's tabs, wrapping around.

  • selectTab: choose a tab by number (index), or by part of its title.

  • setURL: send the current tab to a new address (url or value).

Page commands run JavaScript in the front tab:

  • setTitle: set the page's title to value.

  • clickLink: click a link by its exact text, then by id, name or address, then by part of its text. Its address is the output.

  • focusField, selectField: focus a field, or focus it and select its contents.

  • getField: read a field's value. A checkbox or radio gives its value when checked and empty when not.

  • setField: set a field's value, then fire the page's input and change events.

  • setCheckbox: tick or untick, using checked (default true).

  • setRadio: pick the radio button with the group field and the value value.

  • submitForm, resetForm: act on the first form, or the one named in value (name, id or number).

  • waitForLoad: wait until the page has finished loading.

A field is found by its id, its name, its position (a number, counting inputs, selects and text areas on the page) or a CSS selector.

The output is the value read or set, the address for clickLink, or the browser's name for tab and window commands. into stores it in a variable.

Parameters #

ParameterTypeRequiredValuesWhat it does
commandone ofYesnewTab newWindow nextTab previousTab selectTab setURL setTitle waitForLoad clickLink focusField selectField submitForm resetForm getField setField setCheckbox setRadioWhat to do. One of newTab, newWindow, nextTab, previousTab, selectTab, setURL, setTitle, waitForLoad, clickLink, focusField, selectField, submitForm, resetForm, getField, setField, setCheckbox, setRadio. Required.
browserone ofsafari chrome frontsafari, chrome or front. Default front.
checkedyes / noFor setCheckbox. Default true.
fieldtextThe field for the field commands: id, name, number or CSS selector. Required for them. For setRadio it is the group's name.
indextextThe tab for selectTab: a number, or part of the title.
intotextThe variable that receives the output.
tabtextAnother spelling of index.
texttextAnother spelling of value.
timeoutnumberSeconds for waitForLoad. Defaults to just under the step's timeout.
urltextThe address for newTab, newWindow and setURL.
valuetextThe text for setURL, setTitle, clickLink, setField and setRadio, and the form for submitForm and resetForm. Also used as the address or tab when url or index is empty.

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

Read the page title from the front browser
Do
  1. Browser: wait for load
  2. Run JavaScript in the front browser “document.title”
  3. Show “Page: {{ title }}” for 2 s
Open in Switchboard to try it
Open a new Safari tab (really opens a tab)
Do
  1. Browser: new tab
Open in Switchboard to try it

Gotchas #

  • The first time, macOS asks to let Switchboard control the browser. If you decline, the step fails and points to System Settings > Privacy & Security > Automation.

  • Page commands act on the browser's front tab. Switch tabs first if you need another.

  • Field commands fail with no field "x" on the page, and link commands with no link "x" on the page.

  • selectTab with no number or text, setURL with no address, and a field command with no field all fail with a message saying what is missing.

  • With front and neither Safari nor Chrome running, the step fails with "no Safari or Chrome is running".

  • waitForLoad fails with "the page didn't finish loading within N s".

  • In a dry run the step is simulated and the browser is not touched.

See also #

Run JavaScript in browser, Open URL, Send HTTP request