views:

608

answers:

5

I want to be able to run a text editor from my app, as given by the user in the TEXT_EDITOR environment variable. Now, assuming there is nothing in that variable, I want to default to the TextEdit program that ships with OSX. Is it kosher to hardcode /Applications/TextEdit.app/Contents/MacOS/TextEdit into my app, or is there a better way to call the program?

Edit: For the record, I am limited to running a specific application path, in C. I'm not opening a path to a text file.

Edit 2: Seriously people, I'm not opening a file here. I'm asking about an application path for a reason.

A: 

I believe that Mac OS X provides a default application mechanism, so that .txt will open in TextEdit.app or Emacs or GVim or whatever the user has specified. I couldn't find anything online however.

JeeBee
A: 

You could run following command from your application:

open <full path to text file>

This will open the text file in the default text editor. You can open any file type using open command.

Vivek
Don't do this - you don't need to fork/exec just to open a file in another application.
Chris Hanson
+2  A: 

I believe hardcoding "Applications" will not work if the user's language setting is not English. For example in Norsk the "Applications" folder is named "Programmer".

The Apple document on internationalization is here. Starting on page 45 is a section on handling localized path names.

DGentry
Actually, I found this in the document:"Localized path names are for display only and should never be used to access the file system."
Josh Matthews
+3  A: 

Mac OS X has a mechanism called "uniform type identifiers" that it uses to track associations between data types and applications that can handle them. The subsystem that manages this is Launch Services. You can do one of two things:

  • If you have a file with a reasonably well-known path extension, e.g. .txt, you can just ask NSWorkspace to open the file in the appropriate application.

  • If you don't have a well-known path extension, but you know the type of data, you can ask Launch Services to look up the default application for that type, and then ask NSWorkspace to open the file in that specific application.

If you do it this way you'll get the same behavior as the Finder, and you won't have to fork()/exec() or use system() just to open a file.

Chris Hanson
+2  A: 

In your second edit it makes it sound like you just want to get the path to TextEdit, this can be done easily by using NSWorkspace method absolutePathForAppBundleWithIdentifier:

NSString *path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.apple.TextEdit"];
Dave Verwer