Macro code doesn't go through all if statements

Hello everyone,

I hope you can help me with a script I am having touble with.
I have a folder with a set of composite images, of which I would like to create a MAX intensity projection and only of selected channels, decide wether to enhance the brightness and contrast.
Everything works well, except that the code exists after checking the first channel (so it doesn’t go through the other else if statments)… what am I missing?

Below is the code that I tried:

Dialog.create("Choose of which channel to enhance contrast");
Dialog.addString("Change B&C of channel 1?", "no");
Dialog.addString("Change B&C of channel 2?", "no");
Dialog.addString("Change B&C of channel 3?", "no");
Dialog.addString("Change B&C of channel 4?", "no");

Dialog.show();
CH1 = Dialog.getString();
CH2 = Dialog.getString();
CH3 = Dialog.getString();
CH4 = Dialog.getString();


input = getDirectory(""); 
list = getFileList(input);
name = File.getName(input);
output = input + "Max-" + name + File.separator;
File.makeDirectory(output); 


setBatchMode(true); 
for (i = 0; i < list.length; i++)
	composite(input, output, list[i]);                    

setBatchMode(false);

function composite(input, output, filename) {
        open(input + filename);
		imageId=getImageID();
		
		run("Z Project...", "projection=[Max Intensity]");

		if (CH1 == "yes"){
		Stack.setChannel(1);
		run("Enhance Contrast", "saturated=0.35");}

		else if (CH2 == "yes"){
		Stack.setChannel(2);
		run("Enhance Contrast", "saturated=0.35");}
		
		else if (CH2 == "yes"){
		Stack.setChannel(3);
		un("Enhance Contrast", "saturated=0.35");}

		else if (CH2 == "yes"){
		Stack.setChannel(4);
	        run("Enhance Contrast", "saturated=0.35");}

		saveAs("tiff", output + getTitle);

        close();
}

Thanks a lot!

Dear @ananahana
It looks like your code has a couple of issues. For starters, it looks like you check the CH2 variable in all of your else if statement.

However, the main issue is your use of else if statements. The else if statements are linked to the first if statement, so if the if statement or any of the else if statements are true, then the rest of the else if statements will not execute.

You should change all of the else if statements to if statements.

Let me know if this helps.

1 Like

Thanks a lot! I had overlooked that I had checked only CH2 but it now works!! :slight_smile: