Macro ‘Undefined variable in line 1’ error - bug or newbie mistake?

Hello all,

I am new to FIJI/ImageJ and a novice at macro programming. I’ve
managed to throw together a basic image analysis macro to measure
various parameters from JPGs or TIFs.

It is supposed to open an image file, perform the measurements, then
save the overlays and results tables before closing all the windows in
preparation for running the macro again. I can post the macro if it
would help…

On the whole it works well, but for some reason after I run it a
couple of times, it returns the error message, “Undefined variable in
line 1”, then something in triangular brackets (e.g. JFIF), followed by
what looks suspiciously like some of the file metadata (e.g. ‘Hamamatsu
JPEG library version xxx’, where Hamamatus is the name of the slide
scanner I used).

The puzzling thing (for me at least) is that line 1 is merely

run("Open...");

…and the macro at first seems to work perfectly once or twice. If I
restart FIJI, it again works perfectly once or twice before returning a
similar error.

If I delete the ‘Open’ command it does seem to work, but I’d like to
keep ‘Open’ because I’m using the rename(File.nameWithoutExtension)'
command to rename the image file for saving, and if I don’t use 'Open’
then it gets renamed with the name of the macro instead (hope that makes
sense!).

I would really appreciate any insights / suggestions - I’ve no idea
if this is me making some sort of rookie error, or some weird bug
preventing the macro from working more than a couple of times…

Cheers,

John.

This would be helpful. I was unable to reproduce the issue with just run("Open...");

If you can share a sample image that fails with your script that would be even better. If the data is private, you can use the upload sample image plugin.

1 Like

Thanks hinerm,

Here’s the macro…

run("Open...");
run("Set Scale...", "distance=1.1 known=1 pixel=1 unit=um global");
run("Set Measurements...", "area perimeter bounding display redirect=None decimal=3");
run("Colors...", "foreground=black background=white selection=red"); 
run("8-bit"); //Converts to 8-bit image for thresholding
rename(File.nameWithoutExtension);
run("Select All");
run("Copy");

newImage(File.nameWithoutExtension, "8-bit white", 1680, 1056, 1);
run("Paste");
run("Auto Threshold", "method=Default white");

run("Select None");
setTool("polygon"); 
waitForUser("Draw round all villi of interest then press OK"); 
run("Make Inverse"); 
run("Cut");

function roiRenameLast(name) {
last = roiManager("count")-1;
roiManager("select", last);
roiManager("Rename", name);
}

var n = 0
run("Select None");
while (getBoolean("Draw a base?") == 1)    {
    setTool("polyline");
    run("Line Width...", "line=5");
    waitForUser("Draw villus base (double click to complete) then press OK");
    roiManager("Add");
    n++;
    roiRenameLast(" B"+n);
    roiManager("Measure");
    setForegroundColor(0, 0, 0);
    run("Fill", "slice");
}

selectWindow("ROI Manager");
run("Close");

waitForUser("Correct defects etc. using pencil tool then press OK"); 

n=0;
while (getBoolean("Measure a villus?") == 1)    {
    run("Select None");
    setTool("wand");
    waitForUser("Select a villus then press OK");
    roiManager("Add");
    n++;
    roiRenameLast(" VA/VP"+n);
    roiManager("Measure");
    setTool("polyline");
    run("Line Width...", "line=500");
    waitForUser("Draw villus axis (double click to complete) then press OK");
    run("Straighten...");
    setTool("wand");
    waitForUser("Select a villus then press OK");
    run("To Bounding Box");
    waitForUser("Adjust bounding box if required, then press OK");
    rename(File.nameWithoutExtension);
    roiManager("Add");
    roiRenameLast(" VH/VW"+n);
    roiManager("Measure");
    roiManager("Delete");
    run("Close");
    }

run("Close");

roiManager("Show All");
setTool("freeline");
run("Line Width...", "line=1");
waitForUser("Draw MM then press OK");
roiManager("Add");
roiRenameLast(" MM");
roiManager("Measure");

n=0;
while (getBoolean("Draw crypt?") == 1)    {
    setTool("polyline");
    run("Line Width...", "line=1");
    waitForUser("Draw crypt (double click to complete) then press OK");
    roiManager("Add");
    n++;
    roiRenameLast(" CD"+n);
    roiManager("Measure");
    }

roiManager("Show All");
run("Line Width...", "line=1");
run("Flatten");
rename(File.nameWithoutExtension+"_overlay");
saveAs("jpeg");
saveAs("results");
selectWindow("ROI Manager");
run("Close");
selectWindow("Results");
run("Close");
run("Close All");

I’ve uploaded ‘6302_test.jpeg’ using the plugin you suggested - apologies but I don’t know how to link to the file - is there something else I need to do??

John.

1 Like

No worries. That plugin uploads files to a secure location so that private data can be shared.

I got your image but haven’t been able to reproduce the issue, after running the script several times.

There are many interactive steps in this macro, which unfortunately makes debugging tricky. Can you identify a sequence of responses or actions that always causes this error on the next script execution?

Also, I assume you are using Fiji. Have you run Help > Update... recently? Do you have any additional update sites enabled or plugins installed?

2 Likes

Hmm… I’ve tried it this evening on my home computer and it now seems to be working fine! I’m using FIJI at work (PC) and home (Mac), and AFAIK they are identical installations, except that I have Biomedgroup at home. Both installations update whenever I’m told there are updates to be had… I’ll try installing Biomedgroup at work, but if that doesn’t work I’ll be able to survive using my home version…