使用'launchedTaskWithLaunchPath'Cocoa/objective-c API启动应用 [英] Launching App using 'launchedTaskWithLaunchPath' Cocoa/objective-c API

查看:49
本文介绍了使用'launchedTaskWithLaunchPath'Cocoa/objective-c API启动应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从某个应用程序启动"TextMate",并且使用了以下代码.

I need to launch 'TextMate' from an App, and I used the following code.

[NSTask launchedTaskWithLaunchPath:@"/Applications/TextMate.app" arguments:[NSArray arrayWithObjects:@"hello.txt", nil]];

但是,我得到了以下错误返回.

But, I got the following error return.

*** NSTask: Task create for path '/Applications/TextMate.app' failed: 22, "Invalid argument".  Terminating temporary process.

  • 我的代码有什么问题?我只是尝试运行"TextMate hello.txt".
  • 我可以使其运行如下.

    [NSTask launchedTaskWithLaunchPath:@"/Applications/TextMate.app/Contents/MacOS/TextMate"     arguments:[NSArray arrayWithObjects:@"hello.txt", nil]];
    

    然后我问了另一个问题查看有多少其他可用方式.

    And I asked another question to see how many other ways available.

    推荐答案

    在这种情况下,无效参数是应用程序的名称.

    In this case, the invalid parameter is the application's name.

    如果查看NSTask的文档,您会发现所使用的方法基本上是低级exec()系统调用的包装器.这意味着您需要提供实际的可执行文件或二进制文件的名称,以便它能够创建进程.在您的情况下,您要给它一个目录(使用终端确认/Applications中的大多数应用程序捆绑包都是目录).这就是为什么要呕吐的原因.

    If you check the documentation for NSTask you'll see that the method you're using is basically a wrapper for the low-level exec() system call. This means you need to provide the name of an actual executable or binary file for it to be able to create the process. In your case, you're giving it a directory (use a terminal to confirm that most app bundles in /Applications are directories). That's why it barfs.

    您可以在TextMate的bundle目录中查找实际的可执行文件(应位于/Applications/TextMate.app/Contents/MacOS中的某个位置).然后,您可以修改代码以调用实际的可执行文件.

    You could look inside TextMate's bundle directory to find the actual executable (should be somewhere in /Applications/TextMate.app/Contents/MacOS). You could then modify your code to call the actual executable.

    但是,似乎正确的可可方式是使用NSWorkspace,您可以查看其openFile:withApplication:方法,该方法似乎可以满足您的需要,在这种情况下,您可以指定将应用程序捆绑目录作为参数,就像您最初使用的方式一样.

    However, it would seem that the correct, Cocoa-ish way to do it is by using NSWorkspace, you might look into its openFile:withApplication: method, which seems to do what you need, and in this case you DO specify the application bundle directory as a parameter, the way you were originally doing it.

    官方文档为顺便说一句,我不能完全相信它.请参阅此StackOverflow答案了解有关此主题的更多信息.

    By the way, I can't fully take credit for it; see this StackOverflow answer to learn more about this topic.

    这篇关于使用'launchedTaskWithLaunchPath'Cocoa/objective-c API启动应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆