Login  Register

Re: Running BASIC program from w/in a Macro

Posted by Wayne Rasband on Aug 08, 2007; 6:59pm
URL: http://imagej.273.s1.nabble.com/Running-BASIC-program-from-w-in-a-Macro-tp3698695p3698699.html

The ability to run a native command and get its output does seem useful
so I added an exec() function to the macro language in ImageJ 1.39c,
now available at <http://rsb.info.nih.gov/ij/notes.html>.

-wayne


On Aug 7, 2007, at 4:24 PM, Curtis Rueden wrote:

> Hi Robert,
>
> I briefly checked the ImageJ web site for a plugin that can execute
> system commands, but couldn't find anything, so I wrote one myself.
>
> Below is a short plugin that you can call from a macro. Save the text
> below as "System_Call.java" in the ImageJ folder. Use "Compile and
> Run" to compile the plugin, then see if you can call your BASIC
> program like you would from a command prompt. The ImageJ log window
> should show any output your program produces. Once you're sure it
> works, you can use the macro recorder to record the correct macro
> command for use within your macro.
>
> Wayne and others: this plugin seems like it could be generally useful
> to people, so feel free to put it up on the web, and/or modify it as
> you see fit.
>
> Cheers,
> Curtis
>
> --------
>
> //
> // System_Call.java
> //
>
> import ij.IJ;
> import ij.Macro;
> import ij.gui.GenericDialog;
> import ij.plugin.PlugIn;
> import java.io.*;
>
> /** Executes a system call. */
> public class System_Call implements PlugIn, Runnable {
>
>   private String cmd;
>   private Process p;
>   private BufferedReader pin;
>
>   public System_Call() { }
>
>   public System_Call(InputStream in) {
>     pin = new BufferedReader(new InputStreamReader(in));
>   }
>
>   public void run(String arg) {
>     // get command to execute
>     GenericDialog gd = new GenericDialog("Execute Command");
>     gd.addStringField("Command: ", "", 32);
>     gd.showDialog();
>     if (gd.wasCanceled()) return;
>     cmd = gd.getNextString();
>
>     // execute command
>     try {
>       p = Runtime.getRuntime().exec(cmd);
>     }
>     catch (IOException exc) {
>       exc.printStackTrace();
>     }
>
>     // feed output and error streams to log window
>     IJ.log("-- Execute command: " + cmd + " --");
>     Thread out = new Thread(new System_Call(p.getInputStream()));
>     Thread err = new Thread(new System_Call(p.getErrorStream()));
>     out.start();
>     err.start();
>
>     // wait for process to end
>     try {
>       p.waitFor();
>       out.join();
>       err.join();
>     }
>     catch (InterruptedException exc) {
>       exc.printStackTrace();
>     }
>     IJ.log("-- Command finished. --");
>   }
>
>   public void run() {
>     while (true) {
>       String line = null;
>       try {
>         line = pin.readLine();
>       }
>       catch (IOException exc) {
>         exc.printStackTrace();
>       }
>       if (line == null) break;
>       IJ.log(line);
>     }
>   }
>
> }
>
> On 8/7/07, Barton, Robert <[hidden email]> wrote:
>> Glad I could bring some amusement to your day Curtis. :-)
>>
>> The BASIC program is extremely short, but I was just going to use it
>> based upon my lack of familiarity with Java.
>>
>> I was hoping there was a macro function that I had overlooked that
>> would call the compiled program to run in the Command Prompt outside
>> of ImageJ rather than adapting an interpreter into a plugin, etc.
>>
>> ________________________________
>>
>> From: ImageJ Interest Group on behalf of Curtis Rueden
>> Sent: Tue 8/7/2007 2:57 PM
>> To: [hidden email]
>> Subject: Re: Running BASIC program from w/in a Macro
>>
>>
>>
>> Hi Robert,
>>
>> That's the most amusing request I've seen in a while. :-)
>>
>> My first thought was to find a BASIC interpreter written in Java that
>> you could adapt into a plugin and thus call from a macro. I found one
>> such interpreter called Cocoa (not to be confused with the Mac OS X
>> Cocoa): http://www.mcmanis.com/chuck/java/cocoa/index.html
>>
>> It would take some programming, but should be possible to execute
>> BASIC from within ImageJ. How useful that is depends on what you are
>> trying to do in BASIC, I suppose.
>>
>> On the other hand, if your BASIC program is short as you say, why not
>> just port it to Java?
>>
>> -Curtis
>>
>> On 8/7/07, Robert Barton <[hidden email]> wrote:
>>> Hi all,
>>>
>>> I was wondering if it is possible to open and run a short BASIC
>>> program
>>> from within a macro.
>>>
>>> Thanks!
>>>
>>
>