启动 NSTask 并将其置于最前面
我正在尝试使用 NSTask 启动另一个应用程序
NSArray* argArray = [NSArray arrayWithObjects:fileName, nil];
NSTask* task = [NSTask launchedTaskWithLaunchPath:appName arguments:argArray];
,但主 gui 窗口不会出现在前面。
当使用不同的文件名重复调用时,即使应用程序只有 1 个实例正在运行
任何指针,新文件也会加载到应用程序中吗?我确实尝试了 SetFrontProcess
但即使在引入延迟后似乎也没有效果
我确实查看了 NSRunningApplication 但它似乎在 10.5 上不可用,而我需要针对 10.5 和 10.6 的解决方案
I'm trying to launch another application using NSTask
NSArray* argArray = [NSArray arrayWithObjects:fileName, nil];
NSTask* task = [NSTask launchedTaskWithLaunchPath:appName arguments:argArray];
while this works the main gui window doesn't come to front.
when repeatedly calling with different fileName the new file does get loaded in the app even though only 1 instance of the app is running
any poiners? I did try SetFrontProcess
but seems to have no effect even after introducing a delay
I did look into NSRunningApplication but it seems it is not available on 10.5 whereas I need a solution for both 10.5 and 10.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用
NSTask
启动应用程序。使用 NSWorkspace ,它有多种方法(例如-launchApplication:
)来启动和激活应用程序。Don't use
NSTask
to launch applications. Use NSWorkspace, which has several methods (e.g.-launchApplication:
) to launch and activate an application.我从我的
MDFoundationAdditions
和MDAppKitAdditions
类别中获取了这些内容。此解决方案应适用于 Mac OS X 10.4.x 及更高版本(即引入 LSOpenApplication() 时):
MDAppKitAdditions.h:
MDAppKitAdditions.m:
MDFoundationAdditions.h:
MDFoundationAdditions.h:
I grabbed these from my
MDFoundationAdditions
andMDAppKitAdditions
categories.This solution should work for Mac OS X 10.4.x and later (which is when LSOpenApplication() was introduced):
MDAppKitAdditions.h:
MDAppKitAdditions.m:
MDFoundationAdditions.h:
MDFoundationAdditions.h:
为了扩展 indragie 的答案,如果您想要使用文件参数启动一个新实例,请执行类似的操作(未经测试):
在 10.5 上,您可以尝试(未经测试):
To expand on indragie's answer, if you want a new instance launched with a file argument, do something like (untested):
On 10.5, you might try (not tested):
如果你要启动的任务是一个合适的应用程序,你可以使用 NSWorkspace 的
If the task you want to launch is a proper application, you can use NSWorkspace's