View Full Version : Findwindow on a mac
Jun 29, 2007, 05:11 PM
With windows you can use the function findwindow() in user32.dll to find running windows. Is there a way to do that with a mac? I want to use java to find the running terminal window and when I click buttons on my java gui I want certain commands to be inserted to the terminal. How can I make this happen?
Jun 29, 2007, 05:46 PM
Why would you want to use an actual Terminal window? You can just shell out and execute the commands you want.
If you really have to then I'd imagine you can shell out to AppleScript.
Jun 29, 2007, 06:50 PM
can you give me an example? Im not too sure what you are talking about. Thanks :cool:
Jun 29, 2007, 10:54 PM
Well, assuming you were asking for an example of running a shell program directly, rather than inserting stuff into a Terminal window (which is a terrible idea, in general)...
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"path to some shell command"];
NSArray *arguments = [NSArray arrayWithObjects: @"argument 1", @"argument2", nil];
[task setArguments: arguments];
Jun 30, 2007, 03:29 AM
Catfish_Man that is a lovely example. Unfortunately it's on Cocoa/Obj-C, not in Java!
In Java we'd use a method in the Runtime (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String)) class to spawn a Proccess object. Using that we could communicate with the subprocess via buffered readers. You could simply spawn a shell (say Bash) and then pipe it commands and read the results.