Image J Macro error message " ';' expected in line 16"

Hi all,

This is my first experience using image J macro coding (or any coding) but I’m loving the efficiency of pressing a few buttons and generating a ton of data.

I’m working on a batch processing macro that will be used on a directory of subFolders, which combine single file .tiffs that will be compiled into a multi-channel z-stack .tiff file. My idea is to code a macro to run a batch process on a directory, to open individual subFolders and open all of the “.tif” files in the subFolder, combine tifs into a stack and combine channels, and finally save as a file in an output directory. But I keep running into the problem of figuring out how to make the macro loop a loop, if you know what I mean.

Below is my code so far:

input = getDirectory(“Input directory”);
output = getDirectory("Choose Destination Directory ");

suffix = “.tif”; //you only want to apply to tiff images, no need to ask

processDirectory(input);

function processDirectory(input) {

list = getFileList(input);
for (i = 0; i < lengthOf(list); i++) {
	
	if(File.isDirectory(input + list[i])) {
		
	filelist = getFileList(input + list[i]) 
		for (i = 0; i < lengthOf(filelist); i++) {
			if (endsWith(filelist[i], suffix);) { 
    open(input + File.separator + filelist[i]);
		}
	}

filename = File.getNameWithoutExtension(input);
run(“Images to Stack”, “name=C1 title=C003 use”);
run(“Images to Stack”, “name=C2 title=C002 use”);
run(“Images to Stack”, “name=C3 title=C001 use”);
run(“Merge Channels…”, “c1=C1 c2=C2 c3=C3”);
saveAs(“TIFF”, output + filename);
close();

	}
}

}

Currently, the error message that pops up when I hit run says there is a “;” missing in line 16. Can someone help me with this particular issue, give advice on how to write a macro that will accomplish my goal here, or provide any ideas that would be a better approach than the one I’m taking?

Best,
Kadden

1 Like

Hi Kadden,

I see four minor issues: After the if(endswith... should be no ; within the brackets. Furthermore, you shouldn’t use the variable i in both for-loops. Try using i and j. The open command tries to open a file from the subfolder in the main folder. You need to add list[i] in there.
Last but not least, the indentation is a bit off and it’s hard to see if all opened curly brackets are also closed. Can you check that as well?

Let us know if this help.

Best,
Robert

1 Like

Hi Robert,

I did the following:

  • removed the ; in the brackets
  • replaced “i” with “j” in the nested loop
  • added “list[i]” to the open command
  • and checked all curly brackets to make sure they were all closed

Here is my updated code:

input = getDirectory(“Input directory”);
output = getDirectory("Choose Destination Directory ");
suffix = “.tif”; //you only want to apply to tiff images, no need to ask
processDirectory(input);
function processDirectory(input) {
list = getFileList(input);
for (i = 0; i < lengthOf(list); i++) {
if(File.isDirectory(input + list[i])) {
filelist = getFileList(input + list[i])
for (j = 0; j < lengthOf(filelist); j++) {
if (endsWith(filelist[j], suffix)) {
open(input + list[i] + filelist[j]);
}
}
filename = File.getNameWithoutExtension(input);
run(“Images to Stack”, “name=C1 title=C003 use”);
run(“Images to Stack”, “name=C2 title=C002 use”);
run(“Images to Stack”, “name=C3 title=C001 use”);
run(“Merge Channels…”, “c1=C1 c2=C2 c3=C3”);
saveAs(“TIFF”, output + filename);
close();
}
}
}

When I run it, I still get the same message

‘;’ expected in line 16:
(called from line 6)
< for> ( j = 0 ; j < lengthOf ( filelist ) ; j ++ ) {

This doesn’t make sense to me, as the for is a loop and not a single step command. Any more thoughts on this?

KK

1 Like

Ah, because there is a semicolon missing by the end of the line before.
Regarding the indentation: You removed it. But actually, it’s meant to help you reading the code. After every { you indent deeper and after every } you return to the former level. Check out that tutorial to learn why this is useful, long-term:

Does the code work with this added ; ?

Cheers,
Robert

1 Like

Robert!

Yes, it works!
I’m so grateful for your help.

Also, re: indention - I cannot figure out how to paste my coding into this forum as a quote and also includes my indentation. So, the first post where I pasted my code looks sloppy and the second time, I tried to keep it all together but there was no indention. I agree, intention is super helpful.

Thanks again!

Cheers,
Kadden

1 Like

It looks like you did not use code-formatting, which you can get by highlighting your text and using
image
in the future.