ImageJ Macro Language (Version 1.38m) - ImageJ, open an editor window (ctrl-shift-n), Paste (ctrl-v),

  • View

  • Download

Embed Size (px)

Text of ImageJ Macro Language (Version 1.38m) - ImageJ, open an editor window (ctrl-shift-n), Paste...

  • ImageJ Macro Language 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 with the mouse.

    There are more than 200 example macros on the ImageJ Web 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 it using the editor's File/Run Macro command (ctrl-r). Most of the example macros are also available in the macros 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 an editor window using Plugins/New (shift- n). In the dialog box, enter "Hello_World" as the Name, select "Macro" as the Type, then click "OK". (The underscore in the name is how ImageJ decides, when it is starting up, which ".txt" files in the plugins folder are to be installed as macros.)

    In the editor window, enter the following line:

    print("Hello world");

    To test the macro, use the editor's File/Run Macro command (ctrl-R or cmd-R). To save it, use the editor's File/Save As command. 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 creating a shortcut using Plugins/Shortcuts/Create Shortcut.

    To re-open a macro, use the File/Open or Plugins/Edit commands, or drag and drop it on the "ImageJ" window (Windows) on the the ImageJ icon (Mac).

    204 more Hello World programs are available on the ACM Hello World! Web site.

    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 file name (at least one is required) are converted to spaces in the command name.

    ImageJ Macro Language (Version 1.38m) 1

  • Macro Sets

    A macro file can contain more than one macro, with each macro declared using the macro keyword.

    macro "Macro 1" { print("This is Macro 1");


    macro "Macro 2" { print("This is Macro 2");


    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 Macro/Install Macros command, then select Macros/Macro 1 to run the first macro or Macros/Macros 2 to run the second.

    Macros in a macro set can communicate with each other using global variables.

    var s = "a string";

    macro "Enter String..." { s = getString("Enter a String:", s);


    macro "Print String" { print("This is Macro 2");


    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, use the 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 by listing the shortcut in brackets after the macro name.

    macro "Macro 1 [a]" { print("The user pressed 'a'");


    macro "Macro 2 [1]" { print("The user pressed '1'");


    In this example, pressing 'a' runs the first macro and pressing '1' runs the second. These shortcuts duplicate the shortcuts for Edit/Selection/Select All and Analyze/Gels/Select First Lane so you now have to hold down control (command on the Mac) to use the keyboard shortcuts for these 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

    Macro 1 [a] Macro 2 [1]

    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 duplicates a shortcut used by a plugin. Numeric keypad shortcuts (available in ImageJ 1.33g or later) are only used by macros so duplicates are not possible. Note that on PCs, numeric keypad shortcuts only work when the Num Lock light is on. A more extensive example (KeyboardShortcuts) is available.

    ImageJ Macro Language (Version 1.38m) 2

  • Tool Macros

    You can define macros that are are added to the ImageJ tool bar and executed when the user selects the tool and clicks on the image. Here is an example tool that displays the mouse click coordinates:

    macro "Sample Tool - C0a0L18f8L818f" { getCursorLoc(x, y, z, flags); print("Sample: "+x+" "+y);


    To install this tool, open an editor window (Plugins/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 six tool macros and any number of non-tool macros. Macro files must have a ".txt" extension. The cryptic hex code at the end of the macro name, which defines the tool icon, is described below.

    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 same name 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 is displayed when the users double clicks on the circle tool icon.

    var radius = 20;

    macro "Circle Tool - C00cO11cc" { getCursorLoc(x, y, z, flags); makeOval(x-radius, y-radius, radius*2, radius*2);


    macro "Circle Tool Options" { radius = getNumber("Radius: ", radius);


    Tool macros with names ending in "Action Tool" perform an action when you click on their icon in the tool bar. In this example, the "About ImageJ" window is displayed when the user clicks on the tool icon (a question mark).

    macro "About ImageJ Action Tool - C059T3e16?" { requires("1.37t"); run("About ImageJ...");


    More examples are available at 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 opening an editor window.

    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.

    Command Description Crgb set color Bxy set base location (default is (0,0)) Rxywh draw rectangle Fxywh draw filled rectangle Oxywh draw oval oxywh draw filled oval Lxyxy draw line Dxy draw dot (1.32g or later) Pxyxy...xy0 draw polyline Txyssc draw character

    Where x (x coordinate), y (y coodinate), w (width), h (height), r (red), g (green) and b (blue) are lower case hex digits that specify a values in the range 0-15. When drawing a character (T), ss is the decimal font size in points (e.g., 14) and c is an ASCII character.

    ImageJ Macro Language (Version 1.38m) 3

  • Variables

    The ImageJ macro language is "typeless". Variables do not need to be declared and do not have explicit data types.