Error when running example function for macro on Mac

I’m trying to learn how to write relatively simple macros for image analysis so I am trying to start with the simple tutorials at https://imagej.net/How_to_apply_a_common_operation_to_a_complete_directory.
I am trying to do the first one for the “action” function on one of my images but I am getting an error. I think it is due to my directories but I can’t figure out how to fix it and need some help. I’m using a mac an having trouble figuring out how to get the right directory path. Also, when running a function in a macro, do you just execute the function in the macro to create it and then run it using the parameters?

This is my code:

function action(input, output, file) {
open(input + filename);
makeRectangle(796, 674, 512, 318);
run(“Crop”);
saveAs(“Tiff”, output + filename);
}

action("/Users/myfullname/Desktop/folder", “/Users/myfullname/Desktop/folder”, “file.tif”);

Hi Lindsay
@mosst137
A late reply:
Use the search engine (magnifying glass)
then “option” .
With the keyword “Mac” we find answers;
I hope it helps.

I am learning to create macros using the window::
plugins-> Macros-> record
then
Example:
1.I open the image I want to process
2; I copy the “record” in “macro”
then
3. run
Try!

@Mathew Yes, I did search it. The recorder won’t help as it doesn’t record the directory path or anything meaningful for my purposes.

this is not Mac-specific - your call to the action function is missing a slash. When the function runs open(input + filename);, it is actually running open("/Users/myfullname/Desktop/folderfile.tif"); (which, I’m gonna guess, doesn’t exist) instead of open("/Users/myfullname/Desktop/folder/file.tif"); (which is what you want).

It is good practice, when concatenating a folder path with a filename, to add a file separator to avoid that issue; so replacing that line with open(input + "/" + filename); (and, consequently, your saveAs call with saveAs(“Tiff”, output + "/" + filename);, that should work!

@erickratamero Thanks for the response! Unfortunately it still doesn’t work. Code below

function action(input, output, file) {
open(input + “/” + filename);
makeRectangle(796, 674, 512, 318);
run(“Crop”);
saveAs(“Tiff”, output + “/” + filename);
}

action("/Users/myfullname/Desktop/folder", “/Users/myfullname/Desktop/folder”, “file.tif”);

It will work if you change the ‘file’ argument to ‘filename’. And the ‘input’ and ‘output’ arguments are the same so they can be combined into a ‘dir’ argument. It’s also a good idea to close any files that are opened.

  action("/Users/myfullname/Desktop/folder/", "file.tif");

  function action(dir, filename) {
     open(dir+filename);
     makeRectangle(796, 674, 512, 318);
     run("Crop");
     saveAs("Tiff", dir+filename);
     close;
  }
2 Likes

That does work! Thank you! I should have caught that.

Is there a functional difference between the order of action() and function here?

I put functions at the end of the file but putting them at the beginning also works.