ImageJ allows you to write scripts in several different languages.
- 1 Getting started
- 2 Supported languages
- 3 Script parameters
- 4 Using an interpreter
- 5 Using the script editor
- 6 Adding scripts to the Plugins menu
- 7 Calling a script from another script
- 8 Running scripts in headless mode
- 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.
- See Category:Scripting for a list of all scripting-related pages on this wiki.
ImageJ's Script Editor supports many different languages. The following table summarizes the possibilities.
|Groovy||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.|
|ImageJ Macro||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 (Jython)||Python is a popular choice among scientists.|
|Ruby (JRuby)||Ruby is another popular choice among web developers.|
|Lisp (Clojure)||Lisp is a popular choice among computer scientists.|
|R (Renjin)||R is a popular choice among scientists and statisticians.|
|Java||You can code Java plugins in the Script Editor. This is the most difficult path, but also the most powerful.|
|MATLAB||ImageJ can interface bidirectionally with MATLAB. See the MATLAB Scripting page for details.|
|BeanShell||BeanShell is an old script language, maintained mostly for backwards compatibility. It is nearly 100% compatible with Java syntax, but so is Groovy.|
|Scala||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
- ↑: bring the previously typed command.
- ↓: bring the next typed command.
- ⌅ Enter or ⏎ Return: 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+↑: move to the line above within the prompt.
- ⇧ Shift+↓: 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
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
- ".groovy" for groovy,
- ".py" for jython,
- ".rb" for jruby,
- ".clj" for clojure,
- ".bsh" for beanshell, and
- ".ijm" for ImageJ 1.x macros.
- The script name contains a '_' (underscore) character, e.g. "MyScript_.ijm".
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 run the Command Finder 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.
Scripts can be packaged in a JAR file for easier distribution to your colleagues and via [Update Sites]. For this purpose, example-script-collection can be used as the template Maven project.
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/. ImageJ1 recognizes plugins and scripts in
Calling a script from another script
There are different ways to call a script from another script.
Generally, the called script is executed in the same thread than the calling script, which means that the calling script will wait that the called script terminates before going on with the rest of the execution.
Using ImageJ1 commands
ImageJ offers the possibility to call a plugin, macro or script within another one.
If the plugin is already part of the Menu, the simple command
run(PluginName, string Arguments) (or
IJ.run for other scripting languages) as returned by the macro-recorder will work.
However when one wants to call a home-made local macro that is not part of the ImageJ menu, one uses a different command (see below).
Here the example of a mainMacro calling a subMacro.
IJ.log("Hello world, I'm mainMacro"); runMacro("C:/structure/temp/subMacro.ijm");
IJ.log("Hello world, I'm subMacro");
It is also possible to pass arguments to the subMacro, it works similar to the command line execution.
The subMacro needs to use
IJ.imageJ.getArgs of the ImageJ API) to recover the string of argument passed to it.
IJ.log("Hello world, I'm mainMacro"); runMacro("C:/structure/temp/subMacro.ijm", "Arg1,Arg2");
Arguments = getArgument() IJ.log(Arguments);
runMacro works only for ijm macro.
To call a script written in another scripting languages, one should use the
runMacroFile(PathToScript, Arguments) (respectively
IJ.runMacroFile of the ImageJ API). Still using the
getArgument to pass the variables from mainScript to subScript.
This 1st option is however limited to ImageJ1 code style, meaning that one cannot use script parameters, or call any service in subScript.
Luckily ImageJ2 also have is own way to call a script within a script.
Using ImageJ2 command
One can use the ScriptService from scijava to run a script within a script.
Here the example of a mainScript calling a subScript both in Jython.
#@ ScriptService scriptService from ij import IJ IJ.log("Hello world, I'm mainScript"); Arguments = ["some_string", "val1", "some_int", 5] scriptService.run(r"SomePath/subScript.py", True, Arguments);
#@ String (label="some_string") some_string #@ Integer (label="some_int") some_int IJ.log(some_string) IJ.log(str(some_int))
subScript must use #@ Script Parameters for the inputs, and mainScript pass the arguments to subScript as a list of
Running scripts in headless mode
See the Scripting Headless page for instructions on executing scripts headlessly.