Macro that check the filename


#1

Hi, I am trying to build a mcro that set the appropriate scale depending on the magnification of the picture, which is shown in the filename by 1.6x, 4x, 10x….
So far, I have written this (taken from a previous post in this forum):

filepath=File.openDialog(“Select a File”);
open(filepath);
title=getTitle(filepath);
if (indexOf(title,“1.6X”) >= 0) {
run(“Set Scale…”, “distance=333 known=1000 pixel=1 unit=µm global”);

But when running it, this is the error I get:

Undefined variable in line 4.
If (indexOf(,”1.6x”)>=0) {

Any help? It goes without saying that I am pretty new in this of macro writing so, please, be patient with me…
Thank you very much for your help
Cheers


#2

Not sure what you use in your code,

if (indexOf(title,"1.6X") >= 0) { … }

or

If (indexOf(,”1.6x”) >= 0) { … }

but this

title = "mybestimage_1.6X";
if (indexOf(title,"1.6X") >= 0) { print( "Heureka"); }

works for me.

HTH

Herbie


#3

Hi Herbie,

thanks a lot for your help. There is an error when I typed the error message. The code says:

Great that it worked for you, but the thing is that I am opening different files with this macro.

Now, my question is, when opening them through the dialog called by:

Does the variable “title” gets as value the name of the opened file or you need an additional line in teh code to give “title” a value?

Thanks a lot and sorry for the ignorance

Cheers


#4

Firstly and essentially:
Please consult the commented list of ImageJ macro functions before starting to code macros.
https://imagej.nih.gov/ij/developer/macro/functions.html

From this list you may realize that

getTitle();

doesn’t accept any arguments. It simply returns the title of the frontmost image.

So if you want to grab the title of the just opened window there are a number of options:
You may use

nameWithExt = File.getName(path);

or

nameWithOutExt = File.nameWithoutExtension;

or

nameWithExt = getTitle();

Don’t forget the simple way of debugging by using the “print”-command e.g.

print( nameWithOutExt );

HTH

Herbie


#5

Hi Herbie,
thanls a lot for your help. Now the issue is solved. Here it is the code:

file=getArgument();
open(file);
title=getTitle();
if (indexOf(title,“1.6X”) >= 0) {
run(“Set Scale…”, “distance=333 known=1000 pixel=1 unit=µm global”);
} else {
if (indexOf(title,“4X”) >= 0) {
run(“Set Scale…”, “distance=855 known=1000 pixel=1 unit=µm global”);
} else {
if (indexOf(title,“10X”) >= 0) {
run(“Set Scale…”, “distance=2142 known=1000 pixel=1 unit=µm global”);
} else {
if (indexOf(title,“20X”) >= 0) {
run(“Set Scale…”, “distance=426 known=100 pixel=1 unit=µm global”);
} else {
if (indexOf(title,“40X”) >= 0) {
run(“Set Scale…”, “distance=849 known=100 pixel=1 unit=µm global”);
} else {
if (indexOf(title,“macro”) >= 0) {
run(“Set Scale…”, “distance=219 known=2 pixel=1 unit=cm global”);

It allows me to open any file (through the droplet pluggin) and it automatically set the scale dpeending on the magnificatio of the photo. It works great as I have more than 400 pics taken at different magnifications!

I have anoether question, and again it comes from my total ignorance in coding. Now that the macro set the appropiate scale to the file, how can I code it to do some actions regardless the magnification. I mean. It opens the file, set the scale and then, for example, open the freehand tool. If i just write

setTool(“freehand”);

after the last line of the code, the macro will set to freehand only when the file has “macro” in the title, but no whenever I open pictures with any other magnification

Thanks a lot for your help!


#6

OK, found the solution. Just have to play with the {}… beginners error, I´m afraid!

Thanks a lot for your help!