问题
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?
回答1:
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!
回答2:
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"
回答3:
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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/1446814/open-a-terminal-window-to-a-specified-folder-from-a-cocoa-app