imagej / ImageJ

Public domain software for processing and analyzing scientific images
http://imagej.org
Other
513 stars 218 forks source link

Current working directory is not properly transferred to plugins that are executed within macros #197

Closed Sethur closed 1 year ago

Sethur commented 1 year ago

Configuration: Java 1.8.0_311 (x64) ImageJ 1.53k Windows 10.0.19042.1466

Steps for Reproduction

  1. Run ImageJ, go to Edit->Options->Misc, and check Single instance listener

  2. Create openFile.ijm macro file inside %IMAGEJ_HOME%\macros

    
    setBatchMode(true);
    filePath = getArgument();

IJ.log("Current working directory in macro: " + File.getDefaultDir); IJ.log("Macro file path argument sent to plugin: " + filePath);

run("Example Plugin", "select=&filePath");

setBatchMode("exit and display")


3. Create minimal plugin `Example_Plugin.java` as follows, compile and copy compiled jar into plugins directory:

import ij.IJ; import ij.plugin.PlugIn; import ij.io.OpenDialog;

public class Example_Plugin implements PlugIn {

public void run(String arg) {
    IJ.log("Current working directory in plugin: " + System.getProperty("user.dir"));

    OpenDialog od = new OpenDialog("Select File...", arg);
    String filePath = od.getPath();

    IJ.log("File argument from plugin: " + filePath);
}

}


4. Setup file tree as follows (one file in some directory, another in some subdirectory):
MainDir\
Foo.tif
SubDir\
    Bar.tif

5. Run ImageJ from command line with the following sequence of calls, starting inside `MainDir`:

f:\MainDir> %IMAGEJ_HOME%\ImageJ-win64.exe -macro OpenFile.ijm Foo.tif f:\MainDir> cd SubDir f:\MainDir\SubDir> %IMAGEJ_HOME%\ImageJ-win64.exe -macro OpenFile.ijm Bar.tif


5. The ImageJ Log will show the following output after the commands above are completed:

Current working directory in macro: f:\MainDir\ Macro file path argument sent to plugin: Foo.tif Current working directory in plugin: f:\MainDir File argument from plugin: Foo.tif Current working directory in macro: f:\MainDir\SubDir\ Macro file path argument sent to plugin: Bar.tif Current working directory in plugin: f:\MainDir File argument from plugin: Bar.tif


Note that for `Bar.tif`, the working directory reported in the plugin does not match the real working directory (which is correctly reported in the macro).

**Expected Behavior**
The current working directory in the plugin called in the macro via `run(...)` should match the working directory in the macro.

**Actual Behavior**
Although in the macro itself, the working directory correctly changed to the one from which the macro was externally called, the current working directory in the plugin seems to be stuck at the working directory when ImageJ was first launched.

I have attached to complete code (macro and plugin, including compiled jar) to reproduce this error.
[ExamplePlugin.zip](https://github.com/imagej/ImageJ/files/10490752/ExamplePlugin.zip)
Sethur commented 1 year ago

@rasband Can you reproduce this?

rasband commented 1 year ago

I am looking into this problem. Note that the File.getDefaultDir macro function returns the default OpenDialog directory. Use getDir("cwd") to get the current working directory.

rasband commented 1 year ago

I was able to reproduce this bug on MacOS and fix it in the ImageJ 1.54c7 daily build.

Sethur commented 1 year ago

@rasband Thank a lot for your continued effort invested in this project!