Objective c 使用目标文件启动应用程序

Objective c 使用目标文件启动应用程序,objective-c,macos,launching-application,Objective C,Macos,Launching Application,使用NSWorkSpace启动应用程序,如果使用应用程序文件,一切都可以,但如果希望使用untitled.rtf这样的目标文件启动应用程序,该怎么办 [[NSWorkspace sharedWorkspace] launchApplication:selection] 使用-[NSWorkspace openFile:WithApplication:][/code>如下所示: [[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/untitled

使用NSWorkSpace启动应用程序,如果使用应用程序文件,一切都可以,但如果希望使用untitled.rtf这样的目标文件启动应用程序,该怎么办

[[NSWorkspace sharedWorkspace] launchApplication:selection]

使用
-[NSWorkspace openFile:WithApplication:][/code>如下所示:

[[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/untitled.rtf"
    withApplication:@"TextEdit"];
[[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/untitled.rtf"];
或者,如果只想使用该文件的默认应用程序打开一个文件,请使用
-[NSWorkspace openFile::
如下所示:

[[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/untitled.rtf"
    withApplication:@"TextEdit"];
[[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/untitled.rtf"];

请确保检查详细信息和其他相关方法。

这将仅使用目标文件启动适当的应用程序

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/open"];
[task setArguments: @[@"/somewhere/untitled.rtf];
[task launch];

好的,但关键是在不知道应用程序名称的情况下打开文件。我有一个解决方案可以做到这一点,当我需要做很多工作时,我会回答这个问题,而NSWorkspace已经有了这样做的方法。有关详细信息,请参阅我的答案。