Macro multiply mask to stack

Sample image and/or code

TEST-1.tif (3.0 MB)

Background

Hello,
I am adapting a macro to do some analysis such as background subtraction, filtering but I want to. create a mask from one channel (channel3) and multiply to the stack and save.

I have not figure it out how to call the image that I am merging multiply with the channel 3.
Please, see figure attached and the code.

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], ".tif")) { //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]);
		}
    }
}


function processFile(inputFolder, output, file) {
	open(inputFolder + file);
	title = getTitle();
	run("Split Channels");
	three = "C4-" +title;
	two = "C3-" +title;
	one = "C2-" + title;
	run("Merge Channels...", "c1=["+one+"] c2=["+two+"] c3=["+three+"] create");
	run("ROI Manager...");
	//setTool("rectangle");
	waitForUser("Draw ROI, then hit OK");  
	
	if (selectionType==-1)
		exit("This macro requires an area selection");
	for (j=1; j<=nSlices; j++) { // loop through slices in image
		setSlice(j);
		getStatistics(area, mean);
		run("Select None");
		run("Subtract...", "value="+mean);
		run("Median...", "radius=1.0 stack");  
	    run("Restore Selection"); 
		        
	}
	saveAs("tiff", output + file);
	close(file);
run("Close All"); 	
}

Analysis goals

I want to be able to use this pieces of instruction that I have in another macro but it is written in a way that I am not able to combine with the code above.

selectWindow("C3-" + list[ i ]);
run("Duplicate...", "title=bodiemask");
setAutoThreshold("Li dark no-reset");
run("Threshold...");
call("ij.plugin.frame.ThresholdAdjuster.setMode", "Over/Under");
    waitForUser("Imagen "+n+" de "+length,"Apply Threshold y click OK");
    run("Subtract...", "value=254");
    saveAs("Tiff", path + "output/Bodiemask" + i);
imageCalculator("multiply create 32-bit", "Bodiemask", "HERE I WANT TO PUT MY MERGE3CHANNEL+i+".tif")

Challenge

Can someone give me some guide to continue improving this macro, please?
I would really appreciate.

Thanks in advance

I tried to write it in another way but I have problem with the multiplication.

path    = getDirectory("Choose a Directory");
  list    = getFileList(path);
  length  = list.length;

  File.makeDirectory(path + "imaging/");

  //Calculation

  for (i=0; i<length; i++) {
             
    n=i+1;
    open(path+list[i]);
    img = File.nameWithoutExtension();
    selectWindow(img+".tif");
    run("StackReg", "transformation=Translation");
     //setTool("rectangle");
    waitForUser("add ROI, Draw ROI, then hit OK");    
      if (selectionType==-1)
         exit("This macro requires an area selection");
      for (k=1; k<=nSlices; k++) { // loop through slices in image
          setSlice(k);
          getStatistics(area, mean);
          sum += mean;
          run("Select None");
          run("Subtract...", "value="+mean);
          run("Restore Selection");
          }
         
run("Median...", "radius=1");
run("Split Channels");
selectWindow("C1-" + list[ i ]);
run("Duplicate...", "title=bodiemask");
setAutoThreshold("Li dark no-reset");
run("Threshold...");
call("ij.plugin.frame.ThresholdAdjuster.setMode", "Over/Under");
    waitForUser("Imagen "+n+" de "+length,"Apply Threshold y click OK");
    run("Subtract...", "value=254");
    saveAs("Tiff", path + "imaging/bodiemask" + i);
imageCalculator("Multiply create 32-bit", "C1-" + list[ i ],"bodiemask"+i+".tif");
imageCalculator("Multiply create 32-bit","C2-" + list[ i ],"bodiemask"+i+".tif");
imageCalculator("Multiply create 32-bit","C3-" + list[ i ],"bodiemask"+i+".tif");
imageCalculator("Multiply create 32-bit","C4-" + list[ i ],"bodiemask"+i+".tif");
run("Merge Channels...", "c1=[" + list[i] + "] c2=[" + list[i] + "] c3=[" + list[i] + "] c4=[" + list[i] + "] create");
 saveAs("Tiff", path + "imaging/_merge" + i);
  run("Close All");
}

Still I am looking for solution.

I realise that in the second macro I should put Result of C1_ , etc but I am not able to find the merge configuration even with that fix.

Ok, I find the solution
is run(“Merge Channels…”," c1=["+“Result of C1-” + list[i]+"] c2=["+“Result of C2-” + list[i]+"] create");

That is for the second macro but still I want to do it with the first one