Posted by
ctrueden on
Aug 07, 2007; 9:24pm
URL: http://imagej.273.s1.nabble.com/Running-BASIC-program-from-w-in-a-Macro-tp3698695p3698698.html
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!
> >
>