Skip to content, Skip to search

Changes

Script Parameters

489 bytes removed, 11:45, 17 June 2017
Update to the new script parameter syntax!
{{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]] [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.
== 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 and nothing else.</li><li>Any parameters after the first non-parameter line will not be recognized.</li><li><code>#@type variableName</code> will declare an input of the indicated type, assigned to the specified name.</li><li><code>#@OUTPUT output type outputName</code> will declare the variable of the specified name as an output parameter with the given type.</li>
</ol>
<source lang="python">
# @String name# @OUTPUT output String greeting
# A Jython script with parameters.
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 ==
<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>
<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>
The exact same code works for the [[Macros|ImageJ1 Macro language:<source>#@File(label="Select a file") myFile print(myFile)</source>]], too.
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
print(myDir)
Bureaucrat, emailconfirmed, incoming, administrator, uploaders
11,834
edits