Is there a way to call imagej macro (.ijm) from java (i.e. have macro stored as string and execute it using a java control of imagej)?

StackOverflow https://stackoverflow.com/questions/20574863

  •  01-09-2022
  •  | 
  •  

Question

It is reversed problem to:

How can I call/execute a java program from an ImageJ macro?

Whenever I write imagej I refer to fiji.

Of course a trivial solution is to create a .ijm from java string and call imagej using a system call with .ijm as an argument, but I search for better solution. Alternatively .ijm instructions can be translated into java calls but it is not very convenient (when .jvm is enough for the job it is very convenient way to keep it in this format).

Why is it useful? It can be used e.g. to distribute a .ijm macro in obfuscated way - make it more difficult to the user to extract the macro. String containing instructions for .ijm can be decrypted only when correct password is provided, etc. Thank you for any suggestions!

Was it helpful?

Solution

Use the ij.IJ.runMacro(String) method.

Here is an example:

import ij.IJ;
import ij.plugin.PlugIn;

public class Run_Macro implements PlugIn {
    @Override
    public void run(final String arg) {
        final String macro =
            "run(\"Clown (14K)\");\n" +
            "run(\"Make Binary\");\n";
        IJ.runMacro(macro);
    }
}

A word of warning about obfuscation, though: Java is notoriously easy to decompile.

OTHER TIPS

Correct me if I am wrong but one could call IJ.runMacroFile to refer to a given macro text file in the macro folder.

public void My_Macro() {
        IJ.runMacroFile("My_Macro.txt");
}

Yet I don't know if you can run macros outside of the macro folders, for example from within a .jar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top