3.6.2 Executing LabTalk MacrosExecuting-LT-Macros
Macros can be called or executed anywhere that LabTalk scripts can be executed
This includes the Script window, the Worksheet Script dialog box, the Label Control dialog boxes, *.OGS files, user-defined Y-script fitting functions, and even other macros.
To execute the script associated with a defined macro inside the Script window:
- Type the macro name and press ENTER (see example ).
If you type the hello macro command into the Script window and press ENTER (as defined here), the script associated with the hello macro executes and prints the following in the Script window:
hello;
1. Hello World
2. Hello World
3. Hello World
A script associated with a macro name can be passed up to five arguments
To pass a macro arguments, type the macro name followed by a space separated list of the arguments into the Script window and then press ENTER. The values of the arguments passed will be accessible to the executing script through the use of the variables %1, %2, %3, %4, and %5 (see example ).
If you type the macro command goodbye Liane Sarah Amanda into the Script window and press ENTER (after having defined the goodbye macro), the script associated with the goodbye macro executes and prints the following in the Script window:
goodbye Liane Sarah Amanda;
1. Goodbye Liane
2. Goodbye Sarah
3. Goodbye Amanda
To call or execute a macro from someplace other than the Script window
Place the macro name along with any arguments you are passing to it in the calling script (terminated with a semicolon) just as you would any other LabTalk command (see example ).
hello;
inside the OGS file.
To execute the goodbye macro from inside a Label Control dialog box, include the line
goodbye Liane Sarah Amanda;
inside the Label Control dialog box.
If you want to design a macro that terminates its execution on a testable condition and then returns control back to the Script window or its calling script, use the return command (see example)
This script defines a macro named test that takes an argument, types OK if the argument is greater than 2, and then terminates its execution with the return command. If the argument is less than or equal to 2, the macro types Not OK and continues executing until it reaches its last statement.
def test {
if (%1 > 2) {
type "OK";
return;
};
type "Not OK";
type "Keep running until you reach the end!";
};
If you type the command test 3 and press <ENTER>, the macro will print OK and then terminate its execution.
test 3;
OK
If you type test 1 and press <ENTER>, the macro types Not OK and continues to run until it reaches the end of the macro.
test 1;
Not OK
Keep running until you reach the end!
|