Importing libraries in ImageJ ActionBar macros

I’m using ActionBar to create ImageJ macros, and I’m having trouble creating a macro to manipulate ROIs.

I have .txt files that each contain the x and y coordinates for a polygon ROI. The slice of the polygon is at the beginning of the file name. I’d like to import the ROI files and then use Roi.setPosition to associate each ROI with the appropriate slice, but it looks like libraries cannot be imported in the ActionBar .ijm files. I tried using using setPosition using “call”, but that isn’t working either.

Below is the macro I’m working on, with the lines that aren’t working properly commented out.
Any help would be greatly appreciated!
Thanks,
Jenine

<button> open_folder
label=Open folder
icon=noicon
arg=<macro>

	folderdir = getDirectory("Choose a Directory");
	Dialog.create("Choose a folder with one AVI file and labels");
	filelist = getFileList(folderdir);

	//Opens AVI file
	avifile = "";

	for(i=0; i<filelist.length; i++) {
		if(endsWith(filelist[i], ".avi")) {
			avifile = filelist[i];
		}
	}
	open(folderdir + avifile);

	//Opens .txt ROI files
	for(i=0; i<filelist.length; i++) {
		if(endsWith(filelist[i], ".txt")) {
			run("XY Coordinates... ", "open=[" + folderdir + filelist[i] + "]");

			roiManager("add");
			roiManager("select", (roiManager("count") - 1));
			
			//Roi.setName("newname");
			
			//roi = (call("ij.plugin.frame.RoiManager.getRoi", 0));
			//roi.setName("newname");
			
			//call("ij.gui.Roi.setPosition", 5);
			
			roiManager("Update");
			print(call("ij.plugin.frame.RoiManager.getName", 0));
		}	
	}
		
</macro>

Hi @jenine,

There are a couple of issues in your script. I think I can interpret what you’re trying to achieve, and can provide you with a corrected version. The only thing that I’m not sure about is this:

Does the filename contain only the slice number (like “56.txt”) or is there something else between the slice number and the extension?

In the first case, this corrected script should do the trick:

<button> open_folder
label=Open folder
icon=noicon
arg=<macro>

	folderdir = getDirectory("Choose a folder with one AVI file and labels");
	filelist = getFileList(folderdir);
	
	//Opens AVI file
	avifile = "";
	
	for(i=0; i<filelist.length; i++) {
		if(endsWith(filelist[i], ".avi")) {
			avifile = filelist[i];
		}
	}
	open(folderdir + avifile);
	
	//Opens .txt ROI files
	for(i=0; i<filelist.length; i++) {
		if(endsWith(filelist[i], ".txt")) {
			run("XY Coordinates... ", "open=[" + folderdir + filelist[i] + "]");
			pos=replace(filelist[i], ".txt", "");
			run("Properties... ", "position=&pos");
			roiManager("add");
			print(filelist[i]);
		}	
	}
		
</macro>

Cheers,
Nico