Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred path to applications on OSX?

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.

like image 329
Josh Matthews Avatar asked Jan 24 '23 02:01

Josh Matthews


2 Answers

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"];
like image 61
Dave Verwer Avatar answered Jan 26 '23 15:01

Dave Verwer


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.

like image 28
Chris Hanson Avatar answered Jan 26 '23 15:01

Chris Hanson