The processes of passing command-line parameters to ImageJ plugins isn't the most direct. Illustrated is a simple solution to this problem, full code included.
The processes of passing command-line parameters to ImageJ plugins isn't obvious. However, one can easily send arguments to macros and macros can send arguments; thus, the simplest solution would be to make an empty macro that just accepts command-line parameters and passes them to a chosen plugin. That is outlined below.
First, i created a dummy text file—inputParameters.txt—with some parameters, let's say some values for image processing (you could just as easily put directories to be used to batch image analysis). Then cmdLineFxn.bat loops over each line in inputParameters.txt and passes it to the macro in passCmdLineParameters.ijm, which should be located in the ImageJ ~/macro folder. This macro then calls myPlugin.java (should be compiled into a .class file beforehand), which should display the numbers in the log. Of course, you can do a myriad of other things with the passed arguments (i use it to batch analyze microscope movies), but this simple example should suffice.
The code is shown below along with links to each file.
- :: biafra ahanonu
- :: updated: 2013.10.11
- :: wrapper to call imagej plugin
- @echo off
- setlocal enabledelayedexpansion
- :: go line-by-line in file, get directories to analyze
- :: call imagej, pass along ImageJ directory along with name of macro used to pass parameters.
- javaw -Xmx62000m -Xms62000m -Xincgc -XX:+DisableExplicitGC -XX:+UseCompressedOops -Dplugins.dir="C:\Program Files\ImageJ" -jar "C:\Program Files\ImageJ\ij.jar" -macro passCmdLineParameters.ijm %%A
- ::check the exit status
- ::display different messages depending on how ImageJ exited.
- echo ---------------------
- )
- :: error handling, done this way to allow for loop to continue correctly.
- exit /b
- :WTF
- echo i don't know what went wrong.
- goto ENDLOOP
- :MISSINGFILES
- echo what's a folder without files!?
- goto ENDLOOP
- :FOLDERERROR
- echo folders already exists.
- goto ENDLOOP
- :TIFFERROR
- echo .tif isn't formatted correctly.
- goto ENDLOOP
- :SUCCESS
- echo AMERICA!
- goto ENDLOOP
- :ENDLOOP
- exit /b
- // biafra ahanonu
- // updated: 2013.10.11
- // macro to pass command-line inputs to plugin
- // place in ImageJ's ~/macro folder
- macro "passCmdLineParameters" {
- // pass command-line arguments to plugin
- run("myPlugin",getArgument);
- }
- /*
- biafra ahanonu
- updated: 2013.10.11
- example plugin that accepts an input parameter and prints it
- ERROR CODES
- 0 - normal exit, everything is fine
- 1 - you decide
- 2 - see above
- 3 - see above
- 4 - WTF happened?
- */
- import ij.*;
- import ij.process.*;
- import ij.gui.*;
- import ij.measure.*;
- import ij.text.*;
- import ij.plugin.*;
- import ij.plugin.filter.*;
- import ij.plugin.frame.*;
- import ij.io.*;
- import java.awt.*;
- import java.io.*;
- import javax.swing.*;
- import java.lang.reflect.*;
- import java.lang.System.*;
- import java.util.*;
- public class myPlugin implements PlugIn {
- // this function runs when the plugin is called
- // catch any unknown exceptions
- try{
- // call the main function
- mainFxn("go");
- // exit with unknown
- return;
- }
- // exit imagej, returns control back to the command line
- return;
- }
- // check that options actually passed
- if (IJ.isMacro() && Macro.getOptions() != null && !Macro.getOptions().trim().isEmpty()) {
- // get the arguments, put separator in trim if passed multiple arguments, e.g. '100, 20'
- // print out arguments to command line
- IJ.log(args);
- IJ.log("starting plugin");
- /*
- Do something
- */
- return;
- }else{
- return;
- }
- }
- private void printTime(long startTime){
- // simple method to print the time since startTime
- IJ.log(hrs + " hour(s), " + min + " minute(s), and " + sec + " second(s).");
- }
- }