Program runs function without calling it

Hi everyone!

I am currently writing a plugin, and I am trying to create some functions for it, and I was wondering if anyone knew why it was calling the function “stain1()” in line 3 even though I call the function in line 4?

And on a side note, does anyone know how to add pictures to an interface using the macro language?
For example, is there a way to add a logo to the plugin’s interface?

Thank you so much, I appreciate all the help and advice!

Dialog.addChoice("Antibody/Stain 1:",newArray("Olig2","NeuN"));

	a1 = Dialog.getChoice();
	if (a1 == "Olig2") stain1 = olig2(); else if (a1 == "NeuN") stain1 = neun(); // line 3

stain1();  // line 5

I assume this happened because you have a variable stain1 in line 3. Give it a different name and let us know if this helps :slightly_smiling_face:

Oh sorry, I realized my typo!

Stain1 is just a variable name, and the function should just be olig2(), but I am assigning the olig2() function to the variable stain1, so essentially I have already written the functions for olig2() and neun(), and I am assigning the function that the user chooses to the variable, and calling the function later on.


if (a1 == "Olig2") stain1 = olig2(); // assigns the variable the function

stain1; // calls the variable/function

The IJ1 Macro language doesn’t work like that. By typing stain1 = olig2() you assign the return value of olig2() to stain1, so the method olig2 will be called, as you observe.

I’d recommend to switch to a full-fledged scripting language, such as JavaScript, Groovy or Jython.

For example, the following script shows a similar issue to yours when executed as IJ1 Macro, but works as expected when run as JavaScript in the script editor:

function olig2() { 
	print("Function called");

stain1 = olig2;

print("Before function call");
print("After function call");
1 Like

Ahhh ok, that makes a lot more sense! I didn’t know that I couldn’t assign a function to a variable, but that clears up my confusion. I think I know how to go around this issue now. Thank you so much! Your help is much appreciated :slight_smile:

1 Like