Need to make multi channel max projected Zstack from multiple folders

So, so far i have been trying to stitch together code that i have found online but i’m missing basic logic. But basically, this is how my folders are laid out:


NPYGFP#1 > //this is the animal name
  NPYGFP#1-1 20X EE1.12 // example of one hippocampal brain slice and there are usually 2-5 slices per animal, in this case 3. 
  NPYGFP#1-2 20X EE1.12
  NPYGFP#1-3 20X EE1.12 >
    XY01 //one area imaged specifically on said slice (usually 
                4-5 images for one slice)
    XY05 >
      NPYGFP#1-1 _XY01_Z001_Overlay.tif
      NPYGFP#1-1 _XY01_Z002_Overlay.tif
      // each image folder contains Zslice images in 3 different channels along with an overlay, the overlay can be ignored. 

So far i have been using the code below to get each channel in a stack, then taking that stack and max projecting it. Then once i have three max projected channels (CH1, CH2,CH3) I use “Image to Stack” and then save it to a specific location.

macro "Macro 1 [q]" {

run("Image Sequence...", "open=/Users/me/Desktop/NPYGFP#1/NPYGFP#1-1_20X_EE1.12/XY04 number=11 starting=2 increment=4 sort");
run("Z Project...", "projection=[Max Intensity]");

run("Image Sequence...", "open=/Users/me/Desktop/NPYGFP#1/NPYGFP#1-1_20X_EE1.12/XY04 number=11 starting=3 increment=4 sort");
run("Z Project...", "projection=[Max Intensity]");

run("Image Sequence...", "open=/Users/me/Desktop/NPYGFP#1/NPYGFP#1-1_20X_EE1.12/XY04 number=11 starting=4 increment=4 sort");
run("Z Project...", "projection=[Max Intensity]");

run("Images to Stack", "name= " + getTitle + "title=[] use");
saveAs("Tiff", "/Users/me/Desktop/Results/NPYGFP #1/NPYGFP#1-1 _XY04_MAX.tif");

This code is nice but I have to change the path and slice (XY0n) a lot and i cant seem to get the name of the path to be used as the final image name output – which should be three max projected channels in one stack. Using the path name is nice cause all the info is there for data organizational purposes.

This code, I feel, has a lot of what I need and I have been playing around with it but I have been stuck.

//-----from -------
* Macro template to process multiple images in a folder


//This allows you to select input and output directories

#@ File (label = "Input directory", style = "directory") input

#@ File (label = "Output directory", style = "directory") output

#@ String (label = "File suffix", value = ".tif") suffix

// See also for a version of this code

// in the Python scripting language.


// function to scan folders/subfolders/files to find files with correct suffix

function process_folder(input) {

list = getFileList(input);

list = Array.sort(list);

for (i = 0; i < list.length; i++) {

if(File.isDirectory(input + File.separator + list[i]))

process_folder(input + File.separator + list[i]);

if(endsWith(list[i], suffix))

process_file(input, output, list[i]);



function process_file(input, output, file) {

print("Processing: " + input + File.separator + file);

path = input + File.separator;

full_path = path + file;

open(full_path); //the image is now open

//Set up variables

title = getTitle();

new_title = "MAX" + title;

start_stack_array = newArray(0,6,11);

stop_stack_array = newArray(5,10,15);

//Create three different z stacking

for(index=0; index<start_stack_array.length;index++){


run("Z Project...", "start = " + start_stack_array[index]+ "stop = " + stop_stack_array[index] + " projection=[Max Intensity]");

tag = "[" + start_stack_array[index] + "-" + stop_stack_array[index] + "stop=" + stop_stack_array[index] + " projection=[Max Intensity]");

stack_file_name = title + tag;


run("Split Channels");

run("Merge Channels...", "c1=C1-" +stack_file_name + " c2=C2-" +stack_file_name);

run("RGB Color");

rgb_title = new_title + tag;


saveAs("tiff", path + rbg_title); // This is how you save an active picture



close();//The image is now closed

print("Saving to: " + output);


Choosing the directory is something I have tried. I have tried this by nest the command in Image Sequence, but in vain. Also the newArray function above is not something I think I need (?) but instead tried putting in the Image Sequence function there. This is all out of order and my logic does not flow!
Thank you in advanced for any help you can offer!