imagej2 icon indicating copy to clipboard operation
imagej2 copied to clipboard

Command line invocation of macros with parameters containing a parenthesis will fail if Imagej is already open

Open Sethur opened this issue 7 years ago • 3 comments

The issue is related to the following earlier issue:

https://list.nih.gov/cgi-bin/wa.exe?A2=ind1512&L=IMAGEJ&P=R29972&1=IMAGEJ&9=A&J=on&d=No+Match%3BMatch%3BMatches&z=4

This was fixed, thus now something like:

imagej-win64.exe -macro "mymacro.ijm" "tmp[A(.dcm"

will not result in an error on the first invocation.

However, if one leaves the instance of ImageJ running and repeats this process, there will still be an "Macro or script not found" error, with the relevant part of the debug log showing:

[DEBUG] SocketServer.sendArgument: "user.dir C:\Programs\ImageJ"
[DEBUG] SocketServer.sendArgument: "macro ImportDicom.ijm(tmp[A()"

After this failure, a second instance of ImageJ is opened handling the macro execution correctly.

I assume this happens because the way macros are executed when there is already a running instance of ImageJ is different.

This issue is very annoying when dealing with e.g. DICOM files containing square brackets and parentheses since it effectively prevents the user from opening handling multiple datasets effectively as starting a new ImageJ instance is quite slow at the moment.

Sethur avatar Apr 20 '17 11:04 Sethur

@Sethur Did you try going to Edit ▶ Options ▶ Misc and unchecking the "Run single instance listener" option?

Or are you saying that the bug is that it does not hand off the arguments to the running instance successfully?

ctrueden avatar Apr 20 '17 18:04 ctrueden

@ctrueden Yes, I meant that the arguments are not passed correctly to the running instance. Further fiddling meanwhile revealed that the issue only occurs with ImageJ2. It can be avoided using --ij1 with the launcher, thus disabling ImageJ2 support. Apparently, there is a bug in the way the instance listener works when IJ2 support is enabled. A workaround for me now is to mask the parentheses, i.e. ( and ) when passing the path argument to my DICOM import macro, and then substitute them again in the macro, like so:

In the batch file that launches ImageJ2:

set FILENAME=%ARG1:(=___----%
set FILENAME=%FILENAME:)=____---%

imagej-win64 --no-splash -macro "ImportDICOM.ijm" %FILENAME%

In the macro:

fileName = getArgument;
if (fileName=="") exit ("No file name given!");

// Workaround for bug in ImageJ where parentheses in filenames lead to errors on multiple CLI invocations
fileName = replace(fileName, "___----", "(");
fileName = replace(fileName, "____---", ")");

// Open parent folder
if (File.isDirectory(fileName))
    open(fileName);
else
    open(File.getParent(fileName));

PS: Disabling the single instance listener is not a good workaround since the startup time of IJ2 when compared to legacy IJ has almost quadrupled, so waiting around for that every time is not an option, even when you don't want to use processing that involves multiple data sets.

Sethur avatar Apr 21 '17 08:04 Sethur

@Sethur Glad you have a workaround.

The single instance listener code indeed works differently in ImageJ2. It was actually unsupported for a long time, but then finally readded thanks to sponsorship from Zeiss. Note that the logic is part of ImageJ Legacy, meaning it relies on ImageJ1 being present as part of the installation.

Unfortunately, the developers who have worked on it in the past have left the project, and I do not personally have a good understanding of how it works. If you feel strongly about fixing this, you can have a look at the code here. Otherwise, this bug will likely not be fixed any time soon, due to lack of resources.

ctrueden avatar Apr 21 '17 13:04 ctrueden