Dialog box for user interaction


#1

Hi guys. I am relatively new to imagej and now I am trying to develop some macros of my own that I can use for analyzing my images. In one of these macros, I want to export the images that to a given folder as tiff’s. So lets say that I want to put one set of images in folder x and another set of images (from a different input folder) in folder y. Is there a way that I can create a user interactive dialog box so I can select the input and output folders on which I want the macro to be applied?


#2

Here’s the standard stuff to get people started with dialogs: https://imagej.nih.gov/ij/macros/DialogDemo.txt

Basic steps are

  1. Call the create() function
  2. Add items
  3. show()
  4. Get the items in the order that they were added to the dialog.

You can also use the utility functions for directories: path = getDirectory(“Choose a Directory”);

ImageJ Macro language doesn’t appear to have the concept of working directory, so always save the path for everything in a variable so you can prepend to the file names.

Have a look at the “File” builtin functions to handle directory & file names: https://imagej.nih.gov/ij/developer/macro/functions.html#F . Also check out builtin functions “Dialog.xxx” and d2s(n, decimalPlaces) for your advanced dialog item & string formatting needs for serialized file names, respectively.

There is no return from the user hitting cancel.

Save early, save often.
And if you ever have to do anything complicated with ImageJ, just plan on learning Java.


#3

Right, the Dialog.xxx() way as described by @Guido is the ImageJ 1.x way of achieving this.


Nowadays, with ImageJ2, it has become a lot easier, so if you use Fiji (which includes ImageJ 1.x and ImageJ2), you can use script parameters in the macro language. The following macro will ask for an input directory and print a line for each file contained in the folder:

#@File(style="directory", label="Input directory") inputFolder

list = getFileList(inputFolder);

for (i = 0; i < list.length; i++) {
	print ("Now processing " + list[i]);
}

Have a look at Templates > ImageJ 1.x > Examples > Process Folder (IJ1 Macro) in the script editor.