Skip to main content

From Steps to Script

When to switch to Script​

Steps covers most actions. Switch to Script when you need something a step can't hold:

  • arithmetic, such as moving a window 100 pixels from where it is;
  • building text from pieces, such as a file name with today's date in it;
  • a builtin call inside another builtin's field, without a variable in between;
  • a loop that uses its counter, such as "for each of the first 5 windows".

Switching never loses your steps: an action keeps its Steps body and its Script body side by side (Steps, Script and AutoHotkey).

The code view​

note

Advanced mode. View code, at the end of the Steps editor, shows the steps as text: the form they are stored in. Hide code hides it again. It starts hidden each time you select another action.

Hide code
The raw steps text. Edits here rebuild the step rows above; a body the rows cannot show can only be edited here.
[[steps]] id = 1 op = "call" fn = "KeyboardPressKeyCombo" args = ["^W"]
  • An edit in the code view ② rebuilds the step rows above it as soon as the text is valid.
  • When the text can't be shown as rows, for example because it uses the "go to step" form of if, the editor shows the code without the View code link, since it is then the only way to edit the steps. The rows are replaced by a message saying why (Problems).
  • The editor never rewrites text it can't show as rows. Comments in the text are not kept once you edit the rows.

The text form itself, every kind of step and its fields, is described in The Steps engine.

Translating steps into a script​

  1. Select Script under Written as.
  2. If the action has steps and its Script body is empty, Convert steps to a script? asks whether to translate them. Click Convert.
  3. The translation appears in the Script editor. Check and finish it there.

When the steps are a recording made on the Recorder page, the dialog adds: These steps are a recording. As Script they ignore the Recorder's Replay speed and Replay mouse path detail: waits run at recorded speed and every recorded mouse move replays. (Replay settings)

The translation is one-way. The steps stay as they were, so switching back to Steps shows them unchanged, but nothing turns a script back into steps.

It isn't offered when:

  • the Script body already has text (it would be overwritten);
  • the steps can't be shown as rows;
  • any step is Repeat while, Repeat until, Exit the loop or Stop the steps. Script has only counting loops, and nothing that leaves a loop or ends the action early.

What each step becomes​

StepScript
A builtin stepOne statement, WindowMaximize(window);
A step with Save result asname = Builtin(…);
Set variablename = value;
A $name in a fieldthe bare name, name
A reading such as Target windowthe call, ContextGetWindow()
A named constant such as MouseButton.Primary, outside a Text fieldthe name as is
Texta quoted string, with backslashes and quotes escaped
An empty fieldthe type's empty value, such as 0 or ""
If / Otherwise / End ifif (…) { … } else { … }
A comparisona == b and the other operators; Reverse the result becomes !(…)
Repeat a number of timesa for loop over a counter named i (or i2 and so on, if i is taken)

For example, the maximize or restore recipe becomes:

window = ContextGetWindow();
if (WindowIsMaximized(window)) {
WindowRestore(window);
} else {
WindowMaximize(window);
}

One difference to watch: in Steps, a variable used before any step saves it reads as 0. In Script, a variable must be assigned above its first use in the text, or the script doesn't compile (Variables).

Recordings​

The Macro recorder also shows a recording as steps, with a Script button that translates it the same way. A recording kept as steps follows the Replay speed setting; a translated one plays as recorded.