Loading JavaScript functions

I want to load JavaScript functions from other files. For example, I want to save some common functions in a file, common.js. If I understand, this file should not show up in the plugins as it doesn’t have an underscore. I want to write scripts that do show up in the plugins and import functions. The tutorial says to use load. For example I wrote a script called normalize_one.js. This appears and I can run it. However when I try load(“common.js”); I get an error that the file common.js cannot be found. The file is in the same directory (plugins/JavaScript) as the other file.
Where are files being looked for? Do I need to add something to tell it the relative path or something like that?


The IJ BAR by @tferr has good examples for loading common functions from files in all scripting languages.

Have look at the comments in BARlib.js to see how it’s done.

Something like this should work for you:

load(IJ.getDirectory("plugins") + "JavaScript" + File.separator + "common.js");
1 Like


That was exactly the part that I was missing. I don’t know Java so I find it hard to find all the packages, classes and such.


@Adam_Webb It would be awesome if someone enhanced the JavaScript Scripting page of the ImageJ wiki to include the tip from @imagejan above. Care to give it a try? Editing the wiki is easy and fun! :smile:

1 Like