How to use macro to run a plugin function

I would like to help a user run the Pet-Ct plugin inside an ImageJ macro.
More specifically, a plugin is running and I want to be able to use a function in it to return a value to the macro. I’ve made some progress but I’m still not sure it is possible to do what I need.

Part of the code is:

progs = getList("window.titles");
var done = false;
for(i=0; i<progs.length && !done; i++) {
	pname = progs[i];
	print("    "+pname);
	if(startsWith(pname,"Read Studies")) {
		done = true;
	}
}
if(done) {
	win = selectWindow(pname);
//	path = win.getCDButton(1);
	win.dispose();
}

The macro picks up the running programs and searches for the plugin within which I want to run a function. I chose an existing function and want to see what it returns.
Before that I decided to do something more basic and see if I can close the plugin by using the dispose function.
The macro fell over sooner than expected at win = selectWindow(pname);
It complained that it wanted a numeric input instead of a string input (the name of the window). This is surprising as the documentation mentions a string name as an input and not a number.

In any case the question is: can I get a given running plugin and operate a function within it? Should win.getCDButton(1); return something if “win” is referring to my plugin?

If I am doing something wrong, please point me to the source of the error.

Thanks,
Ilan

Hi,
you can not do this from the macro language. It is interpreted by ImageJ and does not support objects as in win.dispose() also I think selectWindow does not have a return value. You can call static methods from a macro using the call-function, see: https://imagej.nih.gov/ij/developer/macro/functions.html

You can access the functions of a plugin or a library by using one of the scripting languages (groovy, javascript, jython, …).

Best regards,
Volker

Hi Volker,
First of all, thanks for the reply. I noticed that selectWindow is a void function, so I gave up on macros and started to use groovy.

import ij.IJ;
import ij.Window.Manager;

def main() {
	autoRunDirectory = IJ.getDirectory("imagej") + "/plugins/Scripts/Plugins/AutoRun/";
	println(autoRunDirectory);
//	pname WindowManager.getWindow('Read CD');
	progs = IJ.getList('window.titles');
var done = false;
for(i=0; i<progs.length && !done; i++) {
	pname = progs[i];
	print("    "+pname);
	if(startsWith(pname,"Read Studies")) {
		done = true;
	}
}

}

main()

In spite of my efforts to read the documentation and follow the examples, I am having a very difficult time trying to do the first part of the macro. autoRunDirectory works fine and println gives the correct result but I am unable to get the window manager list of non image objects. It doesn’t like my
progs = IJ.getList('window.titles');
which did work in the macro version.

I think groovy will give me the object I need, but I am stick stuck on the syntax of the commands,. I would be most grateful for any suggestions.

Thanks,
ilan

1 Like

The IJ class doesn’t have a method getList. Try WindowManager instead:

import ij.WindowManager
nonImageWindows = WindowManager.getNonImageTitles()
imageWindows = WindowManager.getImageTitles()

Just to finish up, here is a working example of the above script.
https://sourceforge.net/p/bifijiplugins/wiki/Groovy%20scripting%20example/

It includes links 2 videos where the script is explained in detail