Error batch on MACOS

Hello everybody,
Can you help me to understand the logic with fiji on macOS.
Somebody asks me to create a macro to duplicate 300 times the same picture and create a stack.
On windows I can have 300 pictures but on macro only 293 pictures even if the variable is 300. Why???

And I cannot understand why the filename could not be with space or -

/* Macro written by XXXXXXXXxx
This macro will multiply an image X times and save it in a specific folder
this macro will work in batch mode
*/

#@ File (label = "select input", style = "directory") input
#@ String (label = "multiply by", value = "300") NbrMultiply

output= input + "/files/"; 
File.makeDirectory(output);
output2= input + "/stack/"; 
File.makeDirectory(output2);

//get info input
list = getFileList(input);
//batch mode
setBatchMode(true);
//first loop 
for (i = 0; i < list.length; i++){
	run("Bio-Formats Importer", "open="   + input  + File.separator + list[i] + " autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
	//infos picture
    imagesName=getTitle(); 
    //define new folder for multiply pictures
    output1=output+File.separator+imagesName; 
    File.makeDirectory(output1);
    //second loop variable = NbrMultiply
        for (u=0; u<NbrMultiply; ++u) {
        	selectImage(imagesName);
            run("Duplicate...", "copy");
            saveAs("Tiff", output1 + File.separator +imagesName +(u+1));
            close();
             //Next round!
             selectWindow(imagesName);
        }
          close();
          //get info output1
          list1 = getFileList(output1);
          //run Image Sequence and save again
          run("Image Sequence...", "open=" + output1 + " sort");
          getDimensions(width, height, channels, slices, frames);
          saveAs("Tiff", output2 + File.separator +imagesName);
          close();
}

Hi @Alex.h,

Maybe you are using too much memory ?
Maybe be you could help yourself with the folowing example to improve your code.


nbCopy = 15;

run("Blobs (25K)");
getDimensions(width, height, channels, slices, frames);
newImage("HyperStack", "8-bit grayscale-mode", width, height, 1, nbCopy, 1);
selectImage("blobs.gif");
run("Select All");
run("Copy");
selectImage("HyperStack");
run("Select All");
for (i=1; i<=nbCopy;i++)
{
	setSlice(i);
	run("Paste");
}
run("Select None");

Nico

1 Like

Hi thank you for your help but the weird this is that on macOS i lost some slice before 10 and after it’s ok.

@Alex.h If I run the above macro of @VirtualSlide on ImageJ1 1.52n, Mac OS 10.10.5, I get a stack of 15 images, all filled. What do you mean with ‘I lost some slice’, which OS version are you on? Could you use ImageJ1 instead of ImageJ2 temporarily? I ask because you can first try to use ImageJ1 to debug and single step the macro to see what goes wrong, then go to ImageJ2 if you insist.

Or has your question been answered, problem solved?

Sorry My message was not clear. With my macro on MacOS with Fiji I loose some slice before 10 ( ```
saveAs(“Tiff”, output1 + File.separator +imagesName +(u+1));
but the same macro on PC is correct.

Could it be the () in (u+1) ?

Just a silly question (worth ask for it): you say that some images are missing. Isn’t it a sorting problem ? (I mean that sometimes, sorting is weird and 7, 8, 9 are not sorted close to 10 11 12 ?)

I’ve had quite a battle with backslashes, slashes and File.separator. I think your input already contains a trailing slash.
When on Mac OS, I generate new folders and then get their path from getFileList, which includes the appropriate separator (a slash in Mac OS), so if I were you, I’d ditch the ‘/’ in the strings whenever I could.

Then too, I would compose the whole file path string and use
saveAs(tiff,filePathString) rather than the composite statement you use, and thus avoid all sorts of problems like the ones @VirtualSlide suggests. Having composed filePathString makes its content show up in the Debug window (ImageJ1) or can be printed (ImageJ2) to see what’s happening.

1 Like

Thank you for your advice. I’m very newbie in macro with Fiji I start 2 months ago (I never study info, so I often learn from this forum and the macro made by other people). So can you send me an example of a good macro with filePathString.

Replace this with the following substeps.

//insert some sample data to allow debugging
imagesName="example";
u=2;
output1="/Volumes/myDisk/testoutput/";

//actual replacement of Alex.h code
imageNumber = IJ.pad(u+1,3); //this generates a string and forces three digits
fileName=imagesName+imageNumber;
fileName=fileName+".tif";
filePathString=output1+fileName;//assuming output1 has trailing File.separator in place
saveAs("Tiff", filePathString);
a=5;//this is a bogus command to stay in the debugger one more line to inspect variables

BTW, whether you don’t have a informatics background or not, to see what is happening in your variables is very helpful to get going, and it saves you many print statements if you could use ImageJ1 for the time being.

Debug mode allows you to execute your macro code step by step, noticing the effect of every statement as it is executed.
(Debug Macro (cmd D), then Step (cmd E) from the Debug menu (only visible in the menu bar when the Macro code window has focus), if you want to skip code, either throw in a line with debug(); or click your cursor where you want to start Stepping and choose ‘Run to Insertion Point’ (cmd shift E).

1 Like

if you want to be sure that your output variable ends with a /, you can use endsWith() :

if (!endsWith(output1, File.separator))
   output1= output1 + File.separator;
1 Like

Thank you !!! I will try the debug mode!

Be aware that both “/” and “\” are considered valid file separators in Windos, but not so in Mac OS.
So if on a PC your path is D:\somewhere\ and you test this path on Mac OS, your code will happily transform the path to \somewhere\/ (that is not a capital V but the characters backslash and slas) which may or may not fold to \somewhere/ as the \ often is also considered to express ‘the following literal character’ (as this forum software does btw).

Safest way is to go the ‘/’ file separator way as these are considered valid in both environments.

1 Like

Following up on my last posting, having a similar situation at hand today, I found this certain way of extracting into a variable the path ending with the proper separator:

chosenFile = File.openDialog("Select a file");
fn = File.getName(chosenFile);
nDir=File.getParent(chosenFile); //this command strips the separator
dir = replace(chosenFile,fn,""); //this command leaves the directory separator in place