Recorded Macro sequence can run only an image of specific name. How to change that?

Hi,
Im new to creating Macros. I recorded a sequence of functions that I want to use to analyze double stained nuclei. The problem that I’m having is that the macro doesn’t want to run on any other image but the one that it was recorded from (in this example 1.tif). How to make it applicable for any picture that I want to analyze?
Another issue that I have is that after installing the recorded macro and restarting ImageJ the macro is not there and I have to reinstall it again. How to make it permanently appear in the Plugins>Macro location?

run("Split Channels");
selectWindow("1.tif (blue)");
selectWindow("1.tif (red)");
close();
selectWindow("1.tif (blue)");
setAutoThreshold("Default dark");
//run("Threshold...");
setThreshold(44, 255);
//setThreshold(44, 255);
setOption("BlackBackground", false);
run("Convert to Mask");
run("Make Binary");
run("Watershed");
run("Analyze Particles...", "size=50-Infinity show=Outlines display summarize add");
selectWindow("Drawing of 1.tif (blue)");
selectWindow("1.tif (green)");
setAutoThreshold("Default dark");
//run("Threshold...");
setThreshold(29, 255);
//setThreshold(29, 255);
run("Convert to Mask");
run("Make Binary");
run("Analyze Particles...", "size=50-Infinity show=Outlines display summarize add");

Hi @rgrabski

There are a few ways to generalize a macro like this. In this case I think the easiest thing to do is use getTitle() to remember the name of the original image, as Split Channels will just prepend “C1-, C2-, etc…” to the individual channel images.

Also I believe it is redundant to run setAutoThreshold("Default dark"); and then manually set an explicit threshold range. If you prefer to just rely on the auto threshold, you can replace the setThreshold lines below.

If you haven’t already seen it, check out the wiki for additional info on writing macros.

// Preconditions:
//    An RGB image open

title = getTitle();
run("Split Channels");

 // Close the red channel as it's not of interest to us
selectWindow("C1-" + title);
close();

// process blue channel
selectWindow("C3-" + title);
setThreshold(44, 255);
setOption("BlackBackground", false);
run("Convert to Mask");
run("Make Binary");
run("Watershed");
run("Analyze Particles...", "size=50-Infinity show=Outlines display summarize add");

// process green channel
selectImage("C2-" + title);
setThreshold(29, 255);
run("Convert to Mask");
run("Make Binary");
run("Analyze Particles...", "size=50-Infinity show=Outlines display summarize add");
3 Likes

If you’re using the Fiji distribution, save your macro to a .ijm file with an underscore (_) in the name… e.g. “MyMacro_.ijm”

Then put the .ijm file in your Fiji.app/plugins/Macros/ folder.

1 Like

Thank you very much for all your help. I truly appreciate that.

19 posts were split to a new topic: Using duplicates in ImageJ macros

A post was merged into an existing topic: Using duplicates in ImageJ macros

A post was merged into an existing topic: Using duplicates in ImageJ macros

Hey hinerm,

I am having the exact same problem, and your solution really helped me. However, when I split channels, it doesn’t automatically prepend “c1-, c2- …” to the individual channel images. I did some trouble shooting and replaced it with the channel “name” : Title + “(green)” as it displayed on my desktop, but no dice.

Please let me know if you have any advice! I am super new

You’re missing a space character in the generated title. + " (green)" should work.

1 Like

14 PM

No go :frowning: what am I doing wrong?

36 PM

In my above picture it does not look like I inserted the spaces, but hopefully this is clearer:

Take a close look at your variable name: Titletitle

BTW, no need to post screenshots in this case: you can have syntax-highlighted code on the forum by enclosing your code between lines starting with “code fences” (```) – using the javascript keyword works well for IJ1 macros:

```javascript
your code here
```
2 Likes

thank you- I am sorry such a silly mistake.

I really appreciate it .