Batch deinterleave a stack, followed by RGB merge

I am looking for help with a macro that can batch deinterleave a stack of grayscale images,
and then color merge them.

The macro works when I manually open a stack in ImageJ, and then “run selected code” in the macro window.

However, I get an error when I do it with batch processing, as I’d like to do this for multiple TIFF files (each TIFF file is a stack of many images). The error message is attached below as a screenshot. Any help would be much appreciated!

Here is my code:

        dir = getDirectory("Choose a Directory "); 
        dir2 = getDirectory("destination Directory");
	list = getFileList(dir); 
	for (i=0; i<list.length; i++) { 
		path = dir+list[i]; 


fullname3 = name + " #3";
fullname2 = name + " #2";
fullname1 = name + " #1";

run("Deinterleave", "how=3 keep");

selectWindow(name + " #3");
selectWindow(name + " #2");
selectWindow(name + " #1");

run("Merge Channels...", "c1=["+fullname2+"] c2=["+fullname1+"] c3=["+fullname3+"] create");
saveAs("Tiff", dir + slice + "_composite.tif");


I think I fixed most of the problem. If I set batch mode to “false”, the script works. I had to make a few other minor changes but that was the important one.