Adapt duplicate's name in a macro

My programing skills are very poor and that’s why I need your help.
I want to do the same process to many many images and I thought of programing it (well, record it and then add/remove some things). I want to take an image, specify a region, duplicate that region, change the name adding a “_centered” (instead of a "_1) before the .tif and saving it. This is what I’ve got:

run("Specify...", "width=540 height=540 x=50 y=15");
run("Duplicate...", "title=CurrentName_centered.tif");
saveAs("Tiff", "C:\\Users\\Admin\\Desktop\\MyFolder\\CurrentName_centered.tif");

I have seen that there’s a similar topic but they start discussing and I lose track.
I guess I have to use “getImageID()”, but as I said, I am not very skilled.
I would be very grateful if you could help me.
Have a nice day :smiley:

You can do this as follows:

currentImage = getTitle();
dir = getDirectory("image");
run("Specify...", "width=540 height=540 x=50 y=15");
run("Duplicate...", "title="+currentImage +"_centered.tif");
duplicateImage = getTitle();
saveAs("Tiff", dir + duplicateImage);

First of all: thank you so much!
But can I bother you a bit more? When I do this, the image’s name is “Something.tif_centered.tif”. Is it possible to avoid the first .tif? With other kind of files happens the same, for example "Something.png_centered.tif.
But I am happy already, so thank you very much :smiley: :smiley: :smiley:


You’ll want to check out the Built-In Macro Functions page to find some functions dealing with strings… If you print(currentImage);… I am assuming you get the string Something.tif.

So you can just get a substring of that string like…

currentImage = getTitle();
currentImageLength = lengthOf(currentImage);
currentImage = substring(currentImage, 0, currentImageLength-4); // the -4 gets rid of the .tif

The print() statements are just there as a check for you to see that the string is being changed…

Hope this helps!


Thank you so much! I added this to the code above and it works perfectly, as I wished for.
Thank you all so much :heart:

1 Like