views:

64

answers:

2

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.

+3  A: 

Hi,

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.

Roadmaster
+2  A: 

You're trying to launch a directory, not a binary.

jer