Corey Floyd
Corey Floyd

Reputation: 25969

Open a terminal window to a specified folder from a Cocoa app

I have seen this thread on how to execute terminal commands from within a Cocoa app. But I want to actually launch Terminal.app to a specified directory.

I know that the following does not work:

[[NSWorkspace sharedWorkspace] openFile:folderPath withApplication:@"Terminal"];

Terminal tries to actually open the folder as a file.

Is this something I have to use AppleScript for?

Any ideas?

Upvotes: 10

Views: 4761

Answers (5)

Félix Faisant
Félix Faisant

Reputation: 301

You can use the (now obsolete) AppleEvent Carbon API to send an "Do Script" event to Terminal.app :

OSStatus doTerminalScript (NSString* script) {
    AppleEvent evt;
    OSStatus err;
        // Build event
    err = AEBuildAppleEvent(kAECoreSuite, kAEDoScript, 
                            typeApplicationBundleID, "com.apple.terminal", 18L,
                            kAutoGenerateReturnID, kAnyTransactionID, &evt, NULL,
                            "'----':utf8(@)", strlen([script UTF8String]), [script UTF8String]);
    if (err) return err;
    AppleEvent res;
        // Send event
    err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
    AEDisposeDesc(&evt);
    if (err) return err;
        // Check for any errors from Terminal.app
    AEDesc desc;
    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;
}

Taken form here. Note that Terminal.app must be launched with -[NSWorkspace launchApplication:] if not running. If wanted, it can be put in foreground with - [NSApplication activateWithOptions:]

As suggested by a comment, this can be easily ported to the more modern Scripting Bridge API.

Upvotes: 0

Joey Hagedorn
Joey Hagedorn

Reputation: 2408

The existing answers suggesting using the cd command are great. Additionally, I recommend checking out the source to the app cdto for a great example. Cdto is a fast mini application that opens a Terminal.app window cd'd to the front most finder window. This app is designed (including it's icon) to placed in the finder window's toolbar.

Upvotes: 0

Woofy
Woofy

Reputation: 3751

You could use AppleScript from Cocoa like this:

NSString *s = [NSString stringWithFormat:
     @"tell application \"Terminal\" to do script \"cd %@\"", folderPath];

NSAppleScript *as = [[NSAppleScript alloc] initWithSource: s];
[as executeAndReturnError:nil];

AppleScript script was taken from cobbal. Thanks mate!

Upvotes: 14

phoebus
phoebus

Reputation: 14931

I don't really know AppleScript, but I bet you could use it for this.

If the terminal directory is the same each time, you could just make an executeable .sh file with a cd command in it and make that the openFile argument.

Upvotes: -1

cobbal
cobbal

Reputation: 70733

Not sure if there's a way to do it in plain cocoa, but in applescript it's fairly trivial

tell application "Terminal" to do script "cd ~/Desktop"

Upvotes: 2

Related Questions