// This macro batch particle-analyze a folder of images.
// Use the Analyze>Set Measurements command
// to specify the measurement parameters. Check 
// "Display Label" in the Set Measurements dialog
// and the file names will be added to the first 
// column of the "Results" table.

   requires("1.33s"); 
   dir = getDirectory("Choose a Directory ");
   setBatchMode(true);
   count = 0;
   countFiles(dir);
   n = 0;
   processFiles(dir);
   //print(count+" files processed");
   
   function countFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              countFiles(""+dir+list[i]);
          else
              count++;
      }
  }

   function processFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              processFiles(""+dir+list[i]);
          else {
             showProgress(n++, count);
             path = dir+list[i];
             output = dir+"Result_SEC_"+list[i]+".txt";
             processFile(path,output);
          }
      }
  }

  function processFile(path,output) {
       if (endsWith(path, ".tif")) {
           open(path);
           if (nImages>=1) {
               run("Set Measurements...", "area mean standard min center integrated redirect=None decimal=9");
               run("Properties...", "unit=pixel pixel_width=1 pixel_height=1");
//             setThreshold(32, 32767);
//             setThreshold(36, 32767);
               setThreshold(38, 32767);
               run("Analyze Particles...", "size=2-64 circularity=0.00-1.00 show=Nothing display clear");
               saveAs("Measurements", output);
               close();
           }
      }
  }
