Problem with macro - ")" expected in line 3

Hi everyone,

I am really new to making macros for ImageJ. I have made a very basic one to try to automatically make a montage with some merged images out of a 4 channel picture. I however get an error message that I can’t seem to get fixed.

Error: ‘)’ expected in line 3:
run ( “Merge Channels…” , “c3=” - “+title+, c4=” C4 - “+title+, keep” ) ;

The entire macro is:

title = getTitle();
run("Split Channels");
run("Merge Channels...", "c3=["C1-" + title], c4=["C4-" + title] keep");
run("Merge Channels...", "c2=["C2-" + title], c4=["C4-" + title] keep");
run("Merge Channels...", "c1=["C3-" + title], c3=["C1-" + title] keep");
run("Merge Channels...", "c1=["C3-" + title], c2=["C2-"+title], c3=["C1-"+title], c4=["C4-"+title] keep");
run("Images to Stack", "name=Stack title=[] use");
saveAs("Tiff", "DIRECTORY_stack_" + title);
run("Make Montage...", "columns=4 rows=2 scale=1");
saveAs("Tiff", "DIRECTORY_montage_" + title);
close();
selectWindow("Stacks"+title);
close();

I hope someone can help me out, thanks in advance!

Best regards,
Jelle

The problem is the way you perform string concatenation in the Lines with Merge channel:
At the moment this is done like this:

run("Merge Channels...", "c2=["C2-" + title], c4=["C4-" + title] keep");

Where you want to insert a variable called title into the string c2=[C2- …
You need to delete the additional double quotation before C2- and C4- etc.
Then you need to add a plus sign after title and start the string again before the square bracket by inserting a double quotation:

run("Merge Channels...", "c2=[C2-" + title + "], c4=[C4-" + title + "] keep");

The aim is to generate a complete string with the variable inserted.
See how the String is opened and closed and the variable is added to this string with the plus sign operator. then again you add the plus and open the string again.
When you program in the macro editor have a look at the color.
Variables are black. Strings are in magenta.

Here expressed as test macro:

title = "name";
stringConcat = "c2=[C2-" + title + "], c4=[C4-" + title + "] keep";

print(stringConcat);

The log would show then:

c2=[C2-name], c4=[C4-name] keep

Hi Christopher,

Thank you so much! It works perfectly now! I am still struggling a bit with string concatenation, so I will read up on it some more.

Best regards,
Jelle

1 Like

It takes time and practice. I still remember the time I used to struggle with this as well.
At some point your brain will recognize these patterns automatically and the problems will be less.
But the wild missing or additional quotation, bracket or semi colon will be always coming and messing with the code :smiley:

Have a look here to brush up on the ImageJ macro language:

Yes I sure hope that it’ll improve over time! Thanks for the link :slight_smile:

Have a good weekend!

1 Like