How can I write in the macro to select a window if contains a number on the title?

Dear all,
I am writing a macro and I would like to write “select an image window if the title of that image contains any number”. Is that possible?
Thanks
Miguel

Hi @mvizoso
Example
selectWindow(“C-1”);

But I do not want to tell ImageJ which number…I want ImageJ to recognize the window if contains “any” number.

I think you’d have to use the indexof(string, substring) function and have one for every number. Would be kind of a pain, but just a lot of copying and pasting.

Example:

string = getTitle();

if (indexOf(string, “0”) > 0 || indexOf(string, “1”) > 0 || indexOf(string, “2”) > 0 || …)

It would also help if I could know how to type something like this:

Variable1 = “any number”;

You can’t quite do that but you can use regular expressions to check if strings have numeric characters in.

The code below shows an example of doing this.

test = newArray("title", "title_1", "title_10", "anotherTitle", "title100");

for (i = 0; i < test.length; i++){
	if (matches(test[i], ".*[0-9].*")) {
		print("Yes, " + test[i] + " contains a number");
	} else {
		print("No, " + test[i] + " does not contain a number");
	}
}
1 Like

Hi @mvizoso,
Combine the answer of @lmurphy with the command getList(“image.titles”) to create an array of all open window titles. That takes the place of the variable test in Laura’s answer.

A general hint: it is easier to tell image windows apart by using their getImageID() than their names. The former is unique, the latter not per sé.