// Transfer_Metadata macro by Christophe Leterrier // v 1.0 25-09-2025 /* * This macro transfers the metadata from one set of images to a corresponding set of other images. * * After launch, the macro will ask you to select two folders one after another * (MacOS does not have a bar at the top of the window that can tell you what you're selecting like Windows) : * the first is the input folder (tif images that have the metadata you want) and the second is the output folder * (a set of tif images that has a "corresponding" image for each image from the input folder). * A corresponding image is an image whose name contains the name of the input image (minus extension), * like input folder: myimage.tif, output folder: myimage - Deconvolved.tif. It will then process all images * in the input folder, extract their metadata and write the metadata to the corresponding output image. * It will overwrite the existing metadata in the output image (there's an option in the macro to concatenate * the metadata from the input and output images, but that leads to lots of duplicate entries). * Be careful that it rewrites the output images, so work on a copy of it first in case something goes wrong. */ macro "Transfer Metadata" { //*************** Initialization *************** KEEP = false; // Get the folder name INPUT_DIR = getDir("Select the input directory"); OUTPUT_DIR = getDir("Select the output directory"); setBatchMode(true); print("\n\n\n*** Transfer_Metadata Log ***"); print(""); print("Input directory: " + INPUT_DIR); print("Output directory: " + OUTPUT_DIR); //*************** Processing *************** // Get all input file names ALL_INNAMES = getFileList(INPUT_DIR); ALL_INNAMES = Array.sort(ALL_INNAMES); IN_LENGTH = ALL_INNAMES.length; ALL_INEXT=newArray(IN_LENGTH); // Create extensions array for (i = 0; i < IN_LENGTH; i++) { ALL_INNAMES_PARTS = getFileExtension(ALL_INNAMES[i]); ALL_INEXT[i] = ALL_INNAMES_PARTS[1]; } // Get all output file names ALL_OUTNAMES = getFileList(OUTPUT_DIR); ALL_OUTNAMES = Array.sort(ALL_OUTNAMES); OUT_LENGTH = ALL_OUTNAMES.length; // Loop on all .tif extensions for (n = 0; n < IN_LENGTH; n++) { if (toLowerCase(ALL_INEXT[n]) == ".tif" || toLowerCase(ALL_INEXT[n]) == ".tiff" ) { // Get the file path INPUT_PATH = INPUT_DIR + ALL_INNAMES[n]; // Store components of the file name INPUT_NAME = File.getName(INPUT_PATH); INPUT_SEP = getFileExtension(INPUT_NAME); INPUT_SHORTNAME = INPUT_SEP[0]; INPUT_EXT = INPUT_SEP[1]; print(""); print("Input image: ", INPUT_NAME); // Open input image, get metadata, close input image open(INPUT_PATH); INPUT_ID = getImageID(); INPUT_META = getMetadata("Info"); close(); // find corresponding output image, which should contain the name (minus extension) of the input image) for (k = 0; k < OUT_LENGTH; k++) { if (indexOf(ALL_OUTNAMES[k], INPUT_SHORTNAME)>-1) OUTPUT_NAME = ALL_OUTNAMES[k]; } // Open output image, get metadata print("Detected output image: ", OUTPUT_NAME); OUTPUT_PATH = OUTPUT_DIR + OUTPUT_NAME; open(OUTPUT_PATH); OUTPUT_ID = getImageID(); OUTPUT_META = getMetadata("Info"); // Generate new metadata (depends on KEEP condition) if (KEEP == 0) NEW_META = INPUT_META; else NEW_META = INPUT_META + "\n" + OUTPUT_META; // Write the new metadata to the output image, save and close output image setMetadata("Info", NEW_META); save(OUTPUT_PATH); print("Saved output image with modified metadata"); close(); }// end of IF loop on tif extensions }// end of FOR loop on all files setBatchMode("exit and display"); print(""); print("*** Transfer_Metadata end ***"); showStatus("Transfer_Metadata finished"); } //*************** Functions *************** function getFileExtension(Name) { nameparts = split(Name, "."); shortname = nameparts[0]; if (nameparts.length > 2) { for (k = 1; k < nameparts.length - 1; k++) { shortname += "." + nameparts[k]; } } extname = "." + nameparts[nameparts.length - 1]; namearray = newArray(shortname, extname); return namearray; }