'Analyze particles' summary doesn't append

Hey everyone,

I need a bit of help with the macro I wrote and use to analyze tumor infiltration of PMNs and DCs. It works fine in and of itself, the problem I have is with saving the results in the desired format.
I use ‘run(“Analyze Particles…”, “size=2-15.00 summarize slice”);’ to count the amount of infiltrating cells for each file in the folder. But it then makes a new window for each summary, instead of appending them to eachother in a single, saveable window. I want one saveable window and not fifty. But for the life of me, I can’t get it to work.
The only working option that I have now is to display the analysis in the ‘results’ window and then count how many entries it has to know how many cells it counted. Which is tedious and time-consuming if you analyze more than 3 files.
Any and all tips will be appreciated!

Here’s the macro:

////////////////////////////////////CREDIT////////////////////////////////////
//Written by Drs. Dennis Y. Gout,
//PhD Candidate in the group of Prof. Dr. Marjolein van Egmond, VUmc Amsterdam
//
//First version: 	v1.0, 08-05-2019
//Current version: 	v1.4, 07-06-2019
//
//For the purpose of quickly and objectively analyzing tumor infiltration by dendritic cells (DCs) and 
//polymorphonuclear cells (PMNs) in sections stained for DCs and PMNs. This macro measures surface area or counts number of infiltrating cells.

////////////////////////////////////FEATURES////////////////////////////////////
// - Manual or automated tumor marking based on fluorescence
// - Automated infiltrate measurement using custom thresholds
// - Logging of settings and data
// - Surface area and particle analysis

////////////////////////////////////CHANGELOG////////////////////////////////////
//v1.0, 08-05-2019 - First version
//v1.1, 09-05-2019 - Added Logging + ROI saving
//v1.2, 22-05-2019 - Fixed 'no selection in image' bug, added automated tumor marking
//v1.3, 24-05-2019 - Rewired looping statements, added nucleus recognition and particle analysis
//v1.4, 07-06-2019 - Added channel setup

////////////////////////////////////SETTINGS////////////////////////////////////
Version="v1.4"

//Tumor marking + measurement settings
rows = 3;
columns = 1;
n = rows*columns;
label1 = "Auto threshold fluorescent marking";
label2 = "Manual threshold fluorescent marking";
label3 = "Mark tumor by hand";
labels = newArray(label1, label2, label3);
item1 = "Measure surface area";
item2 = "Particle analysis";
items = newArray(item1, item2);
default = "Particle analysis";
label4 = "In which channel is your PMN signal?"
label5 = "In which channel is your DC signal?"
label6 = "In which channel is your Nucleus signal?"
label7 = "In which channel is your Tumor signal?"
defaults = newArray(n);
for (i=0; i<n; i++) {
    defaults[i] = false;    
}
  
Dialog.create("Tumor marking method");
Dialog.addMessage("Do you want to use automated tumor marking? Pick only one.");
Dialog.addCheckboxGroup(rows, columns, labels, defaults);
Dialog.addChoice("What do you want to measure?", items, default);
Dialog.addNumber(label4, 1);
Dialog.addNumber(label5, 2);
Dialog.addNumber(label6, 3);
Dialog.addNumber(label7, 4);
Dialog.show();
Auto_Fluo = Dialog.getCheckbox();
Man_Fluo = Dialog.getCheckbox();
Mark_Hand = Dialog.getCheckbox();
Measurement = Dialog.getChoice();
PMN_chan = Dialog.getNumber();
DC_chan = Dialog.getNumber();
Nuc_chan = Dialog.getNumber();
Tum_chan = Dialog.getNumber();

if (Man_Fluo==1) {
	Auto_Tum_Mark = "Manual fluorescence threshold";
} else {
	if (Auto_Fluo==1) {
		Auto_Tum_Mark = "Automatic fluorescence threshold";
		} else {
			Auto_Tum_Mark = "Manual tumor marking";
			} 
}			

//Opens your image folder
path = getDirectory("Choose a Directory");
file_list = getFileList(path);
file_number = lengthOf(file_list);
i=0;

open(path+file_list[i]);
first_file_name = getInfo("image.filename");
file_length = lengthOf(File.name);

close(first_file_name);

run("Bio-Formats Windowless Importer");
run("Out [-]");
run("Bio-Formats Macro Extensions");

//Determines manual threshold inputs
if (Man_Fluo==1) {
	Stack.setChannel(Tum_chan);
	run("Threshold...");
	run("Enhance Contrast", "saturated=0.35");
	waitForUser("Set tumor Threshold", "Manually determine your tumor threshold and click 'Ok'");
	run("Close");
	getThreshold(Tum_thres_min, Tum_thres_max);
}

if (Measurement=="Particle analysis") {
	Stack.setChannel(Nuc_chan);
	run("Threshold...");
	run("Enhance Contrast", "saturated=0.35");
	waitForUser("Set nucleus Threshold", "Manually determine your nucleus threshold and click 'Ok'");
	run("Close");
	getThreshold(Nucleus_Lower, Nucleus_Upper);
}

Stack.setChannel(PMN_chan);
run("Threshold...");
run("Enhance Contrast", "saturated=0.35");
waitForUser("Set PMN Threshold", "Manually determine your PMN threshold and click 'Ok'");
run("Close");
getThreshold(PMN_thres_min, PMN_thres_max);

Stack.setChannel(DC_chan);
run("Threshold...");
run("Enhance Contrast", "saturated=0.35");
waitForUser("Set DC Threshold", "Manually determine your DC threshold and click 'Ok'");
run("Close");
getThreshold(DC_thres_min, DC_thres_max);
close();

//Logs general info/settings
print("////////General Information and Settings////////");
print("Macro version: "+Version);
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
start_time=getTime();
print("Date: "+dayOfMonth+"-"+month+"-"+year);
print("Time: "+hour+":"+minute+":"+second);
print("File directory: "+"\n"+path);
print("Number of files: "+file_number+"\n");
print("Method of tumor marking: "+Auto_Tum_Mark);
print("Measuring: "+Measurement+"\n");
if (Man_Fluo==1) {
	print("Tumor Threshold:"+"\n"+"     Minimum: "+Tum_thres_min+"\n"+"     Maximum: "+Tum_thres_max);
}
if (Measurement=="Particle analysis") {
	print("Nucleus Threshold:"+"\n"+"     Minimum: "+Nucleus_Lower+"\n"+"     Maximum: "+Nucleus_Upper);
}
print("PMN Threshold:"+"\n"+"     Minimum: "+PMN_thres_min+"\n"+"     Maximum: "+PMN_thres_max);
print("DC Threshold:"+"\n"+"     Minimum: "+DC_thres_min+"\n"+"     Maximum: "+DC_thres_max+"\n");
print("////////Individual Settings////////");
File.makeDirectory(path+"\\ROIs");
ROIpath=path+"\\ROIs\\";

////////////////////////////////////LOOP////////////////////////////////////
//Loop function
do {
	open(path+file_list[i]);
	run("Out [-]");
	Analyze();
	i=i+1;
} while(i<file_number);	

////////////////////////////////////FUNCTIONS////////////////////////////////////
function Analyze() {
//Logs filename
Filename=getInfo("image.filename");
Filename_excl_ext=File.nameWithoutExtension;

print("Image Filename: "+Filename);

//Determines tumor regions
if (Mark_Hand==1) {
	Stack.setChannel(Tum_chan);
	run("Enhance Contrast", "saturated=0.35");
	setTool("polygon");
	waitForUser("Tumor marking", "Mark the tumor area and click 'Ok'");
	roiManager("add");
} else {
	if (Auto_Fluo==1) {
		Stack.setChannel(Tum_chan);
		setAutoThreshold("Default dark");
		run("Create Selection");
		run("Enlarge...", "enlarge=20");
		run("Enlarge...", "enlarge=-20");
		roiManager("add");
	} else {
		Stack.setChannel(Tum_chan);
		setThreshold(Tum_thres_min, Tum_thres_max);
		run("Create Selection");
		run("Enlarge...", "enlarge=20");
		run("Enlarge...", "enlarge=-20");
		roiManager("add");
	}
}

//Determines PMN content
Stack.setChannel(PMN_chan);
setThreshold(PMN_thres_min, PMN_thres_max);
run("Create Selection");
Selection = selectionType();
if (Selection==-1) {
	makeRectangle(0, 0, 1, 1);
	roiManager("add");
} else {
	run("Enlarge...", "enlarge=2.5");
	run("Enlarge...", "enlarge=-2.5");
	roiManager("add");
}

//Determines DC content
Stack.setChannel(DC_chan);
setThreshold(DC_thres_min, DC_thres_max);
run("Create Selection");
Selection = selectionType();
if (Selection==-1) {
	makeRectangle(0, 0, 1, 1);
	roiManager("add");
} else {
	run("Enlarge...", "enlarge=2.5");
	run("Enlarge...", "enlarge=-2.5");
	roiManager("add");
}
	
//Seperates ROI's inside of the tumor area
roiManager("Select", newArray(0,1));
roiManager("AND");
Selection_Tumor_DC = selectionType();
if (Selection_Tumor_DC==-1) {
	roiManager("Select", newArray(0,2));
	roiManager("AND");
	Selection_Tumor_PMN1 = selectionType();
	if (Selection_Tumor_PMN1==-1) {
		roiManager("save", ROIpath+Filename_excl_ext+"_ROIs.zip");
		print("Both no selection"+"\n");
	} else {
		roiManager("add");
		roiManager("deselect")
		roiManager("select", 2);
		roiManager("delete");
		roiManager("save", ROIpath+Filename_excl_ext+"_ROIs.zip");
		print("No DC selection, PMN selection"+"\n");
	}
} else {
	roiManager("add");
	roiManager("Select", newArray(0,2));
	roiManager("AND");
	Selection_Tumor_PMN2 = selectionType();
	if (Selection_Tumor_PMN2==-1) {
		roiManager("select", 1);
		roiManager("delete");
		roiManager("save", ROIpath+Filename_excl_ext+"_ROIs.zip");
		print("DC selection, no PMN selection"+"\n");
	} else {
		roiManager("add");
		roiManager("select", newArray(1, 2));
		roiManager("delete");
		print("DC and PMN selection"+"\n");
	}
}

//Particle analysis
if (Measurement=="Particle analysis") {
	Stack.setChannel(Nuc_chan);
	setThreshold(Nucleus_Lower, Nucleus_Upper);
	run("Make Binary", "method=Default background=Default calculate black");
	run("Watershed", "slice");
	setAutoThreshold("Default dark");
	run("Create Selection");
	roiManager("add");
	roiManager("Select", newArray(1,3));
	roiManager("AND");
	Selection_Nucleus_PMN = selectionType();
	if (Selection_Nucleus_PMN==-1) {
		makeRectangle(0, 0, 1, 1);
		roiManager("add");
	} else {
		roiManager("add");
	}
	roiManager("deselect");
	roiManager("Select", newArray(2,3));
	roiManager("AND");
	Selection_Nucleus_DC = selectionType();
	if (Selection_Nucleus_DC==-1) {
		makeRectangle(0, 0, 1, 1);
		roiManager("add");
	} else {
		roiManager("add");
	}
	roiManager("deselect");
	roiManager("Select", newArray(0,3));
	roiManager("AND");
	Selection_Tot_Cells = selectionType();
	if (Selection_Tot_Cells==-1) {
		makeRectangle(0, 0, 1, 1);
		roiManager("add");
	} else {
		roiManager("add");
	}
	roiManager("Select", newArray(1,2,3));
	roiManager("delete");
	roiManager("save", ROIpath+Filename_excl_ext+"_ROIs.zip");
	run("Set Measurements...", "area display redirect=None decimal=0");
	roiManager("select", 3);
	roiManager("measure");
	run("Analyze Particles...", "size=2-15.00 summarize slice");
	roiManager("select", 1);
	roiManager("measure");
	run("Analyze Particles...", "size=2-15.00 summarize slice");
	roiManager("select", 2);
	roiManager("measure");
	run("Analyze Particles...", "size=2-15.00 summarize slice");
	roiManager("reset");
	run("Close All");
} else {
	//Measures ROI's
	run("Set Measurements...", "area display redirect=None decimal=0");
	roiManager("deselect");
	roiManager("save", ROIpath+Filename_excl_ext+"_ROIs.zip");
	roiManager("measure");
	roiManager("reset");
	run("Close All");
}
}

////////////////////////////////////FINISH////////////////////////////////////
//Save function
if(i==file_number){
	run_time=round((getTime()-start_time)/1000);
	File.makeDirectory(path+"\\Results");
	save_path = path+"\\Results\\";
	saveAs("Results",save_path+"Results.xls");
	print("\n"+"////////Finish////////"+"\n"+"Save Directory: "+"\n"+save_path);
	print("\n"+"Finished analyzing "+file_number+" files in "+run_time+" seconds.");
	selectWindow("Log");
	saveAs("Text",save_path+"Log.txt");
	run("Close");
	selectWindow("Results");
	run("Close");
	selectWindow("ROI Manager");
	run("Close");
}

//Exit
showMessage("Finished analyzing "+file_number+" files in "+run_time+" seconds.");
exit()