Imagej Macro Error: statement can not start with { (and other errors)

I tried to make a basic loop test function for processing images in several directories, just to check if it identifies what it should, before I incorporate it into a large macro. For this test function, it should go through an RGB image in every single folder, split it in 3 channels, and save the green and red channel in a new folder within the directory where it found the image.

I get an error:

Statement cannot begin with “{” in line 42.

I have noted in the code below which line the error shows, apologies if this chunk of code is far too long.
I took out the print function in case it’s messing with stuff, I carefully looked at the spaces,`` etc, but I used a template from online so it shouldn’t be any problem there. I couldn’t find a similar problem on this or any other forum.

FileName = getString("Enter the name of the 2-channel file in each folder", "RGB")
InputFileExt = getString("Enter the file extension", ".tif")
MainDir = getDirectory("Choose the Main Directory");

function processFolder(MainDir) {
    dir = getFileList(MainDir);            
    for (i=0; i < dir.length; i++) {                <---- error 
                print(dir[i] + FileName + InputFileExt)
        if(File.isDirectory(MainDir + dir[i]))
            processFolder("" + MainDir + dir[i]);
        if(endsWith(dir[i] + FileName + InputFileExt) 
            processFile(dir[i] + FileName + InputFileExt);
    }
}

function processFile (dir[i] + FileName + InputFileExt) { 
    run("Split Channels");
    selectWindow("RGB.tif (blue)");
    close();
    selectWindow("RGB.tif (green).tif");
    saveAs("Tiff", MainDir + File.separator + "Split channels" + File.separator + "RGB.tif (green).tif");
    selectWindow("RGB.tif (red)");
    saveAs("Tiff", MainDir + File.separator + "Split channels" + File.separator + "RGB.tif (red).tif");
    close(); 
        }
}

I also keep getting bracket errors for the print function, without any obvious reason, such as [ or . expected on line x, but I check a million times and I seem to have all brackets, semi colons, etc in place.

What am I missing? Any help with be great, thanks!

Hi

Please try running your code again finishing the lines with semicolon ; at the end.

But the error I get comes from the fact there is an extra closing bracket } at the last line, not where you had it…

Does this solve the issue?

2 Likes

Thanks!
Yes removing the extra bracket and placing a ; at the end of some lines that I missed it doesn’t give any error message, but the code also doesn’t seem to do anything… no error but not output either.

I will check the functions and commands etc, thanks for spotting these errors!

1 Like

Hi @Isq ,

are you actually calling the processFolder in your full code? The snippet above contains just the function definition, but no function call like processFolder(MainDir). Also, are you loading the images somewhere?

You can also have a look at the Batch processing template in the script editor for help:
Templates > ImageJ 1.x > Batch > ProcessFolder (IJ1 Macro)
[Note: it is a template, the actual image loading stil needs to be added]

1 Like

Ah yes, I am basically changing an existing, very very long macro, by making it work on all these folders, but I will keep in mind actually using processFolder when I come to that, thanks! :slight_smile: