Skip to content, Skip to search

Changes

Script Parameters

2,364 bytes added, 10:45, 5 July 2018
Adding note to clarify this is a ImageJ2 feature, does not work with ImageJ1.x.
{{ScriptingLearn | 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]] [https://github.com/scijava/scijava-common/blob/scijava-common-2.40.0/src/main/java/org/scijava/plugin/Parameter.java @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 ==
The rules for <code>#@parameter</code> use are as follows:
<ol>
<li>All parameter Parameter declarations must appear in commentsbegin with <code>#@</code>. Each comment such line contains a single parameter declaration or script directive and nothing else.</li><li>Any parameters after the first non-parameter line will not be recognized.</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 output 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>
</ol>
| person = {{Person|kephale}},<br>Clojure developer
| gravatar = 9e4ed4484fd425f3f178bfeed4777b31
| source = https://gitter.im/fiji/fiji/archives/2016/04/20?at=5717afbc98c544f1396cef2f
| float = right
}}
For example, if we look at the [https://github.com/scijava/scripting-jython/blob/scripting-jython-0.2.0/src/main/resources/script_templates/Python/Greeting.py Greeting.py] [[Script_Templates|template]] supplied with Fiji:
<source lang{{GitHubEmbed| org ="python">scijava# @String name# @OUTPUT String greeting # A Jython script with parameters.# It is the duty of the | repo = scripting framework to harvest-jython# the 'name' parameter from the user, and then display# the 'greeting' output parameter, based on its type| path = src/main/resources/script_templates/Intro/Greeting.py greeting = "Hello, " + name + "!"</source>}}
We see that an input parameter <code>name</code> of type <code>String</code> is declared. <code>@Parameters</code> are handled automatically by the framework; if we run this script when the User Interface is available (e.g. from the script editor), the <code>name</code> parameter will automatically be harvested via a pop-up dialog:
We could also run this script [[Scripting Headless|headlessly]], thanks to the general nature of <code>@parameters</code>.
When the script is completed, any <code>#@OUTPUToutput</code> variables are handled by the framework, based on their type. In this case we expect the <code>greeting</code> variable to be printed, since it is a <code>string</code>.  {{Warning | Note that if we added an extra comment to the top of our script we would break the script, as the parameters would not be harvested or displayed due to violation of the second parameter rule: <source lang="python"># Because of this comment, the following parameters will NOT be recognized# @String name# @OUTPUT String greeting</source>}}
== Parameter types ==
| '''Data type'''
| '''Widget type'''
| '''Available styles'''
|-
| | <code>boolean</code> {{!}} <code>Boolean</code>
| checkbox
|
|-
| | <code>byte</code> {{!}} <code>short</code> {{!}} <code>int</code> {{!}} <code>long</code>
| numeric field
| | <code>slider</code> {{!}} <code>spinner</code> {{!}} <code>scroll bar</code>
|-
| | <code>Byte</code> {{!}} <code>Short</code> {{!}} <code>Integer</code> {{!}} <code>Long</code>
| numeric field
| | <code>slider</code> {{!}} <code>spinner</code> {{!}} <code>scroll bar</code>
|-
| | <code>BigInteger</code> {{!}} <code>BigDecimal</code>
| numeric field
| | <code>slider</code> {{!}} <code>spinner</code> {{!}} <code>scroll bar</code>
|-
| | <code>char</code> {{!}} <code>Character</code> {{!}} <code>String</code>
| text field
| | <code>text field</code> {{!}} <code>text area</code> {{!}} <code>password</code>
|-
| | <code>Dataset</code> {{!}} <code>ImagePlus</code>
| (>=2 images) triggers a dropdown list
|
|-
| | <code>ColorRGB</code>
| color chooser
|
|-
| | <code>Date</code>
| date chooser
|
|-
| | <code>File</code>
| file chooser
| | <code>open</code> {{!}} <code>save</code> {{!}} <code>directory</code> {{!}} <code>extensions:</code>
|}
<ol>
<li>All properties are defined in a '''single parenthetical expression''' immediately following the <code>#@Typetype</code> declaration.</li>
<li>Properties are set by a [https://docs.oracle.com/javase/tutorial/java/annotations/basics.html comma-separated list of '''key=value''' pairs]</li>
</ol>
Properties are your way to customize how an <code>#@parameter</code> should be handled by the framework.
=== Widget labels ===
<source lang="python">
# @String(label="Please enter your name") name# @OUTPUT output String greeting
greeting = "Hello, " + name + "!"
<source lang="python">
# @String(label="Please enter your name", description="Your name") name# @OUTPUT output String greeting
greeting = "Hello, " + name + "!"
<source lang="python">
# @Integer(label="An integer!",value=15) someInt
</source>
 
=== 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|default value]].
 
<source lang="python">
#@ Integer(label="An integer!",value=15,persist=false) someInt
</source>
{{Warning | Currently, "two scripts which declare the same parameter name, even with different types, will stomp each other." See [https://github.com/scijava/scijava-common/issues/193].}}
=== Multiple Choice ===
<source lang="python">
# @String(label="What mythical monster would you like to unleash?",choices={"Kraken","Cyclops","Medusa","Fluffy bunny"}) monsterChoice
</source>
=== 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
print(myFile)
</source>
 
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++) {
myFile=listOfPaths[i];
if (File.exists(myFile)) {
print(myFile + " exists.");
if (File.isDirectory(myFile)) {
print("Is a directory");
} else {
print("Is a file");
}
}
}
 
</source>
 
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
 
print(myImageFile)
</source>
<source lang="python">
# @File(label="Select a directory", style="directory") myDir
print(myDir)
</source>
 
=== Styles ===
 
You can influence the visual style of some of the input widgets. See previous paragraph for an example for how to switch from file to folder selection. You can also switch
 
<source lang="python">
#@ String(choices={"Option 1", "Option 2"}, style="listBox") myChoice123
#@ String(choices={"Option A", "Option B"}, style="radioButtonHorizontal") myChoiceABC
 
print(myChoice123)
print(myChoiceABC)
</source>
 
[[File:Input-styles.png]]
[[Category:Scripting]]
Emailconfirmed, administrator
947
edits