Search code examples
javaapiimagej

ImageJ GenericDialog - String containing backslashes is not escaped when recording macro


I am developing an ImageJ plugin and I ran into this problem.

I am using GenericDialog with a text field so the user can input a file system path to a file. On windows, the path contains backslashes. When I am recording a macro for this plugin, I get this result:

run("Example ", "path=C:\results.txt");

The backslash is not escaped and when i try to run the recorded command, the backslash is interpreted as an escape character and obviously, the file can't be found.

Is there a way to use GenericDialog in a way that it correctly records text field containing backslashes? Or I can't use GenericDialog and have to implement macro recording functionality myself?

EDIT: example plugin with the problem:

import ij.IJ;
import ij.ImagePlus;
import ij.gui.GenericDialog;
import ij.plugin.filter.ExtendedPlugInFilter;
import ij.plugin.filter.PlugInFilter;
import ij.plugin.filter.PlugInFilterRunner;
import ij.process.ImageProcessor;

public class Example_ implements ExtendedPlugInFilter {

  public int showDialog(ImagePlus imp, String command, PlugInFilterRunner pfr) {
    GenericDialog gd = new GenericDialog("Example");
    gd.addStringField("path", "C:\\results.txt");
    gd.showDialog();

    String path = gd.getNextString();
    IJ.showMessage("path = " + path);
    return PlugInFilter.DONE;
  }

  public void setNPasses(int nPasses) {
  }

  public int setup(String arg, ImagePlus imp) {
    return PlugInFilter.NO_IMAGE_REQUIRED;
  }

  public void run(ImageProcessor ip) {
  }
}

When i run the macro recorded from this plugin, it shows "path = C:esults.txt".


Solution

  • This bug is solved in the latest version of ImageJ (1.47t). In this version, if you record a macro from a GenericDialog containing Windows path in a StringField, the recorded value is correctly escaped.