Redirect ImageJ/Fiji print macro to console

I’m calling ImageJ/Fiji from the Linux console to run a macro. For example, I have the macro

print("Hello");

run("Quit");

and I’m calling with

$ imagej -macro hello.ijm 

I would like to have ImageJ/Fiji’s log window redirected to the Linux console because my macro will close ImageJ/Fiji.

The Scripting Headless page says

The optional --console argument allows to have print , IJ.log and error statements returned to the console window.

When I run

$ imagej --console -macro t.ijm 

the redirect does not work.

If I run

$ imagej --headless --console -macro t.ijm

the redirect will work. Unfortunately, my macro uses ROI Manager that does not work headless (as mentioned in ROI Manager does not work headless · Issue #153 · imagej/imagej-legacy · GitHub).

Do you know another way that I can redirect ImageJ/Fiji print macro to Linux console? Thanks!

Thanks @raniere.silva for reporting. I opened an issue on GitHub to track this:

1 Like

Hi @raniere.silva ,

A workaround suggestion, since I stumbled onto the exact same problem by chance just yesterday:
I save the Log file output to process it further from the command line:

selectWindow("Log");
saveAs("Text", saveDir+"Log.txt"); // log
2 Likes

imagej -batch hello.ijm

redirects print() macro output to the console.

4 Likes

@Wayne, Thanks for the answer!

Long report to anyone looking at this topic in the future:

$ imagej --headless --console -eval "print(getVersion());" | tail -n 1
2.1.0/1.53g

me.ijm content is

run("Blobs (25K)");
makeRectangle(10, 10, 50, 50);
roiManager("Add");  // Do NOT work in headless mode

print("Hello");
  1. imagej -macro me.ijm will output Hello in the Log window and keep ImageJ running.
  2. imagej -batch me.ijm will output Hello in the shell and close ImageJ at the end of the script.
  3. imagej --headless -macro me.ijm will raise java.awt.HeadlessException because of roiManager("Add") and close ImageJ.
  4. imagej --headless -batch me.ijm will raise java.awt.HeadlessException because of roiManager("Add") and close ImageJ.
2 Likes