Macro: Conditional statements (if x=true)

Dear Forum,

I’ve wrote a little macro wich helpes me exporting images from different software (ThorImage).
Basically the Macro asks me wich slices I want to export from an Image Sequence.

Within the macro I implemented as well three checkboxes in a dialog window.

1st Ckechbox asks if I want to export as a stack.
2nd Checkbox asks if I want to export as Image Sequence (this part Istill have to develop)
3rd Checkbox asks if I want to close Imagej (Fiji) after the export (at this point the “if” funktion always says “true”, and I can’t figure out were I did something wrong.)

This is my first Code, which means I’m not so skilled in writing makros.
However, I think I’m doing something wrong with the “if-Funktion” in ImageJ Macro.

Can you point me in the right direktion?

this is my code so far:

showMessageWithCancel("Bitte warten...","Wenn der Bilderstapel aus ThorImage geladen wurde, klicken Sie auf OK.");
setMinAndMax(20.0000, 80.0000); //Contrast
Dialog.create("ThorImage Import");
Dialog.addMessage("Tragen Sie hier die Dimensionen ein.")
Dialog.addNumber("Erste Ebene:", 80); // first slice
Dialog.addNumber("Letzte Ebene:", 110) // last slice
Dialog.addNumber("X-FOV in mm:", 8.00); // Field of View Value from Data aqistion
Dialog.addCheckbox("Speichere als Tiff Bilderstapel", true); // Checkbox 1, save as tiff stack
Dialog.addCheckbox("Speichere alle Einzelbilder", false); // Checkbox 2, save as Image Sequence (code is in progress)
Dialog.addCheckbox("Exit ImageJ after Export", true); // Checkbox 3;
f = Dialog.getNumber(); // for slice keeper, first image
l = Dialog.getNumber(); // for slice keeper, last image
r = Dialog.getNumber(); // for calculation (line 16) 
h = 1024 // hight in pixels
result = r*1024/3.52; //calculation of widht pixels
w = result; // for "Size" (line 24) 
stack = Dialog.getCheckbox(); //Checkbox 1 ( Save as Stack)
single = Dialog.getCheckbox(); // Checkbox 2 (Image Sequence)
end = Dialog.getCheckbox(); // Checkbox 3 (exit)
run("Slice Keeper", "first=f last=l increment=1");
z = nSlices; // result of Slice keeper
run("Size...", "width=w height=h depth=z average interpolation=None");
run("8-bit"); // converting to 8 Bit to save diskspace
if (stack==true); { //only save if Checkbox 1 is "true" (checked)
dir = getDirectory("Choose a Directory"); //destination of export
title = File.getName(dir); //grabs the last part of the Directory for the filename
saveAs("Tiff", dir+title); 
// old line showMessageWithCancel("Export erfolgreich","Der Export war erfolgreich, mit einem Klick auf OK beenden Sie FiJi.");
if (end==true); {
//showMessage("true"); it always displayes "true". Why?

Some parts of the code is in german, but I tried to comment so you are able to understand what I tryed to do with the code :slight_smile:

Thank you for your help.



Hi Tobias,

it’s usually a good idea to try to cut down an issue to a minimal example (also referred to as MCVE: minimal, complete, and verifiable example), so that others don’t need to sift through all your code to help you.

In your macro, you are using if in a wrong way: by terminating with an ;, the if statement is closed, and the code continues in all cases. Have a look at the macro language documentation.

Below is a minimal example macro illustrating the correct use of if:


Dialog.addCheckbox("First checkbox parameter", true);
Dialog.addCheckbox("Second checkbox parameter", true);;
first = Dialog.getCheckbox();
second = Dialog.getCheckbox();

// One-line usage
if (first) print ("first is true");

// Block usage
if (second) {
    print ("second is true");

Note that (stack==true) is equivalent to (stack), because your variable stack contains a boolean value.


Dear Jan,

thank you for your answer. I must have overseen the ; at the end of line.
I’ll keep that in mind. As well I’ll in the future I shorten my code to the part wich I have trouble with.

Thank you.