简体   繁体   English

从Cocoa应用程序打开指向指定文件夹的终端窗口

[英]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. 我已经看过这个关于如何从Cocoa应用程序中执行终端命令的线程。 But I want to actually launch Terminal.app to a specified directory. 但我想实际将Terminal.app启动到指定的目录。

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? 这是我必须使用AppleScript的东西吗?

Any ideas? 有任何想法吗?

You could use AppleScript from Cocoa like this: 您可以使用Cocoa中的AppleScript,如下所示:

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. AppleScript脚本取自cobbal。 Thanks mate! 谢了哥们!

不确定是否有办法在普通可可中做到这一点,但在AppleScript中它是相当微不足道的

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

I don't really know AppleScript, but I bet you could use it for this. 我真的不懂AppleScript,但我打赌你可以用它来做这件事。

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. 如果终端目录每次都相同,则可以在其中创建一个带有cd命令的可执行.sh文件,并将其作为openFile参数。

The existing answers suggesting using the cd command are great. 建议使用cd命令的现有答案很棒。 Additionally, I recommend checking out the source to the app cdto for a great example. 另外,我建议查看应用程序cdto的源代码,以获得一个很好的例子。 Cdto is a fast mini application that opens a Terminal.app window cd'd to the front most finder window. Cdto是一个快速的迷你应用程序,它打开一个Terminal.app窗口cd'd到最前面的查找器窗口。 This app is designed (including it's icon) to placed in the finder window's toolbar. 此应用程序被设计(包括它的图标)放置在取景器窗口的工具栏中。

You can use the (now obsolete) AppleEvent Carbon API to send an "Do Script" event to Terminal.app : 您可以使用(现已过时)AppleEvent Carbon API向Terminal.app发送“Do Script”事件:

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. 请注意,如果没有运行,则必须使用-[NSWorkspace launchApplication:]启动Terminal.app。 If wanted, it can be put in foreground with - [NSApplication activateWithOptions:] 如果需要,它可以放在前台- [NSApplication activateWithOptions:]

As suggested by a comment, this can be easily ported to the more modern Scripting Bridge API . 正如评论所建议的那样,这可以很容易地移植到更现代的Scripting Bridge API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM