Posted by
Wayne Rasband on
Aug 01, 2009; 8:18pm
URL: http://imagej.273.s1.nabble.com/common-functions-installing-multiple-macro-files-tp3691526p3691527.html
On Jul 31, 2009, at 10:00 AM, Benjamin Gilbert wrote:
> I have a collection and macros and functions in a single macro text
> file. I
> would now like to translate these routines into a toolset, which
> requires
> making a single macro file for each operation and saving it in the
> ImageJ/plugins folder.
>
> The original approach allowed different macros to use common
> functions, but
> it seems that an individual tool must be completely self-contained.
> I was
> hoping that I could install a macro containing all common functions
> that
> would be accessible to tool macros but this does not work. In
> general, it
> appears that only a single macro file can be installed at a time.
>
> Is there a way to do this? I don't want to have multiple instances
> of the
> same functions in different macro files.
You can do this by adding the common functions to the macro language
by calling the Interpreter.setAdditionalFunctions() method. Here is an
example.
Create a folder in the plugins folder named MyMacros. Add these three
files (CommonFunctions, Macro1 and Macro2) to it:
CommonFunctions.txt
function f1() {
print("f1");
}
function f2() {
print("f2");
}
Macro1.ijm
print("Macro1");
f1();
f2();
Macro2.ijm
print("Macro2");
f1();
f2();
Then add this code to the StartupMacros file
(Plugins>Macros>StartupMacros)
macro "AutoRun" {
path = getDirectory("plugins")+"MyMacros/CommonFunctions.txt";
functions = File.openAsString(path);
call("ij.macro.Interpreter.setAdditionalFunctions", functions);
}
save it and restart ImageJ. You will now have two commands (Macro1 and
Macro2) in the Plugins>MyMacros submenu implemented by macros that
share the functions in CommonFunctions. CommonFunctions has a .txt
extension and no underscore in the name so it does not appear in the
Plugins>MyPlugins submenu. Macro1 and Macro2 have .ijm (ImageJ Macro)
extensions so they do appear even though they do not have an
underscore in the name.
You may decide that it's easier and better to keep the functions and
macros in a single file (toolset) located in the ImageJ/macros/
toolsets folder. That way the macros and functions can share global
variables, you can easily add keyboard shortcuts, the toolset can
contain tools, toolbar menus and action tools as well as macros, and
you can have multiple toolsets, selected from the ">>" toolbar menu.
-wayne