Greetings Michael,
In an attempt to implement the ability to run the equivalent of
Process/Math/Marco... on multiple images in unison, I am trying to borrow
code indirectly from CurveFitter. The problem is I do not know how to
determine the offset passed to Interpreter.run(int offset). Could you
explain how to determine this offset?
A few other questions...
a) is the "function dummy() {}\n" needed in the beginning of the macro? if
so, why? Does this somehow separate variable definitions from executable
code?
b) I assume that the Interpreter.run(macro, argument) is called to compile
the macro into a Program that is somehow stored in the Interpreter object.
So, can the Interpreter.run(offset) be called multiple times with
different Interpreter.(g|s)etVariable calls? Or does the String macro need
to be recompiled each time?
c) I noticed a Interpreter.run(Program, offset) method, albeit, I did not
find a way to generate a Program. Is this an avenue I should explore, or
ignore?
d) I assume that the (g|s)etVariable does not work with arrays. Is there a
better way to pass an array than what I have attempted?
Thanks in advance,
Fred
code: in case I did not explain the questions well...
public static ImagePlus macro(String macro, ImagePlus ... imps) {
String code = "var x, y, ";
String[] vna = new String[imps.length];
Array.setAll(vna, i -> "v"+i);
code += String.join(",",vna)+";\n";
code += "va=newArray("+String.join(",",vna)+");"\n;
code += macro;
IJ.log("======= code ======\n"+code);
Interpreter interpreter = new Interpreter();
try { interpreter.run(code, null); } catch (Exception e) {
status("Interpreter throw(ed) an exception."); throw(e); }
if (interpreter.wasError()) {
status("Interpreter.wasError() returned true."); return null; }
int e = 0, en = 10;
ImagePlus resimp = imps[0].duplicate();
resimp.setTitle(imps[0].getTitle()+" macro results");
float[][] rfa = new float[nx][ny];
for(int f=0; f<nf; f++)
for(int s=0; s<ns; s++) {
float[][][] fa = new float[imps.length][][];
Arrays.setAll(fa, i ->
imps[i].getStack().getProcessor(imps[i].getStackIndex(1,s+1,f+1).getFloatArray());
for(int x=0; x<nx; x++)
for(int y, y<ny; y++) try {
interpreter.setVariable("f",f+1);
interpreter.setVariable("s",s+1);
interpreter.setVariable("x",x);
interpreter.setVariable("y",y);
interpreter.setVariable("v",fa[0][x][y]);
for(int i=0; i<imps.length; i++)
interpreter.setVariable("v"+i,fa[i][x][y];
macro.run(39); // 21);
if (interpreter.wasError() && e<en)
IJ.log("macro error "+(++e)+"
at("+(f+1)+","+"(s+1)+","+x+","+y+")
"+interpreter.getErrorMessage());
rfa[x][y] = (float)macro.getVariable("v");
} catch(Exception e) {IJ.log("Exception "+e); throw(e); }
resimp.getStack().getProcessor(resimp.getStackIndex(1,s+1,f+1)).setFloatArray(rfa);
}
return resimp;
}
On Tue, June 30, 2026 4:08 pm, Fred Damen wrote:
> Greetings Michael,
>
> I am on 1.54p, so an upgrade is in order. This will lower the aggravation
> level dramatically.
>
> Thanks for the suggestions for running a macro, albeit I need to run the
> equivalent of Process/Math/Macro...
>
> Thanks for the info,
>
> Fred
>
> On Mon, June 29, 2026 11:07 am, Michael Schmid wrote:
>> Hi Fred,
>>
>> which version of ImageJ are you using?
>>
>> There was a change in 1.54s13 that should avoid deadlocks with
>> NonBlockingGenericDialogs.
>>
>> Anyhow, to run a macro from a Java plugin, I would propose
>> String result = IJ.runMacro(String macro);
>> or
>> String result = IJ.runMacro(String macro, String arg);
>>
>> According to the documantation, the result String is "[aborted]" in case
>> of an error. See
>> https://github.com/imagej/ImageJ/blob/master/ij/IJ.java#L153
>>
>>
>> Michael
>> ________________________________________________________________
>> On 28.06.26 22:52, Fred Damen wrote:
>>> Greetings,
>>>
>>> I write / run a plugin that needs to run a macro, that is generated, on
>>> a
>>> ImagePlus:
>>> String macro = ...
>>> IJ.run(tmpimp, "Macro...", "code=[ "+macro+" ] stack");
>>>
>>> The problem is if there is a error in the macro a
>>> NonblockingGenericDialog
>>> is called and then ImageJ locks up hard; ... cancel and restart.
>>>
>>> Is there a way to run a macro on an image from a plugin and if there is
>>> an
>>> error to throw an exception instead of invoking a dialog???
>>>
>>> Thanks in advance,
>>>
>>> Fred
>>>
>>> PS: the code snipit that was considered an error was a variable
>>> subtracting a negative constant: v--1
>>>
>>> PSS: a side note: leave the "code=" off the arguments and the Macro...
>>> call is silently ignored.
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html