Skip to content

Instantly share code, notes, and snippets.

Embed
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.
// @File(label = "Input directory", style = "directory") input
// @File(label = "Output directory", style = "directory") output
// @String(label = "File suffix", value = ".png") suffix
// Blank ImageJ Macro Script that loops through files in a directory
// Written by Adam Dimech
processFolder(input);
// 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
open(file);
close(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.