Call an ImageJ macro from NetBeans

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

Call an ImageJ macro from NetBeans

Philip Ershler
Hi,
        Is it possible to call an ImageJ macro from NetBeans?

Thanks, Phil
Reply | Threaded
Open this post in threaded view
|

Re: Call an ImageJ macro from NetBeans

Juanjo Vega
Sure, you can use:

IJ.runMacro(String macro)
IJ.runMacro(String macro, String arg)
IJ.runMacroFile(String name)
IJ.runMacroFile(String name, String arg)

Sincerely,

Juanjo.

On Sep 7, 2011, at 2:12 AM, Philip Ershler wrote:

> Hi,
> Is it possible to call an ImageJ macro from NetBeans?
>
> Thanks, Phil

------------------------------------------------------------
Juanjo Vega ([hidden email])

Unidad de Biocomputación. Laboratorio B-13.
Centro Nacional de Biotecnología. CNB-CSIC.
C\ Darwin, 3. Campus de Cantoblanco.
Universidad Autónoma de Madrid.
28049, Madrid, Spain.

http://www.cnb.csic.es
http://www.biocomp.cnb.csic.es

+34 91 585 4510

"Las mejores almas son capaces de los mayores vicios como de las mayores
virtudes, y aquellos que caminan despacio por el camino recto pueden
llegar más lejos que los que corren pero se apartan de él." - Discurso
del Método, René Descartes.
Reply | Threaded
Open this post in threaded view
|

Re: Call an ImageJ macro from NetBeans

Sullivan, Michael J (College of Med.)
In reply to this post by Philip Ershler
Hi Phil.  Yes, it's possible and not too hard.  There is actually a very good webpage about doing it:

http://rsb.info.nih.gov/ij/developer/NBTutorial.html

hth --- Mike


Michael Sullivan
Research Engineer
Vermont Lung Center
HSRF 227
University of Vermont



-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Philip Ershler
Sent: Tuesday, September 06, 2011 8:13 PM
To: [hidden email]
Subject: Call an ImageJ macro from NetBeans

Hi,
        Is it possible to call an ImageJ macro from NetBeans?

Thanks, Phil
Reply | Threaded
Open this post in threaded view
|

Re: Call an ImageJ macro from NetBeans

Philip Ershler
In reply to this post by Juanjo Vega
OK, I am obviously missing something. Here is my NetBeans code that attempts to call an ImageJ Macro

try {
    String name = "VideoImport.txt";
    String retval = "";
          retval = IJ.runMacro(name);  
        } catch (Exception ex) {
            Logger.getLogger(HTS_UI.class.getName()).log(Level.SEVERE, null, ex);
        }

Here is the contents of my macro file VideoImport.txt that according to Wayne's code should be placed in the Plugins Folder.

macro "VideoImport [v]" {
run("Video");
close();
}

    /** Opens and runs the specified macro file, which is assumed to be in the plugins folder,
        on the current thread. Displays a file open dialog if <code>name</code> is an empty string. */

    public void run(String name) {
        Thread thread = Thread.currentThread();
        String threadName = thread.getName();
        if (!threadName.endsWith("Macro$"))
            thread.setName(threadName+"Macro$");
        String path = null;
        if (name.equals("")) {
            OpenDialog od = new OpenDialog("Run Macro...", path);
            String directory = od.getDirectory();
            name = od.getFileName();
            if (name!=null) {
                path = directory+name;
                runMacroFile(path, null);
                if (Recorder.record) {
                    if (Recorder.scriptMode())
                        Recorder.recordCall("IJ.runMacroFile(\""+path+"\");");
                    else
                        Recorder.record("runMacro", path);
                }
            }
        } else if (name.startsWith("JAR:"))
            runMacroFromJar(name.substring(4), null);
        else if (name.startsWith("ij.jar:"))
            runMacroFromIJJar(name, null);
        else {
            path = Menus.getPlugInsPath() + name;
            runMacroFile(path, null);
        }
    }
If I install the macro in ImageJ and call it from there, it works fine. But if I try to call it from Netbeans, I get a dialog that says

Undefined variable in line 1.
<VideoImport> . txt

It seems to me that somewhere a connection is not being made.

Thanks, Phil

On Sep 7, 2011, at 3:20 AM, Juanjo Vega wrote:

> Sure, you can use:
>
> IJ.runMacro(String macro)
> IJ.runMacro(String macro, String arg)
> IJ.runMacroFile(String name)
> IJ.runMacroFile(String name, String arg)
>
> Sincerely,
>
> Juanjo.
>
> On Sep 7, 2011, at 2:12 AM, Philip Ershler wrote:
>
>> Hi,
>> Is it possible to call an ImageJ macro from NetBeans?
>>
>> Thanks, Phil
>
> ------------------------------------------------------------
> Juanjo Vega ([hidden email])
>
> Unidad de Biocomputación. Laboratorio B-13.
> Centro Nacional de Biotecnología. CNB-CSIC.
> C\ Darwin, 3. Campus de Cantoblanco.
> Universidad Autónoma de Madrid.
> 28049, Madrid, Spain.
>
> http://www.cnb.csic.es
> http://www.biocomp.cnb.csic.es
>
> +34 91 585 4510
>
> "Las mejores almas son capaces de los mayores vicios como de las mayores
> virtudes, y aquellos que caminan despacio por el camino recto pueden
> llegar más lejos que los que corren pero se apartan de él." - Discurso
> del Método, René Descartes.
Reply | Threaded
Open this post in threaded view
|

Re: Call an ImageJ macro from NetBeans

Juanjo Vega
Hello Philip,

As you run ImageJ from the Netbeans environment, it doesn't know where to find plugins and macros. You need to specify your plugins folder by setting the property like:

        System.setProperty("plugins.dir", "<path_to_imagej>/plugins");

I always place the following in my main class:

    static {
        System.setProperty("plugins.dir", "/home/juanjo/Desktop/ImageJ/plugins");
    }

That way I can run "new ImageJ()" from anywhere in my code with all the plugins loaded, and it's pretty useful when debugging.

Hope that helps,

Sincerely,

Juanjo.

PS: usually I place macros in the "macros" subfolder but it seems that it also works in the plugins one.


On Sep 7, 2011, at 11:57 PM, Philip Ershler wrote:

> OK, I am obviously missing something. Here is my NetBeans code that attempts to call an ImageJ Macro
>
> try {
>    String name = "VideoImport.txt";
>    String retval = "";
>          retval = IJ.runMacro(name);  
>        } catch (Exception ex) {
>            Logger.getLogger(HTS_UI.class.getName()).log(Level.SEVERE, null, ex);
>        }
>
> Here is the contents of my macro file VideoImport.txt that according to Wayne's code should be placed in the Plugins Folder.
>
> macro "VideoImport [v]" {
> run("Video");
> close();
> }
>
>    /** Opens and runs the specified macro file, which is assumed to be in the plugins folder,
>        on the current thread. Displays a file open dialog if <code>name</code> is an empty string. */
>
>    public void run(String name) {
>        Thread thread = Thread.currentThread();
>        String threadName = thread.getName();
>        if (!threadName.endsWith("Macro$"))
>            thread.setName(threadName+"Macro$");
>        String path = null;
>        if (name.equals("")) {
>            OpenDialog od = new OpenDialog("Run Macro...", path);
>            String directory = od.getDirectory();
>            name = od.getFileName();
>            if (name!=null) {
>                path = directory+name;
>                runMacroFile(path, null);
>                if (Recorder.record) {
>                    if (Recorder.scriptMode())
>                        Recorder.recordCall("IJ.runMacroFile(\""+path+"\");");
>                    else
>                        Recorder.record("runMacro", path);
>                }
>            }
>        } else if (name.startsWith("JAR:"))
>            runMacroFromJar(name.substring(4), null);
>        else if (name.startsWith("ij.jar:"))
>            runMacroFromIJJar(name, null);
>        else {
>            path = Menus.getPlugInsPath() + name;
>            runMacroFile(path, null);
>        }
>    }
> If I install the macro in ImageJ and call it from there, it works fine. But if I try to call it from Netbeans, I get a dialog that says
>
> Undefined variable in line 1.
> <VideoImport> . txt
>
> It seems to me that somewhere a connection is not being made.
>
> Thanks, Phil
>
> On Sep 7, 2011, at 3:20 AM, Juanjo Vega wrote:
>
>> Sure, you can use:
>>
>> IJ.runMacro(String macro)
>> IJ.runMacro(String macro, String arg)
>> IJ.runMacroFile(String name)
>> IJ.runMacroFile(String name, String arg)
>>
>> Sincerely,
>>
>> Juanjo.
>>
>> On Sep 7, 2011, at 2:12 AM, Philip Ershler wrote:
>>
>>> Hi,
>>> Is it possible to call an ImageJ macro from NetBeans?
>>>
>>> Thanks, Phil
>>
>> ------------------------------------------------------------
>> Juanjo Vega ([hidden email])
>>
>> Unidad de Biocomputación. Laboratorio B-13.
>> Centro Nacional de Biotecnología. CNB-CSIC.
>> C\ Darwin, 3. Campus de Cantoblanco.
>> Universidad Autónoma de Madrid.
>> 28049, Madrid, Spain.
>>
>> http://www.cnb.csic.es
>> http://www.biocomp.cnb.csic.es
>>
>> +34 91 585 4510
>>
>> "Las mejores almas son capaces de los mayores vicios como de las mayores
>> virtudes, y aquellos que caminan despacio por el camino recto pueden
>> llegar más lejos que los que corren pero se apartan de él." - Discurso
>> del Método, René Descartes.

------------------------------------------------------------
Juanjo Vega ([hidden email])

Unidad de Biocomputación. Laboratorio B-13.
Centro Nacional de Biotecnología. CNB-CSIC.
C\ Darwin, 3. Campus de Cantoblanco.
Universidad Autónoma de Madrid.
28049, Madrid, Spain.

http://www.cnb.csic.es
http://www.biocomp.cnb.csic.es

+34 91 585 4510

"Las mejores almas son capaces de los mayores vicios como de las mayores
virtudes, y aquellos que caminan despacio por el camino recto pueden
llegar más lejos que los que corren pero se apartan de él." - Discurso
del Método, René Descartes.
Reply | Threaded
Open this post in threaded view
|

Re: Call an ImageJ macro from NetBeans

Philip Ershler
I have made some headway with calling a macro from NetBeans. I'm no longer getting errors, but I still cannot make it work. Below is a snippet of code from my NetBeans project. Note that I can build up the name and location of the macro file exactly the way Wayne does in his code http://rsbweb.nih.gov/ij/developer/source/ij/plugin/Macro_Runner.java.html.

Wayne's Code

if (size<=0
 && !fullPath) {

            file = new File(System.getProperty("user.dir") + File.separator + name);
            size = (int)file.length();

My Code

    try {

    String retval;
    String name = "Video.ijm";
       
        System.out.println(System.getProperty("user.dir")+File.separator+name);
          retval = IJ.runMacroFile(name,null);
          System.out.println(retval);
        } catch (Exception ex) {
            Logger.getLogger(HTS_UI.class.getName()).log(Level.SEVERE, null, ex);
        }        
}  

The println statement produces

/Home2/fishdevelop/NetBeansProjects/High_Throughput_Scanner/Video.ijm

which is exactly where I located the macro. The code above seems to run without errors, but does not produce any result. I changed my macro to simply this.

run("Clown (14K)");

This still does not invoke any response from ImageJ. But if I install any one of my macros in ImageJ they run fine when selected from the Plugins->Macro menu. I am running ImageJ 1.44o 32 bit. EIther I still don't know what I am doing or something is broken in the Macro_Runner code. I did noticed this comment in Wayne's code.

 
/** Opens and runs the specified macro file on the current thread.
      The file is assumed to be in the macros folder unless
      <code>name</code> is a full path. ".txt"  is
      added if <code>name</code> does not have an extension. */

Could there be any issues with "the current thread"?

Thanks, Phil


On Sep 8, 2011, at 3:49 AM, Juanjo Vega wrote:

> Hello Philip,
>
> As you run ImageJ from the Netbeans environment, it doesn't know where to find plugins and macros. You need to specify your plugins folder by setting the property like:
>
>        System.setProperty("plugins.dir", "<path_to_imagej>/plugins");
>
> I always place the following in my main class:
>
>    static {
>        System.setProperty("plugins.dir", "/home/juanjo/Desktop/ImageJ/plugins");
>    }
>
> That way I can run "new ImageJ()" from anywhere in my code with all the plugins loaded, and it's pretty useful when debugging.
>
> Hope that helps,
>
> Sincerely,
>
> Juanjo.
>
> PS: usually I place macros in the "macros" subfolder but it seems that it also works in the plugins one.
>
>
> On Sep 7, 2011, at 11:57 PM, Philip Ershler wrote:
>