1

I modified the following macro I found on an ImageJ message board to batch split channels, create a new folder called "OneChannel" and save the output in the new folder. The code works as expected: it creates the new folder, it splits the channels, and saves them under a new name. The only problem is that it saves the new files in the same folder as the originals and I have to manually drag them to the newly created folder afterwards. I have tried messing with this but I cannot get it to work. Any input would be appreciated, I am very new to Java.

dir=getDirectory("Choose a Directory"); 
print(dir); 
splitDir= dir + "OneChannel"; 
print(splitDir); 
File.makeDirectory(splitDir); 
list = getFileList(dir); 

for (i=0; i<list.length; i++) { 
     if (endsWith(list[i], ".tif")){ 
               print(i + ": " + dir+list[i]); 
             open(dir+list[i]); 
             imgName=getTitle(); 
         baseNameEnd=indexOf(imgName, ".tif"); 
         baseName=substring(imgName, 0, baseNameEnd);
         run("Split Channels"); 
         selectWindow(imgName + " (blue)"); 
         close(); 
         selectWindow(imgName + " (green)"); 
         saveAs("Tiff",  splitDir + baseName +  "-AnkG.tif"); 
         close(); 
         selectWindow(imgName + " (red)");
         run("Close All"); 
     } else {
     write("One Channel Conversion is Complete");
     }
} 
Jan Eglinger
  • 3,995
  • 1
  • 25
  • 51
Nick
  • 95
  • 1
  • 2
  • 8
  • Could you please add a link to the "message board" where you found the macro? ImageJ usage question are best asked on the [ImageJ forum](http://forum.imagej.net/). – Jan Eglinger Mar 22 '16 at 08:24
  • @JanEglinger here it is: http://imagej.1557.x6.nabble.com/Batch-split-channels-and-save-td3687341.html – Nick Mar 22 '16 at 11:54

1 Answers1

1

I actually figured this out. In line 3 I needed to put "/OneChannel/". As soon as I did that it is working fine. I guess I was not specifying a real file path before, but the following works fine now.

dir=getDirectory("Choose a Directory"); 
print(dir); 
splitDir= dir + "/OneChannel/"; // This was my error, I left out "//" surrounding OneChannel
print(splitDir); 
File.makeDirectory(splitDir); 
list = getFileList(dir); 

for (i=0; i<list.length; i++) { 
     if (endsWith(list[i], ".tif")){ 
               print(i + ": " + dir+list[i]); 
             open(dir+list[i]); 
             imgName=getTitle(); 
         baseNameEnd=indexOf(imgName, ".tif"); 
         baseName=substring(imgName, 0, baseNameEnd);
         run("Split Channels"); 
         selectWindow(imgName + " (blue)"); 
         close(); 
         selectWindow(imgName + " (green)"); 
         saveAs("Tiff",  splitDir + baseName "-AnkG.tif"); 
         close(); 
         selectWindow(imgName + " (red)");
         run("Close All"); 
     } else {
     write("One Channel Conversion is Complete");
     }
} 
Nick
  • 95
  • 1
  • 2
  • 8