All scripting languages have access to a universal #@parameter notation for declaring inputs and outputs. This approach is preferred to using ImageJ 1.x GenericDialog because it is totally agnostic of the user interface, allowing such scripts to run in a variety of contexts. As with ImageJ2 plugins, script parameterization is based on the SciJava@Parameter annotation—so experience with plugin writing directly translates to scripting, and vice versa.

Note:Script parameters are a feature of ImageJ2; they will not work in plain ImageJ1. The Fiji distribution of ImageJ is built on ImageJ2, so they also work in Fiji.

Basic syntax

Parameter declarations begin with #@. Each such line contains a single parameter declaration or script directive and nothing else.

#@ Type variableName will declare an input of the indicated type, assigned to the specified name. (The use of a space between #@ and Type is encouraged, but not required.)

#@output Type outputName will declare the variable of the specified name as an output parameter with the given type. The Type parameter is optional, as the output will be treated as Object be default. (For the output directive and other script directives, no space is allowed between #@ and the directive.)

# @String(label="Please enter your name",description="Name field") name
# @OUTPUT String greeting
# A Jython script with parameters.
# It is the duty of the scripting framework to harvest
# the 'name' parameter from the user, and then display
# the 'greeting' output parameter, based on its type.
greeting = "Hello, " + name + "!"

We see that an input parameter name of type String is declared. @Parameters are handled automatically by the framework; if we run this script when the User Interface is available (e.g. from the script editor), the name parameter will automatically be harvested via a pop-up dialog:

We could also run this script headlessly, thanks to the general nature of @parameters.

When the script is completed, any #@output variables are handled by the framework, based on their type. In this case we expect the greeting variable to be printed, since it is a string.

Parameter types

A list of possible data types and the corresponding widgets is provided:

Data type

Widget type

Available styles

boolean | Boolean

checkbox

byte | short | int | long

numeric field

slider | spinner | scroll bar

Byte | Short | Integer | Long

numeric field

slider | spinner | scroll bar

Float

numeric field

slider | spinner | scroll bar

BigInteger | BigDecimal

numeric field

slider | spinner | scroll bar

char | Character | String

text field

text field | text area | password

Dataset | ImagePlus

(>=2 images) triggers a dropdown list

ColorRGB

color chooser

Date

date chooser

File

file chooser

open | save | file | directory | extensions:

float is also an accepted field but the decimal part is not displayed in the form compared to Float (mind the capital F).
A related issue occurs with int and double when a default value is set in the code and entered in the form, the value is not properly recalled at the next run. Use Integer and Double instead.

A single #@ImagePlus or #@Dataset field will not show up in the input form, instead the current image will automatically be processed. The idea is to stick to the IJ macro language. However if 2 #@ImagePlus (or respectively #@Dataset) are present then they will be rendered as drop-down buttons.

Properties are your way to customize how an #@parameter should be handled by the framework.

Widget labels

Widgets are the User Interface elements shown to users to collect input information. For example, instead of just displaying "Name" to the user, we can add a custom label to the field of our Greeting.py script as follows:

Default values

Default values are also supported as parameter properties:

#@ Integer(label="An integer!",value=15) someInt

Persistence

Per default, variable values are persisted between runs of a script. This means that parameter values from a previous run are used as starting value. Please note that a persisted value will overwrite a defined default value.

#@ Integer(label="An integer!",value=15,persist=false) someInt

Currently, "two scripts which declare the same parameter name, even with different types, will stomp each other." See [1].

Visibility

This property set if the parameter should be displayed, editable and/or recorded.

- NORMAL: parameter is included in the history for purposes of data provenance, and included as a parameter when recording scripts.

- TRANSIENT: parameter is excluded from the history for the purposes of data provenance, but still included as a parameter when recording scripts.

- INVISIBLE: parameter is excluded from the history for the purposes of data provenance, and also excluded as a parameter when recording scripts. This option should only be used for parameters with no effect on the final
output, such as a "verbose" flag.

- MESSAGE: parameter value is intended as a message only, not editable by the user nor included as an input or output parameter.

Currently if a script containing a MESSAGE string is recorded with the macro recorder and the resulting recorded code executed, a window will show up containing only the MESSAGE string This is unexpected and will be corrected in the future.

Multiple Choice

Any parameter can be turned into a multiple-choice selector by adding a choices = {...} property:

#@ String(label="What mythical monster would you like to unleash?",choices={"Kraken","Cyclops","Medusa","Fluffy bunny"}) monsterChoice

Files and Folders

By default, a #@ File parameter will create a chooser for a single file. Here is an example in python:

#@ File(label="Select a file") myFile
print(myFile)

You can request for multiple files or folders as well. However multiple files/folders input are not yet macro-recordable.