Using Terminal within Cocoa App.

  • Hello world,

    I want to run some App by using Terminal script.
    My Cocoa App can run Terminal by using NSTask as follow.

    NSTask *task = [[NSTask alloc] init];
    NSPipe *pipe = [NSPipe pipe];

    [task setStandardOutput:newPipe];
    [task setLaunchPath:@"/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal"];
    [task setArguments:[NSArray arrayWithObjects:[NSString stringWithFormat:@"cd %@", someApp_Path],nil]];
    [task launch];

    I expect to set Terminal command "cd Path..." (ex. cd /Users/Desktop/...), and excute the someApp,
    but there is no event.

    How can I develop my Cocoa App to set automatically Terminal command, and someApp executes?
    Is it impossible?
  • On 16 Jan 08, at 00:25, Caroline wrote:
    > I want to run some App by using Terminal script.
    > My Cocoa App can run Terminal by using NSTask as follow.

    This is not an appropriate use of NSTask.

    > NSTask *task = [[NSTask alloc] init];
    > NSPipe *pipe = [NSPipe pipe];
    >
    > [task setStandardOutput:newPipe];
    > [task setLaunchPath:@"/Applications/Utilities/Terminal.app/Contents/
    > MacOS/Terminal"];
    > [task setArguments:[NSArray arrayWithObjects:[NSString
    > stringWithFormat:@"cd %@", someApp_Path],nil]];
    > [task launch];
    >
    > I expect to set Terminal command "cd Path..." (ex. cd /Users/
    > Desktop/...), and excute the someApp,
    > but there is no event.

    Terminal is a Cocoa application. It doesn't accept command-line
    arguments like that.

    > How can I develop my Cocoa App to set automatically Terminal
    > command, and someApp executes?
    > Is it impossible?

    Use Apple Events.
  • Caroline wrote:

    > I want to run some App by using Terminal script.

    If you're wanting to run shell scripts in Terminal (as opposed to just
    executing them directly via NSTask), you need to send Terminal a 'do
    script' event. Example:

    OSStatus DoTerminalScript(char *utf8Script) {
        /*
          * Run a shell script in Terminal.app.
          * (Terminal.app must be running first.)
          */
        char *bundleID = "com.apple.terminal";
        AppleEvent evt, res;
        AEDesc desc;
        OSStatus err;

        // Build event
        err = AEBuildAppleEvent(kAECoreSuite, kAEDoScript,
                                typeApplicationBundleID,
                                bundleID, strlen(bundleID),
                                kAutoGenerateReturnID,
                                kAnyTransactionID,
                                &evt, NULL,
                                "'----':utf8(@)", strlen(utf8Script),
    utf8Script);
        if (err) return err;
        // Send event and check for any Apple Event Manager errors
        err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
        AEDisposeDesc(&evt);
        if (err) return err;
        // Check for any application errors
        err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &desc);
        AEDisposeDesc(&res);
        if (!err) {
            AEGetDescData(&desc, &err, sizeof(err));
            AEDisposeDesc(&desc);
        } else if (err == errAEDescNotFound)
            err = noErr;
        return err;
    }

    To launch Terminal.app, use -[NSWorkspace launchApplication:],
    LSOpenApplication() or similar.

    If you need to do anything more complex, I'd recommend using a high-
    level bridge such as objc-appscript <http://appscript.sourceforge.net/objc-appscript.html> or Leopard's Scripting Bridge.

    HTH

    has
    --
    http://appscript.sourceforge.net
    http://rb-appscript.rubyforge.org
  • check out the open source OS X terminal application iTerm (
    http://iterm.sourceforge.net/)  It has a terminal framework that you can
    embed into your cocoa app.  When it is properly set-up you can
    programatically send terminal commands to the embedded iTerm just as if you
    were typing it in yourself.

    On Jan 16, 2008 6:37 AM, has <hengist.podd...> wrote:

    > Caroline wrote:
    >
    >> I want to run some App by using Terminal script.
    >
    >
    > If you're wanting to run shell scripts in Terminal (as opposed to just
    > executing them directly via NSTask), you need to send Terminal a 'do
    > script' event. Example:
    >
    > OSStatus DoTerminalScript(char *utf8Script) {
    > /*
    > * Run a shell script in Terminal.app.
    > * (Terminal.app must be running first.)
    > */
    > char *bundleID = "com.apple.terminal";
    > AppleEvent evt, res;
    > AEDesc desc;
    > OSStatus err;
    >
    > // Build event
    > err = AEBuildAppleEvent(kAECoreSuite, kAEDoScript,
    > typeApplicationBundleID,
    > bundleID, strlen(bundleID),
    > kAutoGenerateReturnID,
    > kAnyTransactionID,
    > &evt, NULL,
    > "'----':utf8(@)", strlen(utf8Script),
    > utf8Script);
    > if (err) return err;
    > // Send event and check for any Apple Event Manager errors
    > err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
    > AEDisposeDesc(&evt);
    > if (err) return err;
    > // Check for any application errors
    > err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &desc);
    > AEDisposeDesc(&res);
    > if (!err) {
    > AEGetDescData(&desc, &err, sizeof(err));
    > AEDisposeDesc(&desc);
    > } else if (err == errAEDescNotFound)
    > err = noErr;
    > return err;
    > }
    >
    > To launch Terminal.app, use -[NSWorkspace launchApplication:],
    > LSOpenApplication() or similar.
    >
    >
    > If you need to do anything more complex, I'd recommend using a high-
    > level bridge such as objc-appscript <
    > http://appscript.sourceforge.net/objc-appscript.html
    >> or Leopard's Scripting Bridge.
    >
    > HTH
    >
    > has
    > --
    > http://appscript.sourceforge.net
    > http://rb-appscript.rubyforge.org
    >

    --
    ----
    Bruce Johnson
    <bdjohnson01...>
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today