Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xxarmoxx

macrumors newbie
Original poster
Jun 8, 2007
18
0
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

xxarmoxx

macrumors newbie
Original poster
Jun 8, 2007
18
0
can you give me an example? Im not too sure what you are talking about. Thanks :cool:
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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)...
Code:
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: @"path to some shell command"];

    NSArray *arguments = [NSArray arrayWithObjects: @"argument 1", @"argument2", nil];
    [task setArguments: arguments];

    [task launch];
    [task waitUntilExit];
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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 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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.