Possible to call a javascript script or function (w/ arguments) from a macro?

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

Possible to call a javascript script or function (w/ arguments) from a macro?

lechristophe
Hi,

I'd like to know if it is possible to call some javascript from a macro in
ImageJ. What I want to do is call the javascript, passing some arguments to
it (like a filepath), let it run, and return to the macro for further
processing. How should I proceed ?

Thanks for your help,

Christophe

--
Christophe Leterrier
Researcher
Axonal Domains Architecture Team
CRN2M CNRS UMR 7286
Aix Marseille University, France

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Possible to call a javascript script or function (w/ arguments) from a macro?

ctrueden
Hi Christophe,

> I'd like to know if it is possible to call some javascript from a macro

Did you see the eval("script", javascript) method?
    http://imagej.net/developer/macro/functions.html#eval

-Curtis


On Wed, Mar 12, 2014 at 2:36 PM, Christophe Leterrier <
[hidden email]> wrote:

> Hi,
>
> I'd like to know if it is possible to call some javascript from a macro in
> ImageJ. What I want to do is call the javascript, passing some arguments to
> it (like a filepath), let it run, and return to the macro for further
> processing. How should I proceed ?
>
> Thanks for your help,
>
> Christophe
>
> --
> Christophe Leterrier
> Researcher
> Axonal Domains Architecture Team
> CRN2M CNRS UMR 7286
> Aix Marseille University, France
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Possible to call a javascript script or function (w/ arguments) from a macro?

lechristophe
Hi Curtis,

I have seen it, but how to pass an argument to the script? Plus, I'd prefer
to keep the javascript as a separate file because it is used outside the
macro too.

Thanks,

Christophe


2014-03-12 21:59 GMT+01:00 Curtis Rueden <[hidden email]>:

> Hi Christophe,
>
> > I'd like to know if it is possible to call some javascript from a macro
>
> Did you see the eval("script", javascript) method?
>     http://imagej.net/developer/macro/functions.html#eval
>
> -Curtis
>
>
> On Wed, Mar 12, 2014 at 2:36 PM, Christophe Leterrier <
> [hidden email]> wrote:
>
> > Hi,
> >
> > I'd like to know if it is possible to call some javascript from a macro
> in
> > ImageJ. What I want to do is call the javascript, passing some arguments
> to
> > it (like a filepath), let it run, and return to the macro for further
> > processing. How should I proceed ?
> >
> > Thanks for your help,
> >
> > Christophe
> >
> > --
> > Christophe Leterrier
> > Researcher
> > Axonal Domains Architecture Team
> > CRN2M CNRS UMR 7286
> > Aix Marseille University, France
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> >
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Possible to call a javascript script or function (w/ arguments) from a macro?

ctrueden
Hi Christophe,

> I have seen it, but how to pass an argument to the script?

You could pass args using string concatenation; e.g.:

    eval("script", "print(" + myVariable + ")");

> I'd prefer to keep the javascript as a separate file because it is used
> outside the macro too.

OK, apparently the "runMacro" command can run JavaScript, too:

    runMacro("/Users/curtis/Desktop/hi.js");

Where the contents of that file are:

    IJ.showMessage("Hello!");

I figured that out by trying it with Plugins > Macros > Run with the Macro
Recorder active.

Regards,
Curtis



On Wed, Mar 12, 2014 at 3:21 PM, Christophe Leterrier <
[hidden email]> wrote:

> Hi Curtis,
>
> I have seen it, but how to pass an argument to the script? Plus, I'd prefer
> to keep the javascript as a separate file because it is used outside the
> macro too.
>
> Thanks,
>
> Christophe
>
>
> 2014-03-12 21:59 GMT+01:00 Curtis Rueden <[hidden email]>:
>
> > Hi Christophe,
> >
> > > I'd like to know if it is possible to call some javascript from a macro
> >
> > Did you see the eval("script", javascript) method?
> >     http://imagej.net/developer/macro/functions.html#eval
> >
> > -Curtis
> >
> >
> > On Wed, Mar 12, 2014 at 2:36 PM, Christophe Leterrier <
> > [hidden email]> wrote:
> >
> > > Hi,
> > >
> > > I'd like to know if it is possible to call some javascript from a macro
> > in
> > > ImageJ. What I want to do is call the javascript, passing some
> arguments
> > to
> > > it (like a filepath), let it run, and return to the macro for further
> > > processing. How should I proceed ?
> > >
> > > Thanks for your help,
> > >
> > > Christophe
> > >
> > > --
> > > Christophe Leterrier
> > > Researcher
> > > Axonal Domains Architecture Team
> > > CRN2M CNRS UMR 7286
> > > Aix Marseille University, France
> > >
> > > --
> > > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> > >
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> >
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Possible to call a javascript script or function (w/ arguments) from a macro?

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by lechristophe
On Mar 12, 2014, at 4:36 PM, Christophe Leterrier wrote:

> Hi,
>
> I'd like to know if it is possible to call some javascript from a macro in
> ImageJ. What I want to do is call the javascript, passing some arguments to
> it (like a filepath), let it run, and return to the macro for further
> processing. How should I proceed ?

You can call a JavaScript file and pass it an argument using the runMacro(path,arg) macro function. For example, the macro

     rtn = runMacro("test.js", "string argument");
     print("return="+rtn);

outputs

    arg=string argument
    return=this is the return value

when the file "test.js" (assumed to be in the Image/macros folder) contains

    print("arg="+getArgument());
    "this is the return value";

The JavaScript code retrieves the argument by calling getArgument() and the value of the last expression evaluated ("this is the return value") is returned to the calling macro.

-wayne

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Possible to call a javascript script or function (w/ arguments) from a macro?

lechristophe
Thanks Wayne, that is exactly what I was looking for!

Cheers,

Christophe


2014-03-13 4:32 GMT+01:00 Rasband, Wayne (NIH/NIMH) [E] <
[hidden email]>:

> On Mar 12, 2014, at 4:36 PM, Christophe Leterrier wrote:
>
> > Hi,
> >
> > I'd like to know if it is possible to call some javascript from a macro
> in
> > ImageJ. What I want to do is call the javascript, passing some arguments
> to
> > it (like a filepath), let it run, and return to the macro for further
> > processing. How should I proceed ?
>
> You can call a JavaScript file and pass it an argument using the
> runMacro(path,arg) macro function. For example, the macro
>
>      rtn = runMacro("test.js", "string argument");
>      print("return="+rtn);
>
> outputs
>
>     arg=string argument
>     return=this is the return value
>
> when the file "test.js" (assumed to be in the Image/macros folder) contains
>
>     print("arg="+getArgument());
>     "this is the return value";
>
> The JavaScript code retrieves the argument by calling getArgument() and
> the value of the last expression evaluated ("this is the return value") is
> returned to the calling macro.
>
> -wayne
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Possible to call a javascript script or function (w/ arguments) from a macro?

lechristophe
Hi Wayne and all the ImageJers,

The runMacro(path, argument) command works fine to call a javascript via
its path from a macro. I'm now trying to do the same from a javascript,
i.e. calling a javascript (as a file with its path) from a javascript.

I used the following method which works with a js file on a Mac (OSX 10.9,
Java 1.6) :
IJ.runMacroFile(path, argument);

but on a PC (XP, Java 1.6), the argument isn't passed to the second
javascript and I get an error about the string litteral not being
terminated (in french, so I'm not sure about my translation).

What do I do wrong? Is there a better way to call a javascript as a file
from another javascript properly?

Thanks for your help,

Christophe


2014-03-13 13:31 GMT+01:00 Christophe Leterrier <
[hidden email]>:

> Thanks Wayne, that is exactly what I was looking for!
>
> Cheers,
>
> Christophe
>
>
> 2014-03-13 4:32 GMT+01:00 Rasband, Wayne (NIH/NIMH) [E] <
> [hidden email]>:
>
> On Mar 12, 2014, at 4:36 PM, Christophe Leterrier wrote:
>>
>> > Hi,
>> >
>> > I'd like to know if it is possible to call some javascript from a macro
>> in
>> > ImageJ. What I want to do is call the javascript, passing some
>> arguments to
>> > it (like a filepath), let it run, and return to the macro for further
>> > processing. How should I proceed ?
>>
>> You can call a JavaScript file and pass it an argument using the
>> runMacro(path,arg) macro function. For example, the macro
>>
>>      rtn = runMacro("test.js", "string argument");
>>      print("return="+rtn);
>>
>> outputs
>>
>>     arg=string argument
>>     return=this is the return value
>>
>> when the file "test.js" (assumed to be in the Image/macros folder)
>> contains
>>
>>     print("arg="+getArgument());
>>     "this is the return value";
>>
>> The JavaScript code retrieves the argument by calling getArgument() and
>> the value of the last expression evaluated ("this is the return value") is
>> returned to the calling macro.
>>
>> -wayne
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Possible to call a javascript script or function (w/ arguments) from a macro?

lechristophe
Dear Wayne,

After fiddling with Fiji installs to be sure that I have exactly the same
Fiji on both machines (with ImageJ 1.48t), I can confirm that the error
happens on PC and not on Mac (both with Java 1.6, the _24 bundled with Fiji
on the PC and the _65 on OSX).

On the mac the runMacroFile() command works ok, and on the PC I get:
Error: unterminated string literal (<Unknown source>#1) in <Unknown source>
at line number 1
and it looks like the argument isn't passed to the target javascript.

Thanks for your help,

Christophe



2014-04-07 19:07 GMT+02:00 Rasband, Wayne (NIH/NIMH) [E] <
[hidden email]>:

> On Apr 7, 2014, at 7:18 AM, Christophe Leterrier wrote:
>
> > Hi Wayne and all the ImageJers,
> >
> > The runMacro(path, argument) command works fine to call a javascript via
> > its path from a macro. I'm now trying to do the same from a javascript,
> > i.e. calling a javascript (as a file with its path) from a javascript.
> >
> > I used the following method which works with a js file on a Mac (OSX
> 10.9, Java 1.6) :
> > IJ.runMacroFile(path, argument);
> >
> > but on a PC (XP, Java 1.6), the argument isn't passed to the second
> > javascript and I get an error about the string litteral not being
> > terminated (in french, so I'm not sure about my translation).
> >
> > What do I do wrong? Is there a better way to call a javascript as a file
> > from another javascript properly?
>
> Dear Christophe,
>
> Are you using the same version of ImageJ on both computers?
>
> Best regards,
>
> -wayne
>
>
> >
> > Thanks for your help,
> >
> > Christophe
> >
> >
> > 2014-03-13 13:31 GMT+01:00 Christophe Leterrier <
> [hidden email]>:
> > Thanks Wayne, that is exactly what I was looking for!
> >
> > Cheers,
> >
> > Christophe
> >
> >
> > 2014-03-13 4:32 GMT+01:00 Rasband, Wayne (NIH/NIMH) [E] <
> [hidden email]>:
> >
> > On Mar 12, 2014, at 4:36 PM, Christophe Leterrier wrote:
> >
> > > Hi,
> > >
> > > I'd like to know if it is possible to call some javascript from a
> macro in
> > > ImageJ. What I want to do is call the javascript, passing some
> arguments to
> > > it (like a filepath), let it run, and return to the macro for further
> > > processing. How should I proceed ?
> >
> > You can call a JavaScript file and pass it an argument using the
> runMacro(path,arg) macro function. For example, the macro
> >
> >      rtn = runMacro("test.js", "string argument");
> >      print("return="+rtn);
> >
> > outputs
> >
> >     arg=string argument
> >     return=this is the return value
> >
> > when the file "test.js" (assumed to be in the Image/macros folder)
> contains
> >
> >     print("arg="+getArgument());
> >     "this is the return value";
> >
> > The JavaScript code retrieves the argument by calling getArgument() and
> the value of the last expression evaluated ("this is the return value") is
> returned to the calling macro.
> >
> > -wayne
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> >
> >
>
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html