Search code examples
javascriptdialogmacrosimagejimagej-macro

File open dialog in ImageJ Javascript Script


I want to write a macro/script to open a file open dialog, and then import the selected image using BF with various options.

I found this JS script for doing the latter part here:

importClass(Packages.loci.plugins.BF);
importClass(Packages['loci.plugins.in.ImporterOptions']); // 'in' is a reserved word, hence the different syntax
importClass(Packages.loci.common.Region);

var path = "/path/to/file";
var options = new ImporterOptions();
options.setId(path);
options.setAutoscale(true);
options.setCrop(true);
options.setCropRegion(0, new Region(x, y, w. h));
options.setColorMode(ImporterOptions.COLOR_MODE_COMPOSITE);
var imps = BF.openImagePlus(options);

imps[0].show();

I also found the regular macro language file open dialog here:

File.openDialog(title)

How do I do both of these things in a JS script? Specifically, how do I create the file open dialog in JS?

If it is easier to do both in the macro IJM language, rather than Javascript, then how do I create a BF object and set the import options?


Solution

  • All the ImageJ macro functions are implemented in ij.macro.Functions, so you can find what you need in Functions.java

    • From Javascript, you can use ij.io.OpenDialog:

      importClass(Packages.ij.io.OpenDialog);
      
      od = OpenDialog("Choose a file", null);
      folder = od.getDirectory();
      file = od.getFileName();
      path = folder + file;
      
    • Alternatively, you can use the option string of the Bio-Formats Importer macro command, it's all in the macro you linked to:

      run("Bio-Formats Importer", "open=" + path + "autoscale color_mode=Default view=Hyperstack stack_order=XYCZT");
      

      To get the required options, simply use the macro recorder.