Skip to content, Skip to search

Changes

Scripting

2,848 bytes added, 16 May
Add section Calling script from another script
Scripts can be packaged in a JAR file for easier distribution to your colleagues and via [Update Sites]. For this purpose, [https://github.com/imagej/example-script-collection example-script-collection] can be used as the template Maven project.
Inside the example-script-collection jar, the scripts are in <code>./resources/scripts.</code> and therefore get added to the menu when the JAR is on the classpath (i.e. in <code>./plugins/</code> or <code>./jars/</code>).
ImageJ2 (and therefore Fiji) looks for scripts in subfolders of <code>./scripts/</code> as it is already described in the previous section, and for jars in <code>./jars/</code>. ImageJ1 recognizes plugins and scripts in <code>./plugins/</code>
 
= Calling a script from another script =
There are different ways to call a script from another script.<br>
Generally, the called script is executed in the same thread than the calling script, which means that the calling script will wait that the called script terminates before going on with the rest of the execution.
 
== Using ImageJ1 commands ==
ImageJ offers the possibility to call a plugin, macro or script within another one.<br>
If the plugin is already part of the Menu, the simple command <code>run(PluginName, string Arguments)</code> (or <code>IJ.run</code> for other scripting languages) as returned by the macro-recorder will work.
 
However when one wants to call a home-made local macro that is not part of the ImageJ menu, one uses a different command (see below).<br>
Here the example of a mainMacro calling a subMacro.
 
- mainMacro
<source lang='java'>
IJ.log("Hello world, I'm mainMacro");
runMacro("C:/structure/temp/subMacro.ijm");
</source>
 
- subMacro
<source lang='java'>
IJ.log("Hello world, I'm subMacro");
</source>
 
It is also possible to pass arguments to the subMacro, it works similar to the command line execution.<br>
The subMacro needs to use <code>getArgument()</code> (or <code>IJ.imageJ.getArgs</code> of the ImageJ API) to recover the string of argument passed to it.
 
- mainMacro
<source lang='java'>
IJ.log("Hello world, I'm mainMacro");
runMacro("C:/structure/temp/subMacro.ijm", "Arg1,Arg2");
</source>
 
- subMacro
<source lang='java'>
Arguments = getArgument()
IJ.log(Arguments);
</source>
 
The command <code>runMacro</code> works only for ijm macro.<br>
To call a script written in another scripting languages, one should use the <code>runMacroFile(PathToScript, Arguments)</code> (respectively <code>IJ.runMacroFile</code> of the ImageJ API). Still using the <code>getArgument</code> to pass the variables from mainScript to subScript.
 
This 1st option is however limited to ImageJ1 code style, meaning that one cannot use script parameters, or call any service in subScript.<br>
Luckily ImageJ2 also have is own way to call a script within a script.
 
== Using ImageJ2 command ==
One can use the ScriptService from scijava to run a script within a script.<br>
Here the example of a mainScript calling a subScript both in Jython.
 
- mainScript.py
<source lang='python'>
#@ ScriptService scriptService
from ij import IJ
 
IJ.log("Hello world, I'm mainScript");
Arguments = ["some_string", "val1", "some_int", 5]
scriptService.run(r"SomePath/subScript.py", True, Arguments);
</source>
 
- subScript.py
<source lang='python'>
#@ String (label="some_string") some_string
#@ Integer (label="some_int") some_int
IJ.log(some_string)
IJ.log(str(some_int))
</source>
 
subScript must use #@ Script Parameters for the inputs, and mainScript pass the arguments to subScript as a list of <code>field, value</code>
= Running scripts in headless mode =
Emailconfirmed
76
edits