Macro: How can I create an output files with a name related to the input file?


I am very new to programming and have the following issue:

I wrote a Macro which loads images from a user chosen Directory, for instance, “islet1”.

Now I want my images the Macro creates to be saved in a new file on my desktop, termed with the name of the chosen file+_Results, so for the above example: “islet1_results”.

Is that somehow possible? I managed to create an output file but I get stuck when creating a flexible connection to the original file name.

Also I want to save it on my Desktop. Is there a command for that? I only found the command for saving it in the user folder.

This is my very primitive attempt so far:


I hope, somebody can help me.

Hi @consti1511 ,

you can keep track of the original image name by using the following at the beginning of the script:

originalName = getTitle();

Possibly there is a file extension at the end of your original file name. You could remove it as follows

originalNameWithoutExt = replace( originalName , ".tif" , "" ); 

You can then reuse it later on in the macro to define the result image name

resultName = originalNameWithoutExt + "_result.tif";

Good day,

here is a demo macro that does what you want, at least on a mac:

fileName = File.nameWithoutExtension;
path2Desktop = getDirectory("home") + "Desktop/";
save( path2Desktop + fileName + "_results.tif" );

Paste the code to an empty macro window (Plugins >> New >> Macro) and run it.

In general you will have to learn how to use the ImageJ macro language. Here
is a commented list with the available functions and here
you find a description of the language.



Hi @anon96376101 Thank you! Thats it!!

Actually I meant a folder, not a file to save on the Desktop, but I changed it a bit and now it works.

However, somehow I am not able to save anything inside the folder. That is what I tried so far.

//Create output directory
home = getDirectory("home") + "Desktop/";
fileName = File.nameWithoutExtension;
folderName = fileName + "_results";
File.makeDirectory(home + folderName);

outputdir = (home + folderName);

selectWindow(ImageTitle + "_Dapi");
saveAs("tiff" + outputdir);

selectWindow(ImageTitle + "_Dapi-inside");
saveAs("tiff", outputdir);

I do not receive an error message, but the folder is empty and the images are not saved anywhere else…

Does anybody know, where the mistake is?

Thanks again for your help!!

Again a demo macro:

fileName = File.nameWithoutExtension;
path2Folder = getDirectory("home") + "Desktop/" + fileName + "_results/";
if ( File.exists( path2Folder) < 1 ) File.makeDirectory( path2Folder );
save( path2Folder + fileName + "_Dapi.tif" );

Please note that in general you need to generate the directory only once.
And please study the documents I’ve mentioned earlier.



Hi @consti1511,

The path used in saveAs has to include the filename as well.

Instead of this:

selectWindow(ImageTitle + "_Dapi");
saveAs("tiff", outputdir);

try this:

newImageName = ImageTitle + "_Dapi.tif";
selectWindow(ImageTitle + "_Dapi");
saveAs("tiff", outputdir+File.separator+newImageName);

The list of macro functions explains the required arguments for the functions, and links to many examples, as @anon96376101 mentioned.

Hope this helps.

I got it now, thank you very much!!