Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This generic script can be used in ImageJ to loop through files in a selected directory and do something to them.
// Blank ImageJ Macro Script that loops through files in a directory
// Written by Adam Dimech
// Also available at
// Specify global variables
input = getDirectory("Input Directory");
output = input; // Output images to the same directory as input (prevents second dialogue box, otherwise getDirectory("Output Directory"))
Dialog.create("File Type");
Dialog.addString("File Suffix: ", ".png", 5);
suffix = Dialog.getString();
// Scan folders/subfolders/files to locate files with the correct suffix
function processFolder(input) {
list = getFileList(input);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + list[i]))
processFolder("" + input + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
// Loop through each file
function processFile(input, output, file) {
// Define all variables
MonthNames = newArray("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); // Generate month names
DayNames = newArray("Sun", "Mon","Tue","Wed","Thu","Fri","Sat"); // Generate date names
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec); // Get date and time information
// Do something to each file
// Print log of activities for reference...
print (DayNames[dayOfWeek], dayOfMonth, MonthNames[month], year + "," + hour + ":" + minute + ":" + second + ": Processing " + input + file);
// A final statement to confirm the task is complete...
print("Task complete.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.