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
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.
- 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
- Select Script under Written as.
- If the action has steps and its Script body is empty, Convert steps to a script? asks whether to translate them. Click Convert.
- 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
| Step | Script |
|---|---|
| A builtin step | One statement, WindowMaximize(window); |
| A step with Save result as | name = Builtin(…); |
| Set variable | name = value; |
A $name in a field | the bare name, name |
| A reading such as Target window | the call, ContextGetWindow() |
A named constant such as MouseButton.Primary, outside a Text field | the name as is |
| Text | a quoted string, with backslashes and quotes escaped |
| An empty field | the type's empty value, such as 0 or "" |
| If / Otherwise / End if | if (…) { … } else { … } |
| A comparison | a == b and the other operators; Reverse the result becomes !(…) |
| Repeat a number of times | a 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.