- Introduction
- An Example
- Imported Classes
- Pros and Cons
- Commands that Support JavaScript
- Running Scripts from the Command Line
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 packageA 'print' function is also predefined asfunction print(s) {IJ.log(s);}so print("Hello world") works as expected.Pros and Cons
- JavaScript Advantages:
- Full access to ImageJ and Java APIs
- Richer language (objects, "?" operator, etc.)
- Extensive documentation
- Standardized
- JavaScript Disadvatages:
- Slower, especally starting up
- No equivalent of macro sets
- Cannot use most of ImageJ's 286 built in macro functions
- Requires knowledge of complex ImageJ and Java APIs
- No support for "batch mode"
- Cannot create tools and toolbar menus
- Not compatible with Function Finder and CodeBar
- Requires JavaScript.jar on Macs
- No debugger
ImageJ Commands and Macro Functions that Support JavaScript
- Plugins>New>JavaScript opens a new text window with the title "Script.js". As a shortcut, type shift+n (Edit>New>Text Window) and ctrl+j (Macros>Evaluate>javaScript), which changes the title to "Unitled.js".
- Macros>Evaluate JavaScript (in the editor) runs JavaScript code in the editor window. As a shortcut, type ctrl+j.
- Macros>Run Macro (in the editor) runs JavaScript code if the title ends with ".js". As a shortcut, type ctrl+r.
- Plugins>Macros>Run runs a Javascript program contained in a text file with a ".js" extension.
- Help>Update Menus (runs when ImageJ starts) installs JavaScript (".js") programs located in the plugins folder, or subfolders, into the Plugins menu, or submenus.
- eval("script", string) macro function runs the JavaScript code contained in string in the current thread.
- run("Run...", "run=<pathpath>/name.js") macro function runs the JavaScript program contained in the file "name.js" in a separate thread.
- eval('script', File.openAsString("<pathpath>/name.js")) macro function runs the JavaScript program contained in the file "name.js" in the current thread.
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 commandjava -jar ij.jar -macro OpenImage.js blobs.tifand 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 runjava -jar ij.jar -batch OpenImage.js blobs.tifand 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.