Incompatible Javascript editors

Dear All,
I’m using ImageJ 1.51e under Java build 1.8.0_102-b14

If I run Plugins > New > JavaScript and then I open ‘Gamma_adjuster.js’ (one of the Examples provided) this runs perfectly, showing the classical clown picture associated with the gamma slider.

By if I copy&paste the same ‘Gamma_adjuster.js’ code as javascript in the script editor (Plugins > New > Macro) I get errors.

I’ve read something about the different engines of the two editors, but I wander if there is some workaround to use the second one (I mean: Plugins > New > Macro - selecting Javascript language), which is undoubtely much more friendly as this (and only this) makes it possible to

  1. highlight code expressions,
  2. edit/comparing several scripts at the same time, and
  3. running selected bloks of code.
    Thanks

Read this:

Unlike ImageJ 1.x, ImageJ2 (and therefore Fiji) does not automatically import any classes. Consequently, scripts written for ImageJ 1.x will not run in ImageJ2
without adding the proper imports.
The rationale is that the auto-import feature is not safe. What if two
classes of the same name live in two different packages? Or if a new
class is introduced that makes formerly unique names ambiguous? All of a
sudden, all of the scripts that reference the original class no longer
work. In short: auto-imports are dangerously imprecise.

You have to import the classes explicitly when using the script editor!

For the most common stuff like e.g. IJ, the RoiManager or GenericDialog the following lines would be required:

importClass(Packages.ij.IJ);
importClass(Packages.ij.plugin.frame.RoiManager);
importClass(Packages.ij.gui.GenericDialog);

See also:
https://imagej.nih.gov/ij/developer/javascript.html

Thanks Bio7,
but my intention was that of using ImageJ2 (Fiji) scripts in the ‘ImageJ1’ macro editor, which provides a much better interface.

Do you mean this editor (FIJI)?

Which could do what you described:

  1. highlight code expressions,
  2. edit/comparing several scripts at the same time, and
  3. running selected blocks of code.

This is the ImageJ1 editor:

1 Like

Just added the imports to the script and it works as expected:

// This script demonstrates how to interactively update
//  an image that has been added to a GenericDialog.
importClass(Packages.ij.IJ);
importClass(Packages.ij.gui.GenericDialog);
importClass(Packages.ij.gui.DialogListener);

img = IJ.openImage("http://imagej.nih.gov/ij/images/clown.jpg");
ip = img.getProcessor();
ip.snapshot();
listener = new DialogListener {
    dialogItemChanged : function(gd, event) {
        gamma = gd.getNextNumber();
        ip.reset();
        ip.gamma(gamma);
        img.setProcessor(ip);
        gd.repaint();
        return true;
    }
};
gd = new GenericDialog("Gamma Adjuster");
gd.addImage(img);
gd.addSlider("Gamma:", 0.05, 5.0, 1);
gd.addDialogListener(listener);
gd.showDialog();

At the wiki page about JavaScript on imagej.net, all larger example snippets include the necessary imports to be runnable in the Script Editor.

1 Like

Thanks to all.
It’s never too late to understand how things are.

1 Like