JavaScript

Introduction

ImageJ 1.41 adds support for Javascript scripting. ImageJ uses the Mozilla Rhino interpreter built into Java 1.6 for Linux and Windows to run JavaScript. Mac users, and users of earlier versions of Java, must download JavaScript.jar into the plugins folder. This JAR file is available at    rsb.info.nih.gov/ij/download/tools/JavaScript.jar
It is also included with the Mac version of ImageJ 1.41 and later, in the ImageJ/plugins/tools folder.

Example JavaScript programs are available at rsb.info.nih.gov/ij/macros/js/. To run a script in this folder, download it, drag and drop it on the "ImageJ" window, then press ctrl-r (Macros>Run Macro). Or copy it the clipboard, press shift+v (File>New>System Clipboard), then press ctrl+j (Macros>Evaluate JavaScript). Albert Cardona's Javascript Scripting tutorial has more examples.

An Example

Run this one line script and the main ImageJ window will always be on top of other windows (requires Java 1.6).
   IJ.getInstance().setAlwaysOnTop(true);
This is something that cannot be done in the macro language because macros cannot call instance methods like setAlwaysOnTop() and they cannot handle objects, such as the one returned by the IJ.getInstance() method.

To create an "Always on Top" command, save this script in the plugins folder as "AlwaysOnTop.js", run Help>Update Menus, and there will be a new "AlwaysOnTop" command in the Plugins menu. Note that scripts in the plugins folder with ".js" extensions and macros with ".ijm" extensions do not require an underscore in the name.

To have ImageJ run the "AlwaysOnTop" command when it launches, add the following to the beginning of the ImageJ/macros/StartupMacros.txt file:

   macro "AutoRun" {
      run("Always on Top");
   }
Or use the built in eval() macro function to have "AutoRun" run the script inline:
   macro "AutoRun" {
      eval("script", "IJ.getInstance().setAlwaysOnTop(true);");
   }

Imported Classes

By default, the following ImageJ and Java classes are imported:
   ij.*
   ij.gui.*
   ij.process.*
   ij.measure.*
   java.lang.*
   java.awt.*
In v1.42j and later, the following ImageJ packages are also imported:
   ij.util.*;
   ij.plugin.*
   ij.plugin.filter.*
   ij.plugin.frame.*
Additional classes can be imported using importClass() and importPackage(), for example:
   importPackage(java.io); // import all java.io classes
   importClass(java.io.File) // import java.io.File
   importPackage(Packages.ij.text); // import ImageJ text package
A 'print' function is also predefined as
   function print(s) {IJ.log(s);}
so print("Hello world") works as expected.

Pros and Cons

ImageJ Commands and Macro Functions that Support JavaScript

Running Scripts from the Command Line

In ImageJ 1.42k and later you can run a script from the command line and pass it a string argument using the -macro or -batch command line options. As an example, this script opens an image in the 'images' directory in the users home directory:
  name = getArgument();
  if (name=="") IJ.error("No argument!");
  path = IJ.getDirectory("home")+"images/"+name;
  img = IJ.openImage(path);
  print(img.getTitle()+": "+img.getWidth()+"x"+img.getHeight());
Assume it is named 'OpenImage.js' and it is located in the macros folder. Run the command
   java -jar ij.jar -macro OpenImage.js blobs.tif
and ImageJ will launch and "blobs.tif: 256x254" is displayed in the Log window. Note that ImageJ assumed OpenImage.js is located in the ImageJ/macros directory. Or run
   java -jar ij.jar -batch OpenImage.js blobs.tif
and ImageJ does not launch and "blobs.tif: 256x254" is displayed in the terminal window.

A description of all the ImageJ command line options is available at rsb.info.nih.gov/ij/docs/install/linux.html#options.