HELP! with creating a macro with generic file names




I’ve tried and tried to figure creating a macro that removes the specific file name but I have been unsuccessful. Tomorrow, I will start to get the images that need to be processed, so I’m throwing in the towel in trying to figure it out on my own.

I will have images that are stained with Dapi and some cells with also be GFP positive but not all.

How do I make this macro to be used with any image:

run(“Make Composite”);
run(“Split Channels”);
selectWindow(“C1-P4 10106 2.jpg”);
selectWindow(“C2-P4 10106 2.jpg”);
selectWindow(“C3-P4 10106 2.jpg”);
selectWindow(“C1-P4 10106 2.jpg”);
run(“Subtract Background…”, “rolling=50”);
run(“Gaussian Blur…”, “sigma=2”);
setAutoThreshold(“Default dark”);
//setThreshold(80, 255);
setOption(“BlackBackground”, true);
run(“Convert to Mask”);
run(“Fill Holes”);
imageCalculator(“AND create”, “C2-P4 10106 2.jpg”,“C3-P4 10106 2.jpg”);
selectWindow(“Result of C2-P4 10106 2.jpg”);
setAutoThreshold(“Default dark”);
//setThreshold(12, 255);
run(“Convert to Mask”);
run(“Analyze Particles…”, “size=200-Infinity circularity=0.50-1.00 show=Outlines summarize”);

Thank you for any help.


Hello @nbrimer,

The usual way to modify a macro recorded with a specific image to make it work with any generic image normally involves two things:

  1. Identifying and storing the name of the generic image
  2. Replacing every instance in which the name of the original image appears with the generic name previously stored

The first step is accomplished by storing the title (name) of the current image in a variable:


In your macro, the second part has to take into account that Split Channels adds a different prefix to the original title for every channel: “C1-”, “C2-”, etc., so you have to concatenate these prefixes with the contents of your title variable. By inspecting the code provided, in this case you end up having only three channels (original RGB image).

There are many ways to perform parts 1 & 2, the simplest one being:

img_name=getTitle(); //stores the name of the current image
run("Make Composite"); 
run("Split Channels"); // generates three new images: C1-[title], C2-[title], C3-[title]
run("Subtract Background…", "rolling=50");
run("Gaussian Blur…", "sigma=2");
setAutoThreshold("Default dark");
//setThreshold(80, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Fill Holes");
imageCalculator("AND create", "C2-"+img_name,"C3-"+img_name); // generates a "Resut of "+first image used
selectWindow("Result of C2-"+img_name);
setAutoThreshold("Default dark");
//setThreshold(12, 255);
run("Convert to Mask");
run("Analyze Particles…", "size=200-Infinity circularity=0.50-1.00 show=Outlines summarize");

In this case, I simply replaced every instance of P4 10106 2.jpg" for "+img_name (notice placement of quote marks in each case). Sometimes it can get more complicated than this, but you get the idea.

As a final remark, be aware of the following: in some cases, image titles are passed to commands/plugins as part of a string of parameters (usually names). In that case, it is important to notice that if the title contains spaces (as is the case for the particular image you used), the name in the parameter string must be enclosed in square brackets. Sometimes, when recording, square brackets are ommited because the image used does not have spaces:

run("Calculator Plus", "i1=image.tif i2=[other image.tif] operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=0 create");

Notice the diference in the name of the two images provided. It is good practice to always include the square brackets in these cases:

run("Calculator Plus", "i1=["+name1+"] i2=["+name2+"] operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=0 create");


PS: I noticed you are using a .jpg image in your macro. Be careful: jpeg compression is lossy, and adds artifacts to your original image. If possible, always perform the image analysis using the original formats in which the images are captured.



This is very helpful and clears up some confusion I had. I’ve only messed around a little with the macro because currently I’m busy getting images.