Run macro by name from external file (.ijm)

Dear all,

consider I’ve multiple macros called A and B in a file called Foo.ijm:

macro "A" {
}

macro "B" {
}

How can I call from another macro say Bar.ijm only let’s say macro B?

I know this syntax:
runMacro("Foo", "some arg, another arg");

But I’m not sure how I can run a specific macro by name from an external file.

Best wishes,
SG

Maybe this section of the doc answers the question :wink:

1 Like

Sadly now. I have multiple macros in a single .ijm file and I need to call them individually.

Hmm so you need some kind of import functionality for functions between macros.
Unfortunately I think @haesleinhuepf mentioned it in a previous post, there is no such functionality for IJ-macros.

Your alternative is thus either to split the macro in separate files (one file per function) or to translate the macro to one of the scripting language (Jython for instance).
In the latter case, the wiki (link above) has a well documented section about scripting.

1 Like

So, if we are on it right now:

Is there any general recommendation which language to use for smallish tasks? I usually use IJM because I get support for ImageJ 1.x and can quiet easily add the macros to the macro menu. But I think no one of our collaborators uses ImageJ 1.x actually, all use Fiji, so it might be worth to convert to a different scripting language if this will have additional benefits modulo the maybe more familiar syntax of Ruby / Python.

Hi @stephanmg!

This is a really interesting challenge. I don’t know of a direct way of calling a macro within a macro set (that has not been installed, otherwise you just execute: run("macro name")).

But you can certainly make a (somewhat convoluted) workaround. Consider this version of a macro collection, where macros have been moved to functions, and has a little extra header:

argument=getArgument();
//check inputs:
if(argument == "first_macro") first_macro();
if(argument == "second_macro") second_macro(); 
exit(); // avoids executing the rest on a runmacro() call

macro "First macro [F1]" {
	first_macro();
	}

macro "Second macro [F2]" {
	second_macro();
	}


function first_macro() {
	showMessage("The first macro has been run");
	}

function second_macro() {
	showMessage("The second macro has been run");
	}

You can install it and all the macros will be available a usual, even with shortcuts.
But you can also make an external call using:

runMacro(path+"macro collection.ijm", "first_macro");

Is this helpful?

Cheers!
Nico

2 Likes

Well @NicoDF, actually it is an interesting approach, but because this adds boilerplate code I won’t go for this approach.

All the scripting language are compatible with ImageJ1 functions available via the ImageJ API.
It is sometimes a bit challenging to find the correspondance between a IJM command and the API command but if you select Java as language in the recorder you should get a good hint.

You can also put scripts of various scripting language into subfolder of the plugin folder, as long as they contain a space they will show up in the menu folder.

I personally like Jython but this choice really depends on your existing skills and tastes.
Just with Jython, if you want to import functions between scripts, the imported script should reside in the subfolder Fiji.app/jars/Lib and get compiled to a .class upon import (delete the compiled version and restart Fiji if you update the script).

Good Luck !

1 Like

Fair enough :laughing:

1 Like

Okay, that makes a lot of sense. Thanks for the hint(s).