Skip to content

Instantly share code, notes, and snippets.

@AdamDimech
Last active March 14, 2017 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamDimech/cd3999f6fe9eddfa55c19d6fd3997bbc to your computer and use it in GitHub Desktop.
Save AdamDimech/cd3999f6fe9eddfa55c19d6fd3997bbc to your computer and use it in GitHub Desktop.
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 https://gist.github.com/AdamDimech/cd3999f6fe9eddfa55c19d6fd3997bbc
// 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();
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