Using the Script Editor

Revision as of 08:57, 29 August 2014 by Rueden (talk | contribs) (Further reading)
Script Editor (ImageJ)
Author Johannes Schindelin, Sumit Dubey (Google Summer of Code 2009)
Maintainer Johannes Schindelin
Source on GitHub
Initial release 11 Sep 2008
Development status active

Template:Scripting

The script editor is an invaluable help when writing scripts in any of ImageJ's supported languages.

Starting the editor

To get started, start up the script editor:

Script-Editor-new.jpg

There is also the keyboard shortcut { (open curly bracket) to open the editor.

Choosing a language

Then choose a language from the language menu:

Script-Editor-choose-language.jpg

Now you can write your script. In this tutorial, Jython was chosen as scripting language, but the process is really the same for all scripting languages.

Script-Editor-first-script.jpg

Running the script

Once you are satisfied with the script, run it. This does not require saving, but of course you should save your script later when it works.

Script-Editor-run.jpg

Note that while the script is running, the window title shows the tell-tale (Running).

You can use all of ImageJ's classes right away. Here is an example that shows a dialog where the user can input a number. For details how to write dialogs in the different scripting languages, see Scripting comparisons

Script-Editor-dialog.jpg

Script Parameters

There is a universal @ parameter notation available across all scripts for declaring inputs and outputs. The rules for their 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.

Further reading

See the Scripting Help page for an introduction to scripting.

For more information about each specific language, see the Languages section of top right sidebar.