Difference between revisions of "Scripting"

(Add Script parameters section, migrated from Script Editor)
(Remove duplicate section)
Line 104: Line 104:
  
 
You can create, edit and run scripts using Fiji's script editor.  For details, please see [[Using the Script Editor|the Script Editor documentation]].
 
You can create, edit and run scripts using Fiji's script editor.  For details, please see [[Using the Script Editor|the Script Editor documentation]].
 
= Script Parameters =
 
 
There is a universal <code>@ parameter</code> notation available across all scripts for declaring inputs and outputs. The rules for their use is as follows:
 
 
<ol>
 
<li>All parameter declarations must appear in comments. Each comment 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 type outputName</code> will declare the variable of the specified name as an output parameter with the given type.</li>
 
</ol>
 
 
For example, if we look at the Greeting.py template supplied with Fiji:
 
 
<source lang="Python">
 
# @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 + "!"
 
</source>
 
 
We see that an input parameter <code>name</code> of type <code>String</code> 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 <code>String</code> variable named <code>greeting</code> 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:
 
 
<source lang="Python">
 
# 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 + "!"
 
</source>
 
 
We would actually break the script, as the parameters would not be harvested or displayed due to violation of the second parameter rule.
 
  
 
= Creating scripts and using "refresh scripts" =
 
= Creating scripts and using "refresh scripts" =

Revision as of 09:02, 29 August 2014

Template:Scripting

ImageJ allows you to write scripts in several different languages.

Getting started

  • Press the [ key to open the Script Editor.
  • Optionally, choose a template from the Templates menu to get you started.
  • Otherwise, choose your language from the Language menu.
  • Grab code snippets for common tasks from the Scripting toolbox.
  • See Scripting comparisons for a side-by-side comparison of scripting languages.
  • See Category:Scripting for a list of all scripting-related pages on this wiki.

Supported languages

ImageJ's Script Editor supports the following languages:

Additionally, these languages can be installed via update sites:

Script parameters

There 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.

Using an interpreter

All scripting languages use the same basic interpreter, with the following common features.

General key bindings

  • up arrow: bring the previously typed command.
  • down arrow: bring the next typed command.
  • enter or return: execute the contents of the prompt.

Multiline editing and keybidings

You can enlarge the prompt by dragging the middle bar.

  • Shift+ Enter: create a new line within the prompt.
  • Shift+: move to the line above within the prompt.
  • Shift+: id, down.

Selecting and executing text from the screen

On selecting text, a popup offers to:

  • copy
  • execute
  • save to a new file

Using the script editor

You can create, edit and run scripts using Fiji's script editor. For details, please see the Script Editor documentation.

Creating scripts and using "refresh scripts"

On startup, ImageJ will run all "refresh scripts" plugins, one for each supported language. This will result in all scripts present within the plugins folders to be added to the menus.

To run a script, just select it from the plugins menus.

If you edit a script that is already placed in the menus, you don't need to do anything else: just save the text file and run it again by selecting it from the menus.

If you add a new script and ImageJ is running, just go to Plugins - Scripting and run the appropriate Refresh * Scripts for the language.

For the script to appear in the Plugin menus, it needs to terminate in the appropriate file extension. For example, ".js" for javascript, ".py" for jython, ".rb" for jruby, ".clj" for clojure, and ".bs" for beanshell script. The script must also contain a '_' (underscore) in the name. The extension will be stripped and any underscores will be turned into spaces before the script is added to the menus.

Running scripts in headless mode

See the Headless page for instructions on executing scripts headlessly.