Macro Some additional flexibility is available when calling TurboReg from ImageJ's macro language, which may be suitable in certain specialized situations. Most importantly, the landmark positions are directly accessible to further processing. The ImageJ macro language is 'typeless'. Variables do not need to be declared and do not have explicit data types. They are automatically initialized when used in an assignment statement. A variable can contain a number, a boolean, a string or an array. Projects for 2021 haven't been selected yet. You can see past years in the archive. ImageJ comes with a built in tool for recording the steps you perform on an image into the ImageJ macro language. This is a convenient way to grab the actions you take so that they can be incorporated into a batch macro. To open the Macro recorder, click on the Plugins menu item Macros Record.
ImageJ Macro language (IJM) which is designed to be easy to read, learn and use. Programs written in the IJM, or macros, can be used to perform a sequence of actions in ImageJ. ImageJ also supports several other scripting languages.
home | news | docs | download | plugins | resources | list | links
Introduction
A macro is a simple program that automates a series of ImageJ commands. The easiest way to create a macro is to record a series of commands using the command recorder.A macro is saved as a text file and executed by selecting a menu command,by pressing a key or by clicking on an icon in the ImageJ toolbar.
There are more than300 example macros on the ImageJWeb site. To try one, open it in a browser window, copy it to the clipboard (crtl-a, ctrl-c), switch to ImageJ, open an editor window (ctrl-shift-n), paste (ctrl-v), then run itusing the editor's Macros>Run Macro command (ctrl-r). Most of the example macros are also available in themacros folder, inside the ImageJ folder.
'Hello World' Example
As an example, we will create, run and install a one line
Hello World macro. First, open aneditor window using
Plugins>New>MacroImagej Macro Language Learning
(or press shift-n).In the editor window, enter the following line:
print('Hello world');
To test the macro, use the editor's
Macros>Run Macro command (or press ctrl-r).To save it, use the editor's
File>Save As command.In the Save As dialog box, enter 'Hello_World.txt' as file name, then click 'Save'. The macro will be automatically installed as a 'Hello World' command in the Plugins menu when you restart ImageJ, assuming the file name has an underscore in it and the macro was saved in the plugins folder or a subfolder. You can run this macro by pressing a single key by creatinga shortcut using
Plugins>Shortcuts>Create Shortcut.
To re-open a macro, use the File>Open or Plugins>Macros>Edit commands,or drag and drop it on the 'ImageJ' window.
Using run() to execute ImageJ commands
Use the macro language's built-inrun(command, options)function to run any of the 400+ commands in the ImageJ menu bar.The first argument (
command) is any ImageJ menu command (e.g., 'Measure' and 'Close All')and the second argument (
options), which is optional, is a string containing parameter values.The run() function works both with built-in commands and with user-writtenplugins that use ImageJ's GenericDialog and OpenDialog classes to display dialog boxes.
Use the Command Recorder (Plugins>Macros>Record) to generate run() function calls.For example, this code was recorded by running the Process>Find Maxima... command:
This code runs
Find Maxima... with 'Noise tolerence' set to 50,'Output type' set to 'Point Selection' and 'Light background' enabled.String parameters containing spaces, such as 'Point Selection', must beenclosed in brackets.
To test it, copy it to the clipboard, switch to ImageJ, pressshift-b (File>Open Samples>Blobs),press shift-n (File>New>Text Window),press ctrl-v (Edit>Paste),then run it by pressing ctrl+r (File>Macros>RunMacro).
The input fields of a dialog can be set to the contents of a macro variablesby using string concatentation:
With ImageJ 1.43 and later, there is an easier method that only requires adding '&' to thevariable name in the options string:String variables passed using this method do not have to be enclosed in brackets.For more examples, see theArgumentPassingDemo macro.
Using the Command Recorder to Generating Macros
Simple macros can be generated using the command recorder (
Plugins>Macros>Record). For example, this macro, which measures and labels a selection,
run('Measure');
run('Label');
is generated when you use the
Analyze>Measure and
Analyze>Label commands with the recorder running. Save this macro in the plugins folder, or a subfolder, as 'Measure_And_Label.txt', restart ImageJ and there will be a new 'Measure And Label' command in the Plugins menu. Use the
Plugins>Shortcuts>Create Shortcut command to assign this new command a keyboard shortcut.
In this example, the 'Measure And Label' macro is assigned to the F1 key. Note how the underscores in the macro filename (at least one is required) are converted to spaces in the command name.
Macro Sets
A macro file can contain more than one macro, with each macro declared using the
macro keyword.In this example, two macros, 'Macro 1' and 'Macro 2', are defined. To test these macros,select them,
Copy (ctrl-c), switch to ImageJ, open an editor window (ctrl-shift-n),
Paste (ctrl-v), select the editor's
Macros>Install Macros command, then select
Macros>Macro 1to run the first macro or
Macros>Macros 2 to run the second.
Macros in a macro set can communicate with eachother using global variables.
Use the editor's
File>Save As command to create a macro file containing these two macros. Name it something like 'MyMacros.txt' and save it in the macros folder inside the ImageJ folder. (Note that the '.txt extension is required.) Then, to install the macros in the
Plugins>Macros submenu, usethe
Plugins>Macros>Install command and select 'MyMacros.txt' in the file open dialog. Change the name to'StartupMacros.txt' and ImageJ will automatically install the macros when it starts up.
Keyboard Shortcuts
A macro in a macro set can be assigned a keyboard shortcut bylisting the shortcut in brackets after the macro name.In this example, pressing 'a' runs the first macro and pressing '1' runs the second. Theseshortcuts duplicate the shortcuts for
Edit>Selection>Select All and
Analyze>Gels>Select First Laneso you now have to hold down control (command on the Mac) to use the keyboard shortcuts forthese commands.
Note that keyboard shortcuts will not work unless the macros are installed and the 'ImageJ' window, or an image window, is the active (front) window and has keyboard focus.You install macros using the macro editor's Macros>Install Macros command or the Plugins>Macros>Install command.Install the two macros in the above example and you will see that the commands
get added to
Plugins>Macros submenu. Save these macros in a file named 'StartupMacros.txt'in the macros folder and ImageJ will automatically install them when it starts up.
Function keys ([f1], [f2]...[f12]) and numeric keypad keys ([n0], [n1]..[n9], [n/], [n*], [n-], [n+] or [n.]) can also be used for shortcuts.ImageJ will display an error message if a function key shortcut duplicatesa shortcut used by a plugin. Numeric keypad shortcuts are only used by macros so duplicates are not possible.With ImageJ 1.53a and later, use [&n] to createshortcuts that work on keyboards with and withoutnumeric keypadsNote that on PCs, numeric keypadshortcuts might only work when the Num Lock light is on.A more extensive example(KeyboardShortcuts)is available.
Tool Macros
You can define macros that create tools that get added to the ImageJ toolbar. There are three types of macro tools:image tools, action tools and menu tools. The three types can be combined to create a
tool set.
Tool sets can be added to the ImageJ toolbar as needed by clicking on the
>> icon in the toolbar.
Image Tools
An image tool executes when the user clicks on the image with that tool. The macro that defines it must have aname that ends in 'Tool - xxxx', where 'xxxx' is hexadecimal code (described below) that defines the tool's icon.Here is an example image tool that displays the coordinates each time the user clicks on the image:To install this tool, open an editor window (
Plugins>Macros>New), paste in the macro, then use the editor's
Macros>Install Macros command. Put this macro in a file named 'StartupMacros.txt' in the macros folder and it will automatically be installed when ImageJ starts up.A macro file can contain up to eight tool macros and any number of non-tool macros. Macro files must have a '.txt' or 'ijm' extension.
You can also permanently install a macro tool by adding it to the'>>' menu in the toolbar. As an example, save the 'Sample Tool' macroin the ImageJ/plugins/Tools folder as 'Sample_Tool.txt', restartImageJ, and select 'Sample Tool' from the toolbar's '>>' menu.
A tool can display a configuration dialog box when the user double clicks on it. To set this up, add a macro that has the samename as the tool, but with ' Options' added, and that macro will be called each time the user double clicks on the tool icon. In this example, the getNumber dialog isdisplayed when the users double clicks on the circle tool icon.
Action Tools
Tool macros with names ending in 'Action Tool' perform an actionwhen you click on their icon in the toolbar. In this example, the 'About ImageJ' windowis displayed when the user clicks on the tool icon (a question mark).Note that action tool commands that display a dialog box may notrun correctly if they are invoked using the run() function.
More examples are available at rsb.info.nih.gov/ij/macros/tools/or in the ImageJ/macros/tools folder.Use File>Open (or drag and drop) to open one of these files and the tools defined in it will be automatically installed,or use the Plugins>Macros>Install command to install tools without openingan editor window.
Menu Tools
You can use the newMenu functionto add menus to the toolbar. TheToolbar Menusmacro demonstrates how to do this. You can also customizethe contextual menu that appears when you right click on an image.The CustomPopupMenumacro demonstrates how to do this.
Tool Sets
A macro file can contain up to eight macro tools, along with any number of ordinary macros.A macro file (macro set) that contains macro tools is called a
tool set. Save a
tool set in theImageJ/macros/toolsets folder and it will appear in
>> menu at the right end of the toolbar. Select the
tool set from the
>> menu and the tools contained in it will be installed the tool bar. Restorethe default tool set by selecting 'Startup Macos' from the
>> menu.
The /ij/macros/toolsets folder on the ImageJ website contains 13 example tool sets.
Tool Icons
Tool macro icons are defined using a simple and compact instruction set consisting of a one letter commands followed by two or more lower case hex digits.
Alternatively, starting with ImageJ 1.53b, tool icons can be providedby a 24x24 image located in the macros/toolsets/icons/folder using the syntax
macro 'Test Tool - icon:name.png' { }
Icon files can be created with ImageJ by editing a 24x24 image andsaving it in JPG, BMP, GIF or PNG formats. Third party software, suchas Inkscapecan be used to produce high quality icons with transparency.Simply export the current selection usingFile>Export PNG Image with the image size set to 24x24 pixels.
Variables
The ImageJ macro language is 'typeless'. Variables do not need to be declared and do not have explicit data types. They are automatically initialized when used in an assignment statement.A variable can contain a number, a boolean, a string or an array.Numbers are stored in 64-bit double-precision floating point format. Booleansare represented by the numbers 0 and 1.The same variable can be any of these things at different times.Variable names are case-sensitive.'Name' and 'name' are different variables.
In the following example, a number, a string and an array are assigned to the same variable.
You can run this code by selecting it, copying it to the clipboard (ctrl-C), switching to ImageJ, opening an editor window (Edit>New), pasting (ctrl-V), then pressing ctrl-R. (Note: on the Mac, use the apple key instead of the control key.)Global variables should be declared before the macros that use themusing the 'var' statement. For example:
The 'var' statement should not be used inside macro or function code blocks. Using 'var' in a macroor function may cause it to fail.Operators
The ImageJ macro language supports almost all of the standard Java operators but withfewer precendence levels.The macro language does not have a switch statement but it can be simulated using if/else statements. Here is an example:- for - runs a block of code a specified number of times
- while - repeatedly runs a block of code while a condition is true
- do...while - runs a block of code once then repeats while a condition is true
The for statement has the form:The initialization is a statement that runs once at the beginning of the loop.The conditionImagej Macro Language Javascript
is evaluated at top of each iteration of the loop and the loop terminateswhen it evaluates to false.Finally, increment is a statement that runs after each iteration through the loop.In this example, a for loop is used to print the values 0, 10, 20...90.
First, the condition is evaluated. If it is true, the code block is executed and thewhile statement continues testing the condition and executing the code block until the condition becomes false.In this example, a while loop is used to print the values 0, 10, 20...90.
Instead of evaluating the condition at the top of the loop, do-while evaluates it at the bottom. Thus the code block is always executed at least once.In this example, a do...while loop is used to print the values 0, 10, 20...90.
Functions can use the return statement to return a value.The number of arguments given when calling a function mustcorrespond to the number of arguments in the function definition. Thesum() function has two arguments so it must called with two arguments.A function definition with no arguments must include the parenthesesand, unlike built-in functions, must be called with parentheses.Basic data types (strings and numbers) are passed to a function by value; arrays are passed by reference.Working with Strings
Use the indexOf() function to test to see ifone string is contained in another. Use thestartsWith() andendsWith()functions to see if a string starts with or ends withanother string. Use thesubstring() functionto extract a portion of string.Use the lengthOf() function to determine the length of a string.Use the ', '!=', '>' and 'true.
Extending the Macro Language
There are two ways to add functions implemented as Java code to the macro language.One is to use thecallfunction to call static methods defined in a plugin. The other is to write a plugin that implementsthe MacroExtension interface. The ImpPropsplugin demonstrates how to use the call function to get and set image properties and the Image5D_Extensionsplugin demonstrates how to add MacroExtension (Ext) functions that work with the Image5D plugins. Running Macros from the Command Line
You can run a macro from the command line and pass a string argument using the -macro or -batch command line options. As an example, this macro opens an image in the 'images' directory in the users home directory:Assume it is named 'OpenImage.txt' and it is located in the macros folder. Run the commandand ImageJ will launch and 'blobs.tif: 256x254' is displayed in the Log window.Note that ImageJ assumed the '.txt' extension and the ImageJ/macros directory. Or runand 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 atrsb.info.nih.gov/ij/docs/install/linux.html#options.
Debugging Macros
You can debug a macro using the commands in the Debug menu, which was added to the macro editor in ImageJ 1.42. You start a debugging sessionby pressing ctrl-d (Debug Macro). You can then single step through themacro code by repeatedly pressing ctrl-e (Step).Imagej Macro Language
There are seven commands in the Debug menu:
- Debug Macro - Starts running the macro in debug mode and opensthe 'Debug' window, which initially displays the memory usage, number of open images, and the active image's title. The macro stops running at the first executable line of code, whichis highlighted. Use one of the following commands to continue execution.
- Step - Executes the highlighted statement and advances to the next.The variable names and valuesin the 'Debug' window are updated.
- Trace - Runs the macro, displaying variable names and values in the'Debug' window as they are encountered.
- Fast Trace - Same as above, but faster.
- Run - Runs the macro to completion at normal speed.
- Run to Insertion Point - Runs the macro to a statement that was previouslydefined by clicking the mouse on an executable line of code.
- Abort - Exits the macro.
Use theTestArrayInspectormacro to practice using the debugger. The Array Inspector (third window in the illustration below)requires ImageJ 1.48d or later.Imagej Macro Language Definition
top | home | news | docs | download | plugins | resources | list | links