Preferred path to applications on OSX? - macos

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.

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"];

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.

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.

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.

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.

Related

Write data to file and set custom icon

I'm currently developing a macOS cocoa application. Within one of the windows I'm gathering values from multiple arrays and building an xml file as a key/value backup. After saving the file I'm wanting to set a custom icon for the particular xml file. I've searched throughout stackoverflow and the rest of the web and haven't been able to find any solutions. Throughout the different functions within my application I'm able to pull a previously set icon from a file but haven't been able to set one for a newly created file. Any assistance would be greatly appreciated.
In order to change the XML file icon you should use that:
[[NSWorkspace sharedWorkspace] setIcon:iconImage forFile:path options:0];
Where iconImage is a NSImage of the icon and path is the path of the xml file. Note that the change may not be visible until you rename the file or restart the computer. A workaround is renaming the xml file and then renaming it with the old name back again.
Also, note that macOS icons are different of Windows and Linux icons, so that icon may only be visible in macOS systems (in some cases, only in your machine).

MacOS Get default application for file type

I am working on a Mac app. I ultimately want to use default app icons within my app. From the Info.plist and the Resource folder of an app I can get the .icns file and convert that to the image format I need. But I need to know the default application associated with the particular file extension, if any.
So how to get the default application that the system currently associates with a given file extension?
Don't go digging in other apps' bundles. It's always best to work at the level of abstraction that suits the question you want to ask. If you want to get the icon that the Finder (or a Mail attachment, etc) would display for a file of a particular type, use the NSWorkspace iconForFileType: method.
I think what you're looking for is part of the OSX Launch Services: LSCopyDefaultApplicationURLForContentType API. This returns the info on apps that can open specific Uniform Type Identifiers. There's also a similar API called LSCopyDefaultApplicationURLForURL to check which app opens a specific known file.

Open With Application: How to detect filename in go

I have created a go application on my mac that reads/writes from files *.myext. The executable was packed into an a Bundle called "MyApp".
I can start MyApp and then read/write *.myext files, that works.
My question is: how to detect the filename if I am opening e.g. test.myext by Open With > MyApp (usually right mouse button)?
I have tried to read the file name from os.Args, but the file name is not in there.
Is there a way?
Thank you for your help!
Leo
Looks like a program started via "Open With" does not receive the name of the file it has been invoked on but rather has to obtain it using "Apple events" as described in the accepted answer here.
I am thereby afraid currently the only way to solve the problem would be to use cgo; may be along these lines.

Redirect default program to another program when a file opens in Windows OS

This is only under windows env.
As I know windows os identifies associated application of a particular file by file extension.
Like wise each file (binary) starting with corresponding symbols ("starting symbols"). For an example .JPG starts with ÿØÿà. Let say I open this .JPG file in a Hex editor or a Text editor and then I change that starting symbols into another file type. for an example I can change ÿØÿà to .Eߣ (.mkv). So when I double click on the .JPG the Windows Photo Viewer says there are some errors or similar message. So I need to get some information about the application that tries to open that kind of a file. If I can, I need to open that file using the application that associated with "starting symbols".
Briefly when I open .JPG I need to open a default video player .mkv files. But It may not work for this example. Because I changed only the "starting symbols" of my .JPG.
Please give me any idea to do this.
Thanks!
When you encrypt the file, give it a new extension. e.g. Picture.jpg becomes Picture.encrypted-jpg. You then register as the handler for encrypted-jpg, decrypt the file, then launch the normal jpg handler.
When the shell is asked to perform a verb on a file, the shell does not use the contents of the file to determine which app to pass it to. The file extension is what determines how the file will be treated.
You wish to use the contents of the file to influence which app processes a shell verb. In order to do so you would need to create a launcher app that reads the file header and then decides which app to pass the file on to. You would assign your launcher app as the handler app for all file extensions that you were interested in.
Although you could do this, it would be much easier just to set the file extension appropriately.
The proper way to do this sort of thing is to replace the files with reparse points.
The downside is that this involves writing a file system filter driver, i.e., an operating system extension, which is a whole level of trouble above and beyond ordinary application programming. (Since Windows already does file encryption, I doubt it would be worth the effort.)

How do I make certain files accessible in Mac OS X?

I have a db file which I want to open using a firefox plugin.
However, I can't open the file.
I've also got other files like this with other file types.
I'm sure I've had this problem before, any ideas ?
I've tried...
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder
But that didn't work.
Mac OS X displays all of the files in a particular folder on your disk in its Open/Save common dialogs, but it grays out the ones that are in a format that can't be opened by whatever program's Open/Save dialog you're browsing from.
The title of the dialog in the screenshot above gives away your problem. It says "Set Default Directory", which means it's only looking for a folder. The file "bc.db" is just that: a file. Whatever program you're trying to open it from isn't willing to accept it because a file is not interchangeable with a folder/directory. That explains why none of your other file types work, either. You need to choose a folder from that dialog to set as the default.
It's also worth nothing that the .DB extension means that file is a database file of some sort. It's very likely that it's in a proprietary format that can only be read by one particular application. Thus, even if you try to open it from the Finder, you're not likely to get very far. You need to figure out what program originally generated that file and try to open/use it there.

Resources