Re: how to avoid macro interruption
Posted by Wayne Rasband on Dec 14, 2009; 5:33pm
URL: http://imagej.273.s1.nabble.com/how-to-avoid-macro-interruption-tp3690103p3690104.html
> I am using a macro to open and analyse thousands of files
> in a directory. Sometimes damaged files cannot be opened
> and this event cause the macro interruption (due to the
> execption in the Open command). Unfortunately I am not a
> Java programmer and I can only rely on macro language for
> programming and at the moment I am not able to find a way
> to solve the problem. Is there a way to intercept the
> execption in the open process skipping to the next item to
> avoid macro abortion? Thanks for the help
The ImageJ 1.43n daily build adds the IJ.redirectErrorMessages() macro
function, which prevents corrupted images opened using the open()
function from aborting the macro. This example, which measures all the
images in a folder, does not abort if the folder contains an image that
imageJ cannot open.
requires("1.43n");
dir = getDirectory("Choose a Directory ");
list = getFileList(dir);
setOption("display labels", true);
setBatchMode(true);
for (i=0; i<list.length; i++) {
path = dir+list[i];
showProgress(i, list.length);
IJ.redirectErrorMessages();
open(path);
if (nImages>=1) {
run("Measure");
close();
} else
print("Error opening "+path);
}
I tested it by putting an MP3 file in the folder, which caused this
information to be displayed in the Log window:
Opener: File is not in a supported format, a reader
plugin is not available, or it was not found.
/Users/Shared/Downloads/stack/test010.mp3
Error opening /Wayne/Downloads/stack/test010.mp3
-wayne