# 2.3.1.5 Macros

## Definition of the Macros

The command syntax,

define macroName {script}

defines a macro called macroName, and associates it with the given script. MacroName can then be used like a command, and invokes the given script.

For example, the following script defines a macro that uses a loop to print a text string three times.

def hello
{
loop (ii, 1, 3)
{ type "$(ii). Hello World"; } }; Once the hello macro is defined, typing the word hello in the Script window results in the printout: 1. Hello World 2. Hello World 3. Hello World Once a macro is defined, you can also see the script associated with it by typing define macroName; ## Passing Arguments to Macros Macros can take up to five arguments. The %1-%5 syntax is used within the macro to access the value of each argument. A macro can accept a number, string, variable, dataset, function, or script as an argument. Passing arguments to a macro is similar to passing arguments to a script. If arguments are passed to a macro, the macro can report the number of arguments using the macro.nArg object property. For example, the following script defines a macro named myDouble that expects a single numeric argument. The given argument is then multiplied by 2, and the result is printed. def myDouble { type "$(%1 * 2)"; };

If you define this macro and then type the following in the Script window:

myDouble 5

Origin outputs the result to the Script Window:

10

You could modify this macro to take two arguments:

def myDouble { type "\$(%1 * %2)"; };

Now, if you type the following in the Script window:

myDouble 5 4

Origin outputs:

20

## Macro Property

The macro object contains one property which stores the number of arguments passed to the macro.

Property Access Description
Macro.nArg Read only, numeric This property stores the number of arguments passed to the macro.

For example:

The following script defines a macro called TypeArgs. If three arguments are passed to the TypeArgs macro, the macro types the three arguments to the Script window.

Def TypeArgs
{
if (macro.narg != 3)
{
type "Error! You must pass 3 arguments!";
}
else
{
type "The first argument passed was %1.";
type "The second argument passed was %2.";
type "The third argument passed was %3.";
}
};

If you define the TypeArgs macro as in the example, and then type the following in the Script window:

TypeArgs  One;

Origin returns the following to the Script window:

Error! You must pass 3 arguments!

If you define the TypeArgs macro as in the example, and then type the following in the Script window:

TypeArgs  One (This is argument Two) Three;

Origin returns the following to the Script window:

The first argument passed was One.
The third argument passed was Three.