selectWindow Error in ImageJ/Fiji Macro

Hi, I am new to this Macro writing.

I am basically processing my images to split a channel, merge selected channels, and save a merged image.
After that, I am trying to convert a color of each channel into “Gray” in RGB color and save it as “tiff”.

However, Error message shows up at “selectWindow(“C2-MAX_”+title);” part, saying it cannot find this specific window.

Below is my code and thank you much for any feedback!

input = getDirectory("Choose an input directory");
output = getDirectory("Choose an output directory");

processFolder(input); 

function processFolder(dir) {
	list = getFileList(dir);
	for (i=0; i<list.length; i++) {
    	        if(endsWith(list[i], ".oir")) { //add the file ending for your images
    		    processFile(dir, output, list[i]);
		} else if(endsWith(list[i], "/") && !matches(output, ".*" + substring(list[i], 0, lengthOf(list[i])-1) + ".*")) {
   		    //if the file encountered is a subfolder, go inside and run the whole process in the subfolder
    		    processFolder(""+dir+list[i]);
		} else {
    		    //if the file encountered is not an image nor a folder just print the name in the log window
       		    print(dir + list[i]);
		}
    }
}

setBatchMode(true);

for(i=0;i<list.length;i++){
function processFile(inputFolder, output, file) {
	open(inputFolder + file);
	title = getTitle();
	
run("Z Project...", "projection=[Max Intensity]");
	run("Split Channels");

	three = "C3-MAX_" +title;
	two = "C2-MAX_" +title;
	one = "C1-MAX_" + title;
	
	selectWindow("C3-MAX_"+title);
	run("Subtract Background...", "rolling=100")
	
	
	**selectWindow("C2-MAX_"+title); // ERORR!!!!**
	run("Subtract Background...", "rolling=100")
	
	run("Merge Channels...", "c1=["+three+"] c2=["+two+"] c3=["+one+"]create keep");
	run("RGB Color");
	run("Scale Bar...", "width=50 height=5 font=25 color=White background=None location=[Lower Right] bold hide")
	
	saveAs("Tiff",output+"Merge-"+title+lise[i]);

	selectWindow("C3-MAX_"+title+list[i]);
	run("Grays");
	run("RGB Color")
	saveAs("Tiff", output+"C3-Gr-"+title+list[i]);
	
	selectWindow("C2-MAX_"+title);
	run("Grays");
	run("RGB Color")
	saveAs("Tiff", output+"C2-Gr-"+title+list[i]);
	
	close(file);
}
	}
setBatchMode(false);
run("Close");type or paste code here
1 Like

Hey @FiJi1,

could you please turn the batch mode off, e.g. with this line:

 setBatchMode(false);

You will then see how the windows are called and why it can’t find the specified one.

Let us know if this helps!

Cheers,
Robert

Thank you @haesleinhuepf.

I did turn off the batch mode but after that I was encountering other issue with semicolon.
Apparently, the other issue was I haven’t put ‘;’ for few lines, so once i added it for lines, it fixed the problem.

For other people, I will put my whole complete macro.

close("*")
input = getDirectory("Choose an input directory");
output = getDirectory("Choose an output directory");
processFolder(input); 
function processFolder(dir) {
	list = getFileList(dir);
	for (i=0; i<list.length; i++) {
    	        if(endsWith(list[i], ".oir")) { //add the file ending for your images
    		    processFile(dir, output, list[i]);
		} else if(endsWith(list[i], "/") && !matches(output, ".*" + substring(list[i], 0, lengthOf(list[i])-1) + ".*")) {
   		    //if the file encountered is a subfolder, go inside and run the whole process in the subfolder
    		    processFolder(""+dir+list[i]);
		} else {
    		    //if the file encountered is not an image nor a folder just print the name in the log window
       		    print(dir + list[i]);
		}
    }
}
for(i=0;i<list.length;i++){
function processFile(inputFolder, output, file) {
	open(inputFolder + file);
	title = getTitle();
		run("Z Project...", "projection=[Max Intensity]");
	run("Split Channels");
	three = "C3-MAX_" +title;
	two = "C2-MAX_" +title;
	one = "C1-MAX_" + title;
	selectWindow("C3-MAX_"+title);
	run("Subtract Background...", "rolling=100");
	//run("Brightness/Contrast...");
	selectWindow("C2-MAX_"+title);
	run("Subtract Background...", "rolling=100");
	run("Merge Channels...", "c1=["+three+"] c2=["+two+"] c3=["+one+"]create keep");
	run("RGB Color");
	run("Scale Bar...", "width=50 height=5 font=25 color=White background=None location=[Lower Right] bold hide");
	saveAs("Tiff",output+"Merge-"+title+list[i]);
	selectWindow("C3-MAX_"+title);
	run("Grays");
	run("RGB Color");
	saveAs("Tiff", output+"C3-Gr-"+title+list[i]);
		selectWindow("C2-MAX_"+title);
	run("Grays");
	run("RGB Color");
	saveAs("Tiff", output+"C2-Gr-"+title+list[i]);
	close(file);
}
	}
1 Like