Scripting IJ with javascript

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Scripting IJ with javascript

Gabriel Landini
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?


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?

Many thanks in advance.
Cheers

Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Scripting IJ with javascript

Thomas Boudier
Hi Gabriel,

Why not use the duplicate function ?

erodeExact();

function erodeExact() {
var imp = IJ.getImage();
ip=imp.getProcessor();
ipcopy=ip.duplicate();
 ip.invert();
 ip.erode();
 ip.invert();
 imp.updateAndDraw();
 new ImagePlus("original",ipcopy).show();
}


Cheers,

Thomas

> 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?
>
>
> 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?
>
> Many thanks in advance.
> Cheers
>
> Gabriel
>
>
>  


--
/*****************************************************/
 Thomas Boudier, MCU Université Pierre et Marie Curie
 UMR 7101 / IFR 83. Bat A 328, Campus Jussieu
 Tél : 01 44 27 35 78  Fax : 01 44 27 25 08
/*****************************************************/


 
Reply | Threaded
Open this post in threaded view
|

Re: Scripting IJ with javascript

Wayne Rasband
In reply to this post by Gabriel Landini
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
Reply | Threaded
Open this post in threaded view
|

Re: Scripting IJ with javascript

Gabriel Landini
Thank you Wayne & Thomas,

On Saturday 21 February 2009, Rasband Wayne wrote:
> To support Undo, call both ip.snapshot() and Undo.setup(), as in this
> example:

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

That is what I was looking for.

By the way I just noticed that one cannot re-edit a .js file via the
Plugins>Macros>Edit...  menu (expects .txt or .java extension).

One can drag and drop these, though.

I guess that this needs changing line 179 of Compiler.java from (I have a
slightly old source):

} else if (!(lcName.endsWith(".java")||lcName.endsWith(".txt")||
lcName.endsWith(".ijm"))) {

to:

} else if (!(lcName.endsWith(".java")||lcName.endsWith(".js")||
lcName.endsWith(".txt")|| lcName.endsWith(".ijm"))) {


Many thanks again,


Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Scripting IJ with javascript

Gabriel Landini
In reply to this post by Wayne Rasband
Strangely, the same  js below does not execute when run from the menu:

Error: missing ; before statement (<Unknown source>#9) in <Unknown source> at
line number 9

but executes OK from a macro window.

----8<----
erodeExact();

function erodeExact() {
 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();
}
----8<----

I tried adding { } after the "if" but makes no difference
Cheers

Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Scripting IJ with javascript

Wayne Rasband
The JavaScript code contains 5 invalid hidden characters. You can  
remove them using the Edit>Zap Gremlins command in the macro editor.

-wayne

On Feb 22, 2009, at 8:44 AM, Gabriel Landini wrote:

> Strangely, the same  js below does not execute when run from the menu:
>
> Error: missing ; before statement (<Unknown source>#9) in <Unknown  
> source> at
> line number 9
>
> but executes OK from a macro window.
>
> ----8<----
> erodeExact();
>
> function erodeExact() {
>  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();
> }
> ----8<----
>
> I tried adding { } after the "if" but makes no difference
> Cheers
>
> Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Scripting IJ with javascript

Gabriel Landini
On Sunday 22 February 2009, Rasband Wayne wrote:
> The JavaScript code contains 5 invalid hidden characters. You can
> remove them using the Edit>Zap Gremlins command in the macro editor.

Thanks Wayne,
I can see that now with a hexeditor...
I have no idea how those got in, but they are definitely there.

Cheers

G.