Macro call function defined in another file?

Hi there,

I’m putting together a set of scripts (ijm files) and would like call the same function in each of those.

So to make maintenance/updating etc easier, I’d like to put the function in a separate file by itself.

So I’m wondering how, from an ijm macro/script file do you call an ijm function defined in another file?

1 Like

Good day!

I’d just call another ImageJ macro instead of a function, i.e. I’d use the other macro as the desired function.

From your main macros you call another macro by using the ImageJ-function:

runMacro("name of the external macro", "string with parameters to pass");

The external macro should look like this:

macro "name of the external macro" {
	print( getArgument() ); // do what you need to do here with the parameters returned by getArgument()



1 Like

We (@oodegard,@Christian_Tischer) in fact had the same question. It feels a bit sub-optimal to be only able to pass on one big string as a “function argument”, which then one needs to parse. Isn’t there a way to actually call a proper function with comma separated arguments in an external macro?
@haesleinhuepf @ctrueden @Fabrice_Cordelieres
Thanks a lot!

Hey @Christian_Tischer,

I think the general idea behind this was: If you have something reusable, please make a plugin out of this and call it from macros. Macros should be only the top-layer; the workflow let’s say. Components should not be written in macro.

If macros call macros, which call macros, which call macros, which call macros… things become complicated to maintain. Also macro might not be the best programming language we have available in the ImageJ universe.

I recently saw a presentation where somebody suggest to NOT call Jupyter notebooks from Jupyter notebooks. The better way is to structure code in a library, clean it up and even distribute it to others. In the ImageJ-world this unfortunately means translating code from one language to another.

I hope that helps.



Two comments:

  • Depending on how you write your macro, you don’t need to parse anything. If you setup your called macro using script parameters (or Dialog.* functions, for that matter), you won’t have to use any getArgument() or similar, but just use the variables defined in your parameters.
    You’ll still need to put together the option string in the calling macro, though.

  • When using scripting languages other than IJ1 macros, you can also provide a Map<String, Object> to the ScriptService to set your parameters:

    parameterMap = ["inputString":  "foo", "inputNumber": 42]"MyScript.groovy", true, parameterMap)

Not to my knowledge. ImageJ macro function definitions do not persist across macro files—e.g. adding a function to the StartupMacros.txt does not make it callable elsewhere.

If you are trying to write macros that call macros, then your macro is too complicated. Switch to a more complete scripting language instead. I know people find macros convenient, but it really is not a proper programming language compared to every other choice. Rather than continuing to build stuff on top of macro, we would be better served developing a convenience library that could be shipped with ImageJ that makes all the built-in macro functions also available as global functions from all the other languages.


2 posts were split to a new topic: How to promote scripting over macros