Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This generic script can be used in ImageJ "headless" mode 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
// Specify global variables
#@String input
#@String suffix
// Add trailing slashes
output=input; // This can be changed to a separate variable if need-be
// 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
Months = 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 day names
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
timestamp ="";
if (dayOfMonth<10) {timestamp = timestamp+"0";}
timestamp = ""+DayNames[dayOfWeek]+" "+Months[month]+" "+dayOfMonth+" "+year+", "+timestamp;
if (hour<10) {timestamp = timestamp+"0";}
timestamp = timestamp+""+hour;
if (minute<10) {timestamp = timestamp+"0";}
timestamp = timestamp+":"+minute+"";
if (second<10) {timestamp = timestamp+"";}
timestamp = timestamp+":"+msec;
// Do something to each file
// Insert additional code here
// Print log of activities for reference...
print (timestamp + ": Processing " + input + file);
// A final statement to confirm the task is complete...
print("Task complete.");

This comment has been minimized.

Copy link

@rayzing rayzing commented Oct 23, 2018

Hi Adam,

Do you know what gists are used for? Could I use them to obtain some help on programs I'm writing?


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.