Ugly Macro help - Splitting multi channel images and combining into stack made up of images of one colour

Posted by lmurphy on
URL: http://imagej.273.s1.nabble.com/Ugly-Macro-help-Splitting-multi-channel-images-and-combining-into-stack-made-up-of-images-of-one-colr-tp5018408.html

Someone asked me to write a macro that does the below. I know I'm doing it in a very inelegant fashion (especially including the fact that I save and delete files). I was wondering if anyone could help me see where it could be done in a better way? Thank you in advance.

Input is a number of multi-channel images
The desired output is one saved tiff stack for each of the channels.

setBatchMode(true);
dir=getDirectory("Choose an Input Directory");
print(dir);
list = getFileList(dir);
saveloc = getDirectory("Choose Ouput Directory");

for (i=0; i<list.length; i++) {
if (endsWith(list[i], ".tif")){
print(i + ": " + dir+list[i]);
open(dir+list[i]);
imgName=getTitle();
run("Split Channels");
selectWindow("C1-"+imgName);
saveAs("Tiff", saveloc + "C1-"+imgName);
close();
selectWindow("C2-"+imgName);
saveAs("Tiff", saveloc + "C2-"+imgName);
close();
selectWindow("C3-"+imgName);
saveAs("Tiff", saveloc + "C3-"+imgName);
close();
run("Close All");
} else {
}
}

run("Image Sequence...", "open=[saveloc] file=C1 use");");
saveAs("Tiff", saveloc+"FullStackC1.tif");
run("Close All");

run("Image Sequence...", "open=[saveloc] file=C2 use");");
saveAs("Tiff", saveloc+"FullStackC2.tif");
run("Close All");

run("Image Sequence...", "open=[saveloc] file=C3 use");");
saveAs("Tiff", saveloc+"FullStackC3.tif");
run("Close All");

list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (startsWith(list[i], "C"))
File.delete(dir+list[i]);
}

Dialog.create("Progress");
Dialog.addMessage("Complete!");
Dialog.show;

setBatchMode(false);