Passing command line parameters to groovy script

groovy
Tags: #<Tag:0x00007fd546ea2dd8>

#1

Hello,

I looked around in the internet but i cannot find the answer. I am trying to pass a command line argument to a Fiji groovy script:

This is my current groovy script attempt, but it throws an error:

println this.args[0];

I run it basically like this:

fiji.exe -run script.groovy "hello"

Error message:

Error while executing the main() method of class 'net.imagej.Main':
java.lang.IllegalArgumentException: Unknown variable: hello
	at org.scijava.parse.eval.AbstractEvaluator.get(AbstractEvaluator.java:99)

Probably really simple mistake but using Google I can’t figure it out :frowning:


Running ImageJ2 plugin from command line
#2

Dear @Christian_Tischer,

I haven’t actually tried this raw approach. I guess the issue here is that "hello" is interpreted according to the scheme of how you set script parameters from the command line: "varname1=value,varname2=value". If that’s the case, hello is not a variable that you define in your Groovy script.

That said, the following scripts works for me

#@ String hello
println hello;

when executed with that call

$ ./ImageJ-linux64 --ij2 --headless --run TestArgs.groovy "hello='something'"
...
something

What exactly are you trying to pass from the command line? Would it be possible to use script parameters? If not, we should work on improvements to script parameters to enable your use case!

Best,
Stefan


Pass parameters from a Command line to BeanShell Script
#3

Thank you very much! That works perfect for me!
I will use it how you posted it.
I just need to pass some String variables for filenames and directories.
I did not try the “raw approach” again, but I guess I just could not figure out the right syntax with the quotation marks and commas :wink:


#4

For future reference I just post a tiny bit more complex version here:

#@ String s1
#@ String s2

import ij.*

println s1;
println s2;

IJ.run("Quit");

/* 

On a mac, call me like this:

/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --ij2 --headless --run /Users/me/test.groovy "s1='hello',s2='world'"

*/

#5

…now I of course got interested to learn more!

#@ String(label="String1",value="aaddga") s1
#@ Integer(label="aaa",value=2) s2

import ij.*

println s1;
println s2;

For me it does not take the 2 as a default value for s2; am I doing something wrong?
(For the string value it works).


#6

Nope, but you are missing a piece of information: values are persisted by default. You can, however, disable that feature to always start out with 2 in your case:

#@ String(label="String1",value="aaddga",persist=false) s1
#@ Integer(label="aaa",value=2,persist=false) s2

println s1;
println s2;