Skip to content

Instantly share code, notes, and snippets.

@AdamDimech
Created October 23, 2018 04:46
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/d0c26351529c8a0959db7ffdd98eb5bf to your computer and use it in GitHub Desktop.
Save AdamDimech/d0c26351529c8a0959db7ffdd98eb5bf to your computer and use it in GitHub Desktop.
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
// https://code.adonline.id.au/imagej-batch-process-headless/
// Specify global variables
#@String input
#@String suffix
// Add trailing slashes
input=input+"\\";
output=input; // This can be changed to a separate variable if need-be
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
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
open(input+file);
// Insert additional code here
close(file);
// Print log of activities for reference...
print (timestamp + ": Processing " + input + file);
}
// A final statement to confirm the task is complete...
print("Task complete.");
@rayzing
Copy link

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?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment