本文介绍了从Cocoa应用程序打开到指定文件夹的终端窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到这个线程如何在Cocoa应用程序中执行终端命令。但我想将Terminal.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.
我知道以下不工作:
[[NSWorkspace sharedWorkspace] openFile:folderPath withApplication:@"Terminal"];
终端尝试将文件夹实际打开为文件。
Terminal tries to actually open the folder as a file.
这是我必须使用AppleScript吗?
Is this something I have to use AppleScript for?
任何想法?
推荐答案
您可以使用Cocoa中的AppleScript,如下所示:
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脚本来自cobbal。感谢mate!
AppleScript script was taken from cobbal. Thanks mate!
这篇关于从Cocoa应用程序打开到指定文件夹的终端窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!