Adding Macros to Startup Macros

macro
Tags: #<Tag:0x00007fa3050d5368>

#1

I am trying to add two macros to my Startupmacros file so that I have them available in every session.
However, when I add them as below, I get an error when running the first macro that references the line number for “function processFiles(dir) {” in the second macro.

If I split the macros, they individually work fine. I think that this has something to do with my brackets, i.e. the macro seems to “continue on” to the second macro despite what I think are my correct brackets in the first macro.

Any ideas?

macro "split images" 
{
	input = getDirectory("Choose a Directory ");
	output = getDirectory("Choose a Directory ");

	// See also Process_Folder.py for a version of this code
	// in the Python scripting language.
	setBatchMode(true);
	print (input);
	print (output);

	input += File.separator;
	output += File.separator;

	print (input);
	print (output);


	processFolder(input);

	// function to scan folders/subfolders/files to find files with correct suffix
	function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	Array.print(list);
	//waitForUser("");

			for (i = 0; i < list.length; i++) {
			if(File.isDirectory(input + File.separator + list[i]))
				processFolder(input + File.separator + list[i]);
				processFile(input, output, list[i]);
		}
	}

	function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	// Leave the print statements until things work, then remove them.

	open(input + file);
	run("Split Channels");
	title = getTitle();
	saveAs("Tiff", output + title); 
	close();
	title = getTitle();
	saveAs("Tiff", output + title); 
	close();
	print(title);
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
	list = getList("window.titles"); 
		 for (i=0; i<list.length; i++) { 
		 winame = list[i]; 
			selectWindow(winame); 
		 run("Close"); 
		} 
	}
}

macro "Max Z Intensity" 
{
   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],File.separator))
              countFiles(""+dir+list[i]);
          else
              count++;
      }
  }

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

  function processFile(path) {
       if (endsWith(path, ".tiff")) {
           open(path);
         run("Z Project...", "projection=[Max Intensity]");
         save(path);
         close(); 
         close();   
      }
  }
}

#2

Hi @Chris4,

You are right, there is a } too many in your code. I would move functions outside my macros by the way.

It is a matter of proper indentation again. Train yourself to always use a matching pair of {} and/or{}else{} after an if clause as the day will come when you want to extend your macro to execute more than one statement when true or when false.

As a matter of fact, that day may have been yesterday.

Checking for balancing {} pairs is easy with a text editor like TextWrangler (MacOS, free, or in the Unix text editor vi/vim and which have undoubtedly a counter part in the WinDOS world); place the cursor at a { or a }, press cmd-B and the block of code enclosed by the balanced pair of {} is show.

I do see that when balancing the first { of your first macro, the } before Max Z Intensity macro is dangling.

for (i = 0; i < list.length; i++) {
			if(File.isDirectory(input + File.separator + list[i]))
				processFolder(input + File.separator + list[i]);
				processFile(input, output, list[i]);
		}

is actually

for (i = 0; i < list.length; i++) {
	if(File.isDirectory(input + File.separator + list[i]))
		processFolder(input + File.separator + list[i]);
	processFile(input, output, list[i]);
}

to the computer. But maybe that is intentionally?


#4

Thanks for the reply.

I think that I have found a solution, and it may reference your advice to not include functions in macros, although to be honest, I’m not sure why it works now.

I had reused the same code for the function “ProcessFile” in macro1 and macro2.

In macro2, when I renamed the “Processfile” function to “Processfile2”, the error was removed.