Variables and Expressions
Variables and Expressions
Section titled “Variables and Expressions”A macro can hold its own variables – values that survive across actions, get set by you or by the user mid-run, and get substituted into typed text, conditions, and other expressions. Expressions themselves are real JavaScript.
This is the conceptual page for everything in Advanced Actions. Read it once, then dip into the specific action pages (Set Variable, Conditional Branches, Find Text / Color / Image) when you need them.
Overview
Section titled “Overview”Three building blocks power everything on this page:
- Variables hold values. They can be text, number, boolean, or list.
- Expressions are short JavaScript snippets. They read variables and produce values.
- Text mode is plain text where each
{name}is replaced by that variable’s value and everything else is used literally.
Every field that produces text – Type Text, Find Text, a text Set Variable, Take Screenshot / Screen Recording file names, User Verification messages, Open Application paths – offers the same Text / Expression switch, so the choice works the same way everywhere. See Text mode vs Expression mode.
Variables are referenced anywhere using curly braces: {counter} is the value of the variable named counter. The same syntax works in typed strings (Characters), in expression fields, and inside other expressions.
Variable types
Section titled “Variable types”| Type | Example values | Default |
|---|---|---|
| Text | "hello", "42 widgets" | Aa |
| Number | 0, 3.14, -7 | 0 |
| Boolean | true, false | false |
| List | ["a", "b", "c"] (text list) or [1, 2, 3] (number list) | empty [] |
Lists are homogeneous – every item has the same sub-type (text or number). You can store numbers in a text list, but then you can’t calculate with them or compare them as numbers.
Defining and editing variables
Section titled “Defining and editing variables”Open the macro’s Variables panel with the Variables button in the toolbar. Use it to add, rename, retype, and delete variables.
For each variable you set:
| Field | What it does | Default |
|---|---|---|
| Name | The identifier used in {name} references and expressions. Unique within the macro. Spaces are stripped as you type, and names are capped at 40 characters. | – |
| Type | Text / Number / Boolean / List. | Text |
| Sub-type (List only) | Text or Number. | Text |
| Default value | The starting value at the beginning of every run. | Per-type defaults shown above |
| Public | When on, this variable is visible to a parent macro that embeds this one (input override + return mapping). When off, it’s invisible to the parent. See Embedded Macros. | Off |
For List variables, you add, edit, remove, and reorder the default items right in the editor. In a Number list, every item must be a valid number.
Renaming variables
Section titled “Renaming variables”When you rename a variable, ClicKey rewrites every reference to it in the same macro at rename time – expressions, target fields, save-to fields, From list source names, list-operation values, and so on. You can rename freely without breaking your macro.
This also works across files: rename a public variable in an embedded macro, and the parent macro that uses it follows along.
If a reference is genuinely broken (you deleted a variable without recreating it, changed its type, or pasted actions from a different macro), the Expression Repair dialog opens. See Repairing broken expressions below.
Using a variable
Section titled “Using a variable”Use {variableName} in any expression, template, or condition. Examples:
- In a Characters action:
Hello {name}, your order is #{orderId} - In a Wait expression:
{baseDelay} * 2 - In a conditional expression:
{counter} > 10
Typing { in a field opens a list of your variables – pick one to insert it. An inserted reference shows as a small colored pill with the variable’s name. A reference to a variable that doesn’t exist shows in red.
How values look in the action list
Section titled “How values look in the action list”Action rows follow a few consistent rules, so you can read a row without opening it:
| What you see | What it means |
|---|---|
fx: before a value | That field is in Expression mode. Without it, the field is in Text mode. |
| A colored pill | A {name} that resolves to an existing variable, so it will be replaced at playback. Plain braces mean the text is used as typed. A pill in red means the variable no longer exists. |
| Quotation marks | Always characters that are really in the value – either typed by you, or part of an expression’s syntax. Rows never add quotes for decoration. |
(empty) in grey | The field has no value yet. |
␣ | A space at the very start or end of a Text value, which would otherwise be invisible. |
Expressions (JavaScript)
Section titled “Expressions (JavaScript)”Expressions are real JavaScript. They run synchronously and produce a value that has to match the type the action expects.
Operators and built-ins
Section titled “Operators and built-ins”- Arithmetic:
+,-,*,/,% - Comparison:
==,!=,===,!==,<,<=,>,>= - Ternary:
condition ? valueIfTrue : valueIfFalse - JavaScript built-ins:
Math.random(),Math.floor(),Math.ceil(),Math.round(),Math.max(),Math.min(),String(), and the rest of the standardMathobject. - Literals: numbers (
42,3.14), strings ("hello"or'hello'), booleans (true,false). - Variable references:
{name}anywhere. The braces are substituted with the variable’s current value before evaluation.
Decimal comma or decimal point
Section titled “Decimal comma or decimal point”In a Number field both separators work: 1,3 * 200 and 1.3 * 200 both give 260. A comma directly between two digits is read as a decimal separator and evaluated as a point.
Two limits, because a comma means something else in JavaScript:
- Inside brackets, a comma stays a separator.
Math.max(1,2)is still the larger of 1 and 2, and[1,2,3]is still a three-item list. So a decimal inside a function call or a list has to be written with a point:Math.round(1.5),[1.5, 2.5]. - Only Number fields are affected. Text, boolean, and list fields keep every comma exactly as typed, and a comma inside a string literal (
"1,3") is never touched.
The same applies to a plain value with no arithmetic: typing just 1,3 into a Number field stores 1.3.
Type rules
Section titled “Type rules”Each expression must produce one of: text, number, boolean, list. The expected type depends on where the expression is used:
| Where | Expected type |
|---|---|
| Characters expression | Text |
| Characters > From list source list | Text list |
| Wait expression | Number (milliseconds) |
| Scroll vertical distance | Number |
| Scroll horizontal distance | Number |
| Scroll duration | Number |
| Conditional expression / While / Until | Boolean |
| Set Variable expression | The variable’s type |
| Ask user timeout / number min / number max | Number |
| Stop Timer From / To (ms) | Number (milliseconds) |
| Window size, position, and coordinate-shift fields | Number |
| Check Color coordinates | Number |
| Capture Text, Find Text, and Find Image area fields | Number |
| Find Text, Find Image, and Interact with Area offsets | Number |
| Mouse coordinates and Move / Drag endpoints | Number |
| Find action target as expression | Text |
| List operation index / value | Depends on the operation |
Every field that produces text has a Text / Expression switch (see Text mode vs Expression mode). In Text mode a wrong type is impossible, so there’s nothing to validate; the type rules below apply to Expression mode and to the always-expression fields (numbers, conditions).
Wrong types in Expression mode cause a validation error in the editor before the macro can run. A common slip is typing a bare word where you meant a variable – for example as instead of {as}. The editor catches this and tells you what the field needs and how to fix it.
Text mode vs Expression mode
Section titled “Text mode vs Expression mode”Every field that produces text offers a two-way switch:
- Text – you write plain text. Each
{name}that matches an existing variable is replaced by that variable’s value; everything else is used exactly as typed, including a literal+, punctuation, and any{…}that isn’t a plain variable name. Text mode never runs JavaScript, so it can’t produce an error. - Expression – the whole field is one JavaScript expression, coerced to text. This is where you combine pieces with
+, transform values, or compute something:"Order #" + {id}.
The two behave oppositely around +. In Text, {first} + {last} produces Ada + Lovelace (the + is literal). In Expression, {first} + {last} produces AdaLovelace (the + joins the two strings). Pick Text for “some words with a value dropped in”, Expression for “build this from parts”.
Text mode’s substitution is deliberately simple and safe:
| You type (Text mode) | Result (with step = 2) |
|---|---|
before-{step} | before-2 |
{first} + {last} | Ada + Lovelace |
{step + 1} | {step + 1} – not a plain variable name, so left literal |
{"key": 1} | {"key": 1} – JSON braces are left literal |
a{2,3}b | a{2,3}b – a regex quantifier is left literal |
{missing} | {missing} – no such variable, so left literal |
To do maths or call a function, switch to Expression – e.g. "before-" + ({step} + 1).
Variable scope
Section titled “Variable scope”- Variables are macro-wide. There is no function-local scope, no block scope. Setting a variable inside a Group, a Conditional, or a Find branch persists for the rest of the run.
- Two separate macros do not share variables, even if both are open in ClicKey at the same time.
- Embedded macros have their own variable scope, with an explicit input/output bridge – see Embedded Macros.
Where variables are set
Section titled “Where variables are set”There are several ways a variable changes value during a run:
| Source | How it works |
|---|---|
| Variable default | Used at the start of every run, before any action runs. |
| Set Variable action | Explicit assignment, with many sources – see Set Variable. |
| Ask the user (inside Set Variable) | Pause and prompt the user; the answer becomes the value. |
| Find Text / Find Image | These actions can save the matched text or coordinates into variables. See Find Text, Find Image. |
| Capture Text | OCR the screen and save into a text variable. See Capture Text. |
| Embedded macro returns | A public variable from an embedded macro can be mapped back to a parent variable when it finishes. See Embedded Macros. |
| Set Variable > From list or Characters > From list with Remove from list in playback | Reads an item from a list variable and removes it from the runtime copy of that list. The saved Macro keeps the original default list. Useful for draining a queue across a loop. See Set Variable and Keyboard Input. |
Expressions in the editor: what previews and Test use
Section titled “Expressions in the editor: what previews and Test use”A coordinate or area field holding an expression has no single value while you’re editing – the real one only exists during a run. Everything the editor shows or does on screen therefore resolves the expression against each variable’s default value:
- the orange area outline that appears while an area field is focused, and the Show Area buttons,
- the crosshair preview for point fields (Mouse Click, Mouse Move, Mouse Drag, Check Color),
- the Test buttons in Find Text, Find Image, Capture Text, and the “Text in area” / “Image in area” conditions,
- Preview on a saved action row.
During playback the same fields are resolved against the variable’s live value at the moment the action runs, which is the whole point of using an expression there. The literal number stored in the macro file next to the expression is only a fallback for fields that hold no expression at all; it is never what an expression field evaluates to.
Repairing broken expressions
Section titled “Repairing broken expressions”When a variable is renamed, retyped, or deleted, or when you paste actions from another macro into one with different variables, some expressions may end up referencing something that doesn’t fit anymore. ClicKey catches this and opens the Expression Repair dialog.
For each broken reference you can:
- Edit the expression to fix it.
- Pick a replacement variable from a list.
- Delete the offending action if it’s no longer needed.
When you paste actions whose variables aren’t in this macro, the dialog also adds a Missing Variables section at the top. It lists every missing variable with its name and type. Tick the ones ClicKey should create; for the others, pick a replacement.
Pasting an embedded macro checks its nested-variable overrides and return mappings the same way: if one refers to a variable this macro doesn’t have, it shows up in the dialog like any other reference.
Notes & limitations
Section titled “Notes & limitations”- Expressions are synchronous. No
async, noawait, nosetTimeout, no network calls. They’re for computing values, not for full programs. - Variables are stored with the macro file, so they travel with it. The defaults you set are part of the file.
{variableName}substitution happens before evaluation. This means an expression like{name}.lengthbecomes (e.g.)"Alex".length– JavaScript string member access works.- Text mode substitutes only plain
{name}tokens, never JavaScript inside braces.{step + 1}in Text mode is left literal; to compute, switch to Expression mode ({step} + 1). Expressions don’t support nested braces either – arbitrary{{}}nesting isn’t parsed. - Renames rewrite every reference in the macro at rename time. Expressions, target/save-to fields, From list source names, and list-operation values all get updated in one pass.
- Numeric expressions must be finite numbers. JavaScript results like
NaN,Infinity, and-Infinityfail validation instead of being accepted as usable numbers. - List variables are arrays, so list-aware expressions need to access them as arrays:
{items}[0],{items}.length,{items}.join(","). The full set of array methods is available. A number list evaluates as real numbers, so{nums}[0]is a number you can do arithmetic with; a text list evaluates as strings.
Related
Section titled “Related”- Set Variable – assign values to variables, with all the sources and list operations.
- Conditional Branches – branch on expressions and other condition types.
- Groups, Loops, and Repeats – loops with While / Until conditions that use the same condition types.
- Find Text – save matched text and coordinates into variables.
- Find Image – save matched coordinates and dimensions into variables.
- Capture Text – write OCR output into a text variable.
- Embedded Macros – pass variables in and out of called macros.
- How Macros Work – the mental model behind variables and expressions.