http://imagej.273.s1.nabble.com/macro-batch-mode-problem-tp5003989p5004018.html
> Im currently writing my first real macro which runs fine in normal mode but not batch mode. The macro is pasted below. Essentially it first asks whether there is a brightfield image in the stack or not. Based on the number of channels in the stack and the response to the aforementioned question it runs one of four blocks of code. If I open multiple 3 or 4 channel stacks lacking a brightfield image and i run this macro and uncheck the tick box at the start and push ok it works fine. However, if i add setBatchMode(true); at the top of the macro it fails. It makes an attempt at the first stack but never gets beyond this generally failing with stack slicer saying it needs a stack. I have a suspicion the problem lies somewhere in one of the close(); statements closing one of my stacks before it has been processed when in batch mode. Have I done something wrong??? Im learning as i write basically.
Modify the macro so that it opens the stacks to be processed when the macro is in batch mode. Before opening and processing a stack, the macro should call run("Close All") to insure no images are open. The basic structure should be similar to the following macro, which z-projects all the stacks in a folder, saving the projections in another folder.
> I also had to remove all print functions with // otherwise if the log window was closed and i ran the code it would fail every time. However, if the log window was open it did not fail. Not sure why this is.
> I did try and write something simple to highlight the problem but i cant figure it out !
> Any help would be appreciated.
> Thanks
> Mike
>
>
> setBatchMode(true);
> Dialog.create("Mikes macro"); Dialog.addCheckbox("BF image in stack?", true) Dialog.show(); BFimage = Dialog.getCheckbox(); dir = getDirectory("Choose a Directory");
> ids=newArray(nImages); for (i=0;i<nImages;i++) { selectImage(i+1); title = getTitle; //print(title); ids[i]=getImageID; getDimensions(w, h, channels, slices, frames);
> if (BFimage==false && channels==3) { //print("3 fluorescent channels no BF"); run("Duplicate...", "duplicate channels=1-3"); rename(title+"_"+i); newtitle = getTitle; run("8-bit"); run("Z Project...", "projection=[Max Intensity]"); saveAs("tiff", dir+newtitle+"_MAXprj.tif"); run("Image Sequence... ", "format=TIFF start=0 digits=2 save=dir"); MAX = getImageID(); run("Stack Slicer", "split_channels keep_original stack_order=XYCZT"); selectImage(MAX); run("RGB Color"); saveAs("tiff", dir+newtitle+"_MAXprj_RGB.tif"); run("Images to Stack", "name=[Stack] title=[] use"); run("Make Montage...", "columns=4 rows=1 scale=1 border=5"); saveAs("tiff", dir+newtitle+"_Montage.tif"); close(); close(); close(); close(); }
> else if (BFimage==false && channels==4) { //print("4 fluorescent channels no BF"); run("Duplicate...", "duplicate channels=1-4"); rename(title+"_"+i); newtitle = getTitle; run("8-bit"); run("Z Project...", "projection=[Max Intensity]"); MAX = getImageID(); saveAs("tiff", dir+newtitle+"_MAXprj.tif"); run("Image Sequence... ", "format=TIFF start=0 digits=2 save=dir"); run("Stack Slicer", "split_channels keep_original stack_order=XYCZT"); selectImage(MAX); run("Make Substack...", "channels=1,3,4"); substack1 = getImageID(); run("RGB Color"); saveAs("tiff", dir+newtitle+"_MAXprj_RGBch1-3-4.tif"); selectImage(substack1); close(); selectImage(MAX); run("Make Substack...", "channels=2,3,4"); substack2 = getImageID(); run("RGB Color"); saveAs("tiff", dir+newtitle+"_MAXprj_RGBch2-3-4.tif"); selectImage(substack2); close(); run("Images to Stack", "name=[Stack] title=[] use"); monty = getImageID(); run("Make Montage...", "columns=6 rows=1 scale=1 border=5"); saveAs("tiff", dir+newtitle+"_Montage1.tif"); close(); selectImage(monty); run("Make Substack...", "slices=1-5"); substack3 = getImageID(); run("Make Montage...", "columns=5 rows=1 scale=1 border=5"); saveAs("tiff", dir+newtitle+"_Montage2.tif"); close(); selectImage(substack3); close(); selectImage(monty); run("Make Substack...", "slices=1,2,3,4,6"); substack3 = getImageID(); run("Make Montage...", "columns=5 rows=1 scale=1 border=5"); saveAs("tiff", dir+newtitle+"_Montage3.tif"); close(); selectImage(substack3); close(); selectImage(MAX); close(); selectImage(monty); close(); close(); }
> else if (BFimage==true && channels==4) { //print("3 fluorescent channels and one BF image"); run("Duplicate...", "duplicate channels=1-4"); rename(title+"_"+i); newtitle = getTitle; run("8-bit"); run("Z Project...", "projection=[Max Intensity]"); saveAs("tiff", dir+newtitle+"_MAXprj.tif"); run("Image Sequence... ", "format=TIFF start=0 digits=2 save=dir"); MAX = getImageID(); run("Make Substack...", "channels=1-3"); fchanMAX = getImageID(); run("Stack Slicer", "split_channels keep_original stack_order=XYCZT"); selectImage(fchanMAX); run("RGB Color"); saveAs("tiff", dir+newtitle+"_MAXprj_RGB.tif"); selectImage(MAX); run("Make Substack...", "channels=4"); run("Images to Stack", "name=[Stack] title=[] use"); run("Colors...", "foreground=white background=white"); run("Make Montage...", "columns=5 rows=1 scale=1 border=5"); saveAs("tiff", dir+newtitle+"_Montage.tif"); close(); close(); close(); close(); close(); }
> else if (BFimage==true && channels==5) { //print("4 fluorescent channels and one BF image"); run("Duplicate...", "duplicate channels=1-5"); rename(title+"_"+i); newtitle = getTitle; run("8-bit"); Im1 = getImageID(); run("Z Project...", "projection=[Max Intensity]"); MAX = getImageID(); saveAs("tiff", dir+newtitle+"_MAXprj.tif"); run("Image Sequence... ", "format=TIFF start=0 digits=2 save=dir"); selectImage(MAX); run("Make Substack...", "channels=1-4"); substack3 = getImageID(); run("Stack Slicer", "split_channels keep_original stack_order=XYCZT"); selectImage(MAX); run("Make Substack...", "channels=1,3,4"); substack1 = getImageID(); run("RGB Color"); saveAs("tiff", dir+newtitle+"_MAXprj_RGBch1-3-4.tif"); selectImage(substack1); close(); selectImage(MAX); run("Make Substack...", "channels=2,3,4"); substack2 = getImageID(); run("RGB Color"); saveAs("tiff", dir+newtitle+"_MAXprj_RGBch2-3-4.tif"); selectImage(substack2); close(); selectImage(MAX); run("Duplicate...", "duplicate channels=5"); run("Images to Stack", "name=[Stack] title=[] use"); monty = getImageID(); run("Make Montage...", "columns=7 rows=1 scale=1 border=5"); saveAs("tiff", dir+newtitle+"_Montage1.tif"); close(); selectImage(monty); run("Make Substack...", "slices=1,2,3,4,5,7"); substack3 = getImageID(); run("Make Montage...", "columns=6 rows=1 scale=1 border=5"); saveAs("tiff", dir+newtitle+"_Montage2.tif"); close(); selectImage(substack3); close(); selectImage(monty); run("Make Substack...", "slices=1,2,3,4,6,7"); substack3 = getImageID(); run("Make Montage...", "columns=6 rows=1 scale=1 border=5"); saveAs("tiff", dir+newtitle+"_Montage3.tif"); //selectImage(substack3); close(); //selectImage(Im1); close(); //selectImage(MAX); close(); //selectImage(monty); close(); close(); close(); }}
>
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html