Skip to content, Skip to search


Script Parameters

1,438 bytes added, 15:45, 5 July 2018
Adding note to clarify this is a ImageJ2 feature, does not work with ImageJ1.x.
{{Learn | scripting}}All scripting languages have access to a universal <code>#@parameter</code> notation for declaring inputs and outputs. This approach is preferred to using ImageJ 1.x <code>GenericDialog</code> because it is totally agnostic of the user interface, allowing such scripts to run in a variety of contexts. As with [[Writing_ImageJ2_plugins|ImageJ2 plugins]], script parameterization is based on the [[SciJava]] [ @Parameter annotation]—so experience with plugin writing directly translates to scripting, and vice versa.  <b>Note:</b> <i>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.</i>
== Basic syntax ==
<li>Parameter declarations begin with <code>#@</code>. Each such line contains a single parameter declaration or script directive and nothing else.</li><li><code>#@type Type variableName</code> will declare an input of the indicated type, assigned to the specified name.(The use of a space between <code>#@</code> and <code>Type</code> is encouraged, but not required.)</li><li><code>#@output type Type outputName</code> will declare the variable of the specified name as an output parameter with the given type.The <code>Type</code> parameter is optional, as the output will be treated as <code>Object</code> be default. (For the <code>output</code> directive and other script directives, no space is allowed between <code>#@</code> and the directive.)</li>
| | <code>File</code>
| file chooser
| | <code>open</code> {{!}} <code>save</code> {{!}} <code>directory</code> {{!}} <code>extensions:</code>
<source lang="python">
#@String(label="Please enter your name") name
#@output String greeting
<source lang="python">
#@String(label="Please enter your name", description="Your name") name
#@output String greeting
<source lang="python">
#@Integer(label="An integer!",value=15) someInt
<source lang="python">
#@Integer(label="An integer!",value=15,persist=false) someInt
 [[{{Warning | Currently, "two scripts which declare the same parameter name, even with different types, will stomp each other." See [].]]}}
=== Multiple Choice ===
<source lang="python">
#@String(label="What mythical monster would you like to unleash?",choices={"Kraken","Cyclops","Medusa","Fluffy bunny"}) monsterChoice
=== Files and Folders ===
By default, a <code>#@File</code> parameter will create a chooser for a single file. Here is an example in python:
<source lang="python">
#@File(label="Select a file") myFile
You can request for multiple files or folders as well.
Example in ImageJ Macro Language:
<source lang="python">
#@File[] listOfPaths(label="select files or folders", style="both")
print("There are "+listOfPaths.length+" paths selected.");
for (i=0;i<listOfPaths.length;i++) {
if (File.exists(myFile)) {
print(myFile + " exists.");
if (File.isDirectory(myFile)) {
print("Is a directory");
} else {
print("Is a file");
The exact same code works for the [[Macros|ImageJ1 Macro language]], too.
You can set restrictions on accepted file types based on file extension (using a <code>style</code> property):
<source lang="python">
#@ File(label="Select an image file", style="extensions:png/jpg") myImageFile
If you want to select a directory instead, use a <code>style</code> property:
<source lang="python">
#@File(label="Select a directory", style="directory") myDir
<source lang="python">
#@String(choices={"Option 1", "Option 2"}, style="listBox") myChoice123#@String(choices={"Option A", "Option B"}, style="radioButtonHorizontal") myChoiceABC
Emailconfirmed, administrator