Launch File.openDialog from Javascript Macro

I have a few years of experience with JavaScript/Web development programming but I’m new to ImageJ/Fiji. I’m trying to write a macro that ask the user to select a file to run on ImageJ 1.52r on top of Java 11.0.9.1 (64-bits).

I tried the solution provided at File open dialog in ImageJ Javascript Script and Open one or more image files from macro function but when I try to run it, I’m getting

TypeError: OpenDialog is not a function in <eval> at line number 3

The code that I’m running is

importClass(Packages.ij.io.OpenDialog);

od = OpenDialog("Choose a file", null);
folder = od.getDirectory();
file = od.getFileName();
path = folder + file

Thanks for the help.

Indeed, the error message says it all: OpenDialog is not a function, the call in line 3 should be a constructor call, which in JavaScript has to contain the new keyword:

od = new OpenDialog("Choose a file", null);

This piece was missing in the linked forum topic. I now edited the solution there to fix this.


Still I recommend the much simpler #@ parameter syntax:

#@ File (style="open") file
1 Like

Hi @imagejan. Thanks very much for the reply. The new keyword resolved the “is not a function” issue reported in the first message.

When I ran the following macro/scrip, I don’t get the dialog window that I was expecting.

importClass(Packages.ij.io.OpenDialog);

od = new OpenDialog("Choose a file", null);

Instead of the dialog window, I get the log window with

com/google/common/collect/Lists

The GIF below exemplifies my steps.

Can you help me find out what I’m missing? Thanks in advance.

This JavaScript example works as expected:

  od = new OpenDialog("Choose a file");
  directory = od.getDirectory();
  name = od.getFileName();
  IJ.showMessage("File Path", directory+name);