Login  Register

Re: Scripting IJ with javascript

Posted by Wayne Rasband on Feb 21, 2009; 9:25pm
URL: http://imagej.273.s1.nabble.com/Scripting-IJ-with-javascript-tp3693611p3693612.html

On Feb 21, 2009, at 10:02 AM, Gabriel Landini wrote:

> I have a couple of question regarding javascript and IJ:
>
> Q1:How do I enable at least one Undo?
> I created a js file, below, that erodes the white (as foreground)  
> (the exact
> erosion we discussed about a few days ago) :
> ----8<----
> erodeExact();
>
> function erodeExact() {
>  var imp = IJ.getImage();
>  var ip = imp.getProcessor();
>  ip.invert();
>  ip.erode();
>  ip.invert();
>  imp.updateAndDraw();
> }
> ----8<----
>
> However this process cannot be undone. Is there a way to return to the
> original image before running the script? If I execute ip.snapshot
> (); this is
> not restored with the "undo" found in the Edit menu. I guess that  
> there is no
> way of undoing this?

To support Undo, call both ip.snapshot() and Undo.setup(), as in this  
example:

     var imp = IJ.getImage();
     var ip = imp.getProcessor();
     ip.snapshot();
     Undo.setup(Undo.FILTER, imp);
     ip.invert();
     if (Prefs.blackBackground)
        ip.erode();
     else
        ip.dilate();
     ip.invert();
     imp.updateAndDraw();


> Q2: Is there a way of finding out what is the status of the
> Process>Binary>Options "black background"?
>
> This is recorded with:
> run("Options...", "iterations=1 black count=1");
>
> But how do I know whether "black" is true or false in javascript?

Access the public Prefs.blackBackground variable to find out if  
"Black Background" is true or false.

-wayne

>
> Many thanks in advance.
> Cheers
>
> Gabriel