Run groovy script from code

Hi,

Can someone give me an advice how to run groovy script from another application (standalone Java, not a plugin) using Image API? The script attached below runs perfectly when executed from script editor within Fiji, but fails when run from external code.

I am trying to run this script:

// Exemplary script: script.groovy
//#@String(label="path") path

import ij.IJ
import ij.ImagePlus
import ij.process.ImageProcessor
import ij.process.ShortProcessor

println IJ.getFullVersion()

curr = IJ.openImage(path)
println "Opened file ${curr}"
stack = curr.getStack()

ImageProcessor ip = stack.getProcessor(1)
ip.setAutoThreshold("Default") // not relevant for this example
ip.setThreshold(0.0, 5, 0) // not relevant for this example
mask = ip.createMask()
IJ.save(new ImagePlus("out", mask), "/tmp/out.tif")

from program:

// Script runner
import java.io.File;
import java.io.FileNotFoundException;
import javax.script.ScriptException;
import org.scijava.script.ScriptService;
import net.imagej.ImageJ;
public class Run {

  public static void main(String[] args) throws ScriptException, FileNotFoundException {
    final ImageJ ij = new ImageJ();
    // ij.ui().showUI();
    ScriptService script = ij.script();
    script.run(new File("/tmp/script.groovy"), // call the script above
            false, "path", "/tmp/Zstack_raw_deskew_c1_t46.tif");
  }
}

but I get error like this:

groovy.lang.MissingMethodException: No signature of method: ij.process.ShortProcessor.createMask() is applicable for argument types: () values: []
Possible solutions: createImage(), createImage(), getMask(), setMask(ij.process.ImageProcessor)
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:70)
	at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)

... (truncated)

Another question is if it is possible to pass ImagePlus reference to the script as a parameter, but again, from standalone application.

Thanks

Hi,
from the error message I conclude that the script has actually been executed. The problem is in the call
mask = ip.createMask()
Which is strange because the short processor actually has the method createMask().
Good luck,
Volker

Hi Volker,

Indeed this is the main problem in my example. Perhaps it has something with initialization of “old” IJ1 API that should be done somehow in Run class?
Cheers,
Piotr