# Macros

Source: https://wndsoftware.com/docs/macros

A WND Terminal macro is a short script anyone can read and edit. Record it as you work or write it by hand, and let it read the screen and decide what to do next.

Open Ctrl+K > Macros to record a macro, write one, or import a PCOMM `.mac` file. A macro types, presses keys and waits like a person. It can also read the screen into variables, choose with `if`, repeat steps, ask questions as it runs and save rows to a CSV file. Extra! Basic, Reflection Basic and VBA, HostExplorer Basic and PowerTerm PSL macros run as they are. See [Run your existing macros](https://wndsoftware.com/docs/legacy-macros).

This macro logs on to CICS. It asks for the user ID and password each time it runs.


```text
# Log on to CICS
wait "LOGON ==>"
type "CICSPROD"
press Enter
type {user}          # asked for when it runs
press Tab
type {password}      # asked for, hidden, never saved
press Enter
wait "READY" 10
```

Macros work the same on mainframe (3270), IBM i (5250) and [UNIX and Linux](https://wndsoftware.com/docs/vt-sessions) sessions, and on [Unisys, HP, Tandem and airline](https://wndsoftware.com/docs/other-terminals) sessions, in the desktop app and in a browser.


## How a script reads

- One command to a line. The first word says what the line does.
- `#` starts a comment that runs to the end of the line. A `#` inside quotes is just text.
- Upper and lower case don’t matter in commands, key names and variable names. Text in quotes is compared exactly as written.
- Blank lines and indenting don’t matter. Indent the lines inside an `if` or a `repeat` so they’re easy to follow.
- Rows and columns count from 1, the way the status line shows them.


## Typing and keys

On a small screen, swipe the table to read all columns.


| Command | What it does |
| --- | --- |
| `type "text"` | Types the text at the cursor. |
| `type {name}` | Asks for a value before the macro runs, then types it. A name with `password`, `pin`, `secret`, `passphrase` or `token` in it is asked for in a hidden box and never saved. |
| `type acct` | Types a variable’s value. |
| `type "Account " & acct` | Types a value joined from pieces. |
| `press KEY` | Presses a key. After Enter and the other keys that go to the host, it waits for the host to answer, for up to a minute. |
| `move 5 12` | Puts the cursor on row 5, column 12. The row and column can be variables. |
| `pause 2` | Waits 2 seconds, up to 600. |

Keys for `press` are Enter, Clear, PF1 to PF24, PA1 to PA3, Tab, BackTab, Home, NewLine, EraseEOF, EraseInput, Reset, Insert, Delete, Backspace and the arrow keys. IBM i sessions also have FieldExit, FieldPlus, FieldMinus, Dup, FieldMark, RollUp, RollDown, Help, SysReq, Attn and Print. UNIX and Linux sessions take VT keys by name, such as `press F5`, `press Ctrl+C` and `press KPEnter`. A UNIX host doesn’t say when it has finished, so wait for its prompt, such as `wait "$ "`.


## Waiting

On a small screen, swipe the table to read all columns.


| Command | What it does |
| --- | --- |
| `wait "READY"` | Waits until the text is on the screen, for up to 30 seconds. |
| `wait "READY" 10` | The same, for up to 10 seconds. |
| `wait until CONDITION` | Waits until a condition is true. See Conditions below. |
| `wait while CONDITION` | Waits as long as a condition is true. |
| `wait for cursor 5 12` | Waits until the cursor is on row 5, column 12. |
| `wait for keyboard` | Waits until the host gives the keyboard back. |

A wait can say how long to wait and what to do when the time runs out.


```text
wait until screen has "READY" within 20 seconds else stop "The host didn't say READY"
wait while screen has "PROCESSING" within 60 seconds else continue
```

Without `else`, the macro stops and says what didn’t appear. `else stop "message"` stops with your message. `else continue` goes on to the next line, so the macro can check the screen with `if`. A wait is at most an hour.


## Variables

A variable holds text or a whole number under a name you choose, such as `acct` or `page_count`.

On a small screen, swipe the table to read all columns.


| Command | What it does |
| --- | --- |
| `set n = 0` | Gives `n` a value. |
| `set n = n + 1` | Counts with `+` and `-`. |
| `set name = first & " " & last` | Joins values into one piece of text with `&`. |
| `set line = "Account {acct} is {status}"` | Puts variables’ values into text. |
| `ask acct "Which account?"` | Asks the person as the macro runs and keeps the answer. |

A variable written on its own must be set somewhere in the macro, by `set`, `read`, `ask` or `repeat with`. The editor marks the line if nothing sets it. A value written in braces, such as `{user}`, that nothing sets is asked for before the macro runs. That’s how `type {user}` and `type {password}` work.


## Questions and passwords

`ask` shows a small window with the question and waits for the answer. When the name sounds secret, such as `password` or `pin`, the answer goes into a hidden box. Answers are never saved or logged. A macro can type and compare a secret value, but it can’t show it with `say` or save it to a file.

Don’t write a password into a macro. Anyone who opens the macro could read it, so the editor refuses it and suggests `ask password` or `type {password}`.


## Reading the screen

On a small screen, swipe the table to read all columns.


| Command | What it reads |
| --- | --- |
| `read acct 5 20 8` | 8 characters from row 5, column 20 |
| `read line row 24` | The whole of row 24 |
| `read balance after "BALANCE:"` | The value after a label, up to two spaces in a row or the end of the line |
| `read balance after "BALANCE:" 12` | 12 characters right after the label |

Values are trimmed of spaces at both ends. The row, column and length can be variables, such as `read name r 12 44`. Hidden fields are never read. A read that touches a password field stops the macro and says where. Values your organization masks, such as card numbers, read as they show on the screen, masked.


## Conditions

On a small screen, swipe the table to read all columns.


| Condition | True when |
| --- | --- |
| `screen has "text"` | The text is anywhere on the screen. |
| `row 24 has "MORE"` | Row 24 has the text. The row can be a variable. |
| `cursor at 5 12` | The cursor is on row 5, column 12. |
| `keyboard locked` | The host is busy, or an IBM i error is waiting for Reset. |
| `acct = "0001"` | Two values are the same. `<>`, `<`, `>`, `<=` and `>=` work too. |
| `name contains "SMITH"` | The first value has the second in it. |

Put `not` before any condition, and join conditions with `and` and `or`. Comparisons treat whole numbers as numbers, so `"9" < "10"` is true.


## Decisions


```text
ask acct "Which account?"
type acct
press Enter
if screen has "NOT FOUND"
  say "Account {acct} isn't on file"
  stop
else if row 23 has "INVALID"
  read why row 23
  stop "The host said: " & why
else
  read balance after "BALANCE:"
end
```

`if` runs the lines up to the next `else if`, `else` or `end` when its condition is true. Otherwise the first `else if` that’s true runs, or the `else` lines.


## Repeating

On a small screen, swipe the table to read all columns.


| Command | Repeats |
| --- | --- |
| `repeat 5 times` | 5 times. The number can be a variable. |
| `repeat while CONDITION` | As long as the condition is true |
| `repeat until CONDITION` | Until the condition is true |
| `repeat with r from 6 to 22` | Once for each number from 6 to 22, with `r` set to it. `by 2` sets the step. |
| `repeat` | Until `exit repeat` |

`end` closes a repeat, and `exit repeat` leaves it at once. A macro can never loop forever. One run goes round its repeats at most 100,000 times, then stops with a message. The Stop button on the running macro’s note stops it at any time.

This macro saves every data set in an ISPF list to a CSV file, page by page.


```text
write "datasets.csv" "Data set", "Volume"
repeat
  repeat with r from 6 to 22
    if row r has "End of Data Set list"
      exit repeat
    end
    read name r 12 44
    read volume r 74 6
    append "datasets.csv" name, volume
  end
  if screen has "End of Data Set list"
    exit repeat
  end
  press PF8
end
say "Saved the list"
```

Check for the end of the list before you press the key that pages, so the last page is saved too. On IBM i, page with `press RollUp` and stop at “Bottom”.


## Messages, stopping and other macros

On a small screen, swipe the table to read all columns.


| Command | What it does |
| --- | --- |
| `say "All done"` | Shows the message on the running macro’s note and goes on. |
| `stop` | Ends the macro there. |
| `stop "Account closed"` | Ends the macro and shows the message. |
| `call "Logon"` | Runs the saved macro called Logon, then goes on. Both share the same variables. |

A macro can’t call itself, even through other macros, and calls go at most 8 deep.


## Saving to files

On a small screen, swipe the table to read all columns.


| Command | What it does |
| --- | --- |
| `write "accounts.csv" "Account", "Name"` | Starts the file again with this row, usually a header. |
| `append "accounts.csv" acct, name` | Adds a row to the end of the file, and makes the file if it isn’t there. |

Files go in the WND Terminal macros folder in your Documents folder. In a browser, each file downloads when the macro ends. A file name has no folder, and only `.csv` and `.txt` files can be saved. Values are written as CSV that Excel reads correctly, with accents intact. A value Excel would take as a formula gets an apostrophe in front.

When the organization turns off copying from the screen (`AllowCopy` = false), macros can’t save screen data either. A macro that saves files then doesn’t start, and says why. The audit log records each run (`macro.run`) and each saved file (`macro.file`), with how many rows it saved. It never records what the rows held.


## Mistakes

The editor colors the script, numbers its lines and checks it as you type. Each mistake is marked on its line and says what’s wrong, such as `Line 7: this if has no end`. A quick reference sits next to the editor. When a running macro stops on a problem, its note gives the line and the reason.


## Record a macro

- Press Ctrl+K and choose Macros .
- Choose Record a macro .
- Do the task on the screen as you normally would.
- Press Ctrl+K and choose Stop recording .

Recording never keeps what’s typed into a hidden field. It records `type {password}` instead, so the macro asks for the password when it runs. The recorded macro is a plain script you can edit and add decisions to.


## Run a macro

- From the Macros panel (Ctrl+K > Macros).
- From the Ctrl+K palette, such as “Run macro: Logon”.
- From a key. In Ctrl+K > Keyboard maps, set a key to “Run the macro Logon”. See [Keyboard maps](https://wndsoftware.com/docs/keyboard-maps).

Other sessions keep working while a macro runs. A recorded logon macro can also log on with [z/OS Express Logon](https://wndsoftware.com/docs/sign-on#express-logon).


## Import PCOMM macros

In the Macros panel, import a PCOMM `.mac` file. The common lines become steps, and the lines that couldn’t convert are listed. See [Import from PCOMM](https://wndsoftware.com/docs/import-pcomm).


## Moving macros from other emulators

Extra! Basic, Reflection Basic, Reflection Desktop VBA, HostExplorer Basic and PowerTerm PSL macros run in WND Terminal as they are. Import them in the Macros panel with Import a macro , and any line WND Terminal can’t run is listed with its line number. See [Run your existing macros](https://wndsoftware.com/docs/legacy-macros).

PCOMM macros with program logic, BlueZone scripts and InfoConnect CASL macros don’t run as they are. Rumba text macros and scripts convert to WND Terminal macros. The [Migration readiness report](https://wndsoftware.com/migration-readiness-report) counts them and lists each file. Their common statements have a WND Terminal equivalent, so a person can rewrite them by hand.

On a small screen, swipe the table to read all columns.


| Their statement | In WND Terminal |
| --- | --- |
| `If x = 1 Then` … `ElseIf` … `End If` | `if x = 1` … `else if` … `end` |
| `Do While cond` … `Loop` | `repeat while cond` … `end` |
| `For i = 1 To N` … `Next` | `repeat with i from 1 to N` … `end` |
| `Exit Do`, `Exit For` | `exit repeat` |
| `x = a & b` | `set x = a & b` |
| `GetString(row, col, len)`, `autECLPS.GetText` | `read x row col len` |
| `WaitForString "text"` | `wait "text"` |
| `WaitForCursor row, col` | `wait for cursor row col` |
| `SendKeys`, `TransmitTerminalKey` | `type "text"` and `press Enter` |
| `MoveTo row, col`, `SetCursorPos` | `move row col` |
| `MsgBox "text"` | `say "text"` |
| `InputBox("Question")` | `ask x "Question"` |
| `InStr(GetString(...), "text")` | `screen has "text"` |
| `Open "file.csv" For Append` then `Print #1` | `append "file.csv" a, b` |

Statements with no equivalent, such as string functions, arrays, other files and programs, Windows dialogs and COM objects, need a person to rework them. Usually that means a read at a row and column, `read … after` or `contains`. Programs that drive the emulator from outside keep working through WND Terminal’s [EHLLAPI, COM and .NET](https://wndsoftware.com/docs/ehllapi-com) support.

Some jobs people used to script are built in. [Fill from Excel](https://wndsoftware.com/fill-from-excel) types each row of a spreadsheet into an entry screen and writes the host’s answer back. [Capture all pages](https://wndsoftware.com/capture-all-pages) collects a multi-page list into one Excel table.


## Questions


### Does a recorded macro save my password?

No. Recording replaces anything typed into a hidden field with type {password}, so the macro asks for it each time it runs. That value is hidden and never saved.


### Can a macro page through a list and save it to Excel?

Yes. A macro can read rows from the screen, repeat until the end of the list and append each row to a CSV file that Excel opens.


### Can WND Terminal run VBA or Reflection Basic macros?

Yes. Import them in the Macros panel with Import a macro. WND Terminal runs Extra! Basic, Reflection Basic, Reflection Desktop VBA, HostExplorer Basic and PowerTerm PSL macros in its own interpreter, and lists any line it can’t run with its line number.


### Can a macro loop forever?

No. One run goes round its repeats at most 100,000 times, then stops with a message. The Stop button ends a running macro at any time.


### Can I run a macro from a key?

Yes. In Ctrl+K > Keyboard maps, set a key to run the macro.

Technical reviewer: [Albert Joseph](https://wndsoftware.com/authors/albert-joseph), WND Software.
