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

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

问题描述

我需要从应用程序启动TextMate,我使用以下代码。

  [NSTask launchedTaskWithLaunchPath:@/ Applications / TextMate.apparguments:[NSArray arrayWithObjects:@hello.txt零]]; 

但是,我收到以下错误返回。

  *** NSTask:任务创建路径'/Applications/TextMate.app'失败:22,参数无效。终止临时进程。 




  • 我的代码有什么问题?我刚试图运行TextMate hello.txt。



ADDED



  [NSTask launchTaskWithLaunchPath:@/ Applications / TextMate.app / Contents / MacOS / TextMate参数:[NSArray arrayWithObjects:@hello.txt,nil]]; 

我问另一个问题,以查看其他可用的方法。

解决方案

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



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



你可以看看TextMate的bundle目录中找到实际的可执行文件(应该在/Applications/TextMate.app/Contents/MacOS中的某个地方)。然后,您可以修改您的代码以调用实际的可执行文件。



但是,似乎正确的Cocoa-ish方法是使用NSWorkspace,看看它的openFile:withApplication:方法,它似乎做你所需要的,在这种情况下,你指定应用程序包目录作为参数,你最初做的方式。



官方文档这里



顺便说一句,它;请参见此StackOverflow答案以了解有关此主题的详情。


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.

  • What's wrong with my code? I just tried to run "TextMate hello.txt".

ADDED

I could make it run as follows.

[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.

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.

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.

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.

Official documentation is here.

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

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

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