SaveAs command in script appears to work but is not saving files

Hello,

I am writing a script file with the aim of:

  • splitting a stack into individual images

  • applying the Analyze Particles macro to each image

  • saving the results table from Analyze Particles for each image in a separate file.

Everything works and I get no errors when I run the script as it is below. However when I go to the folder ‘output path’ nothing has been saved there. I have tried using various filename & extensions options in the saveAs command however I get an error when I do that (Error message states: The system cannot find the specified path).

I have been looking online for a solution but haven’t been able to find one.

Many thanks

Cameron

// clear the log before starting
run("Clear Results"); // Empty results table
run("Close All"); // Closes any other images

output_path = "C:\\Users\\Cameron\\Documents\\Image J Results\\"; 
// https://forum.image.sc/t/batch-processing-by-macro-recording/1902/4 - a comment on this forum entry used the \\ in the directory name.
 
open("C:/Users/Cameron/Documents/1. PhD/5. ImageJ/Datasets/Core 2 - Substack (1-75-5) segmented.tif"); // trial file. Will need to change to open selected file once script complete.
original_to_close = getTitle(); // assigns filename to variable to allow it to be selected and closed after stack to image command.
Stack.getDimensions(width, height, channels, slices, frames); // Returns the dimensions of the current image. Interested in slices.
run("Stack to Images"); // converts stack to individual images
close(original_to_close); // close the original file

no_active_images = nImages; // Variable to store number of images. Needed for the loop

for (i=1; i <= no_active_images; i++) {		// for loop to run Analyse Particles on each image 

run("Clear Results"); // Empty results table
active_image = getTitle(); // select current active image
run("Analyze Particles...", "size=0.1-Infinity display stack"); // calculates area's etc for this slice
close(active_image); // close the active image
selectWindow("Results");
saveAs("Results", output_path); // save results to the results folder

}

close("results") 
run("Close All")

Your output path is not complete. The file name is missing.
Instead of

saveAs("Results", output_path);

try

saveAs("Results", output_path + "Results_" + i + ".csv");

Thanks phaub,

I put your code in and it didn’t work (came up with the same error)

I then went back and changed the output folder name to remove the spaces between the characters in both the folder and the variable output file.

It is now fixed, thanks for your assistance after a very frustrating afternoon trying to fix this…