Temporarily store integer variable while macro code is running (managed by java code)


Hi all,

I tried to make the title simple… My goal is to create some java code, called from the macro code, to interact with variables while the macro code is running. Actually, I would like to know first if it is possible…
Note that here, I want to implement this in the macro code for users who do not know anything about java language. Obviously, it is simple to do it with some pure java code.
I’ll try to give an example:

// Macro language
run(“Plugin-setVariable”, “variableA=value”);

-some macro code-

run(“Plugin-doAction1-if-variableA-equals”, “parameters…”); // would redefine the variable A with a new value

-some macro code-

run(“Plugin-doAction2-if-variableA-equals”, “parameters…”); // would redefine the variable A with a new value, again


Maybe I missed something with what we can do with java…
Thanks for your help!

PS: I already thought about an alternative which would be to read/write a text file, with variable A info…



We use a simple interface to set and get data from an open text window in ImageJ.

setData("Another Key", someOtherVariable);
someVariable = getData("Some Key");

This works very well, if you’d like to see the macro code, you can download my group’s Update Site (PTBIOP) and open the BiopLib.ijm file inside the plugins>BIOP folder . And moving this so that plugins can benefit from it too would be rather trivial.

But otherwise I could think of you using the IJ.Prefs to get and set some hidden preference variables that coulld be modified by each plugin.


Hi @oburri,

Thanks for the share. I was also thinking about the text window, but the problem is that the window won’t close if macro code crashes. Of course, you would detect the text window which is still opened in a second run of the macro…
I thought about the IJ.Prefs as well, but I was wondering how risky it could be to use it.
Did you try it before?



IJ.Prefs is the good old IJ1 way to go and it’s quite reliable. It also teaches people how to store variables when writing their plugins.

The only thing is that your keys should have rather unique identifiers internally, so that your variableA should be internally named something like

IJ.Prefs.set("setvariable.plugin.variable.a", someValue);

Which looks a bit messy for non-coders at first.

Then you will have to offer some wrapper functions as there is no setPreferences or get Preferences in the ImageJ Macro Language, so you need to use the call() method

// Set a variable into ImageJ Prefs
call("ij.Prefs.set", "test.variable.1.demo.var", 10);

// Recover a variable from ImageJ Prefs, return "unset" if it was not there
val = call("ij.Prefs.get", "test.variable.1.demo.var","unset");


However, as for the text window, these will not be temporary, they will remain in the “IJ_Props.txt” and “IJ_Prefs.txt” files even if the plugins or macros crash.

Finally, the text window is nice from a teaching point of view as people can actually see the variables being affected and changed as they are being used, which is more interactive than the hidden IJ.Prefs elements.




Just to add to this, the ImageJ2 equivalent of this is the PrefService that can be used in any scripting language using Script Parameters, e.g. in this Groovy snippet:

// @PrefService prefs

prefs.put("myPlugin_Preference", "foo")

println prefs.get("myPlugin_Preference")


Thanks again for the details about Prefs.
I will actually use this solution rather than text window because I want to hide this process.

Thanks @imagejan for the extra info about IJ2…