Difference between revisions of "Script Parameters"

(Add scripting category)
(Fix syntax highlighting)
Line 12: Line 12:
 
For example, if we look at the Greeting.py template supplied with Fiji:
 
For example, if we look at the Greeting.py template supplied with Fiji:
  
<source lang="Python">
+
<source lang="python">
 
# @String name
 
# @String name
 
# @OUTPUT String greeting
 
# @OUTPUT String greeting
Line 30: Line 30:
 
Note that if we added an extra comment to the top of our script as such:  
 
Note that if we added an extra comment to the top of our script as such:  
  
<source lang="Python">
+
<source lang="python">
 
# A simple python script
 
# A simple python script
 
# @String name
 
# @String name

Revision as of 05:41, 2 September 2014

Template:ScriptingThere is a universal @parameter notation available across all scripts for declaring inputs and outputs. This approach is preferred to using ImageJ 1.x GenericDialog because it is totally agnostic to the user interface, allowing such scripts to run in a variety of contexts.

The rules for @parameter use is as follows:

  1. All parameter declarations must appear in comments. Each comment line contains a single parameter declaration and nothing else.
  2. Any parameters after the first non-parameter line will not be recognized.
  3. @type variableName will declare an input of the indicated type, assigned to the specified name.
  4. @OUTPUT type outputName will declare the variable of the specified name as an output parameter with the given type.

For example, if we look at the Greeting.py template supplied with Fiji:

# @String 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. This will automatically be harvested via a pop-up dialog when the script is run.

When the script is completed, we expect to have a String variable named greeting which will be displayed as appropriate, based on the variable type.

Note that if we added an extra comment to the top of our script as such:

# A simple python script
# @String 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 would actually break the script, as the parameters would not be harvested or displayed due to violation of the second parameter rule.