ImageJ allows you to write scripts in several different languages.
- Read the ImageJ tutorial notebooks to learn how to write ImageJ scripts.
- Press the [ key to open the Script Editor (or ⇧ Shift-[ to open the Script Interpreter).
- 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.
ImageJ’s Script Editor supports many different languages. The following table summarizes the possibilities.
Groovy is a flexible and powerful scripting language, Java-like but less verbose and dynamically typed. Learn this, and using Java later (if needed) will become easier.
The ImageJ 1.x macro language is less powerful than the other scripting languages, but is designed to be easy to learn and use.
Python is a popular choice among scientists.
Ruby is another popular choice among web developers.
Lisp is a popular choice among computer scientists.
R is a popular choice among scientists and statisticians.
You can code Java plugins in the Script Editor. This is the most difficult path, but also the most powerful.
ImageJ can interface bidirectionally with MATLAB. See the MATLAB Scripting page for details.
Scala support is currently experimental, and has bugs.
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.
See the script parameters page for details.
Using an interpreter
All scripting languages use the same basic interpreter, with the following common features.
General key bindings
- ↑ Up: bring the previously typed command.
- ↓ Down: bring the next typed command.
- ↵ Enter: execute the contents of the prompt.
Multiline editing and keybindings
You can enlarge the prompt by dragging the middle bar.
- ⇧ Shift + ↵ Enter: create a new line within the prompt.
- ⇧ Shift + ↑ Up: move to the line above within the prompt.
- ⇧ Shift + ↓ Down: move to the line below within the prompt.
Selecting and executing text from the screen
On selecting text, a popup offers to:
- save to a new file
Using the script editor
Adding scripts to the Plugins menu
For the script to appear in the ImageJ menus, the following must apply:
- The script file is saved in the
ImageJ.app/plugins/Scriptsdirectory (or a subdirectory thereof).
- The script name ends in a supported script extension. For example:
.bshfor beanshell, and
.ijmfor ImageJ 1.x macros.
- The script name contains a
_(underscore) character, e.g.
The extension will be stripped and any underscores will be turned into spaces before the script is added to the menus.
Scripts in the top-level
ImageJ.app/plugins directory will appear at the bottom of the Plugins menu. Scripts can be placed in other menus by nesting subdirectories, for example placing a script in the
ImageJ.app/scripts/File directory will add it to the File menu.
If you aren’t able to find your script, you can always use the search bar to verify its location (or absence).
Commands added to the menu in the described way can be called from other scripts. Use the macro recorder to get the required code for doing so.
Adding JAR-packaged scripts to the menu
Inside the example-script-collection jar, the scripts are in
./resources/scripts. and therefore get added to the menu when the JAR is on the classpath (i.e. in
ImageJ2 (and therefore Fiji) looks for scripts in subfolders of
./scripts/ as it is already described in the previous section, and for jars in
./jars/. ImageJ 1.x recognizes plugins and scripts in
Running scripts in headless mode
See the Scripting Headless page for instructions on executing scripts headlessly.