Filename contains "X" macro

Hi,
I’m trying to use a macro to measure images differently based on whether or not they have the phrase “40X” in their names.

So far I have:

title = getTitle();
 if ("title=40X") {
run("Set Scale...", "distance=7401.1736 known=1 pixel=1 unit=mm");
run("Measure")
 } else {
run("Set Scale...", "distance=11493.2204 known=1 pixel=1 unit=mm");
run("Measure");
}

I’m trying to replace
"title=40X"
with something that detect if the file name contains 40X anywhere in the title.

I’m aware that the batch macro program has the “file name contains …” tool, but I don’t want to use it in this case.

Thanks in advance!

Welcome to the forum, @chris1.

For string matching, you can use either:

if (indexOf(title, "40X") >= 0) {

Or if you like regex:

if (matches(title, ".*40X.*")) {

See the built-in macro functions page for full details.

9 Likes

Hi ctrueden thanks for help!

This will definitely save me a lot time in the future!

1 Like