JavaScript

JavaScript

Runs user-defined JavaScript code in the processing pipeline.

Inputs

The inputs of the tool are defined by the inputParameters parameter. Input values can be accessed in the script code through an object called "$i" in the global scope. For example, if there is an input parameter called "input", its value is stored in "$i.input".

inputParameters
A table that defines input parameters. The first column contains the name of a parameter, the second one its type (optional/required/fixed) and the third one the data type of the parameter's value.
outputParameters
A table that defines output parameters. The first column contains the name of the parameter, the second one is type (synchronous/asynchronous) and the third one the data type of the parameter's value.
code
JavaScript code to be run. Assume the names of input parameters are "operand1" and "operand2", and that there is a single output parameter called "sum". The code of a tool that calculates the sum of the two operands could be written as follows:
stateEnabled
Whether the script code supports state variables or not. It may have a slight impact on performance, so if the script does not need state variables, it is advisable not to enable this parameter.
$o.sum = $i.operand1 + $i.operand2

Outputs

The outputs of the tool are defined by the outputParameters parameter. Output values can be accessed in the script code through an object called "$o" in the global scope. For example, if there is an output parameter called "output", it can be assigned a value by "$o.output = value".

If an output is synchronous, it must be assigned a value on each processing round. Leaving a synchronous output empty will cause a run-time error. On the other hand, asynchronous outputs do not need to be assigned values.

State variables

The script can also use special state variables that maintain their values between the script runs. They can be accessed in the script code through an object called "$s" in the global scope. For example, storing to the state variable "foo" can be done by "$s.foo = 'bar'". Next time the script is run, variable "$s.foo" will have 'bar'.