Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MacOSX - File extension associate with application - Programmatically

I'm trying to get my program to automatically associate certain file extensions to be opened by it but I'm not sure how to do that in MacOSX. I'm not asking how to associate a program with a file extension in the GUI, I want to be able to program it into my program.

like image 956
Nick Avatar asked Jun 03 '10 21:06

Nick


People also ask

How do I associate an application with a file extension Mac?

Alternatively, right-click on the file and select Get Info. Click the down arrow for Open with, click the drop-down menu to choose the default app, and then click Change All to always associate that file type with your chosen app.

How do I associate a file extension to a program?

Under the Default Programs window, click on the “Associate a file type or protocol with a program” link. In the Set Associations window, scroll down the list until you see the file extension that you want to change the default program for. Select your desired file extension and click on Change Program.


2 Answers

To register a new file extension with an application use the following defaults command.
Replace PUT_FILE_EXTENSION_HERE_WITHOUT_PERIOD with the file extension i.e. txt.
Replace org.category.program with the com/org name of your program i.e. com.apple.itunes.

$ defaults write com.apple.LaunchServices LSHandlers -array-add \
"<dict><key>LSHandlerContentTag</key>
<string>PUT_FILE_EXTENSION_HERE_WITHOUT_PERIOD</string><key>LSHandlerContentTagClass</key>
<string>public.filename-extension</string><key>LSHandlerRoleAll</key>
<string>org.category.program</string></dict>"


Once you have added the file extension to the launch services, you must restart the launch services deamon so it will re-read the configuration file.

You can either run the command below to restart launch services, or simply restart your computer. Loging in/loging out also might do it but I haven't tried.

$ /System/Library/Frameworks/CoreServices.framework/Versions/A/Framework/LaunchServices.framework/Versions/A/Support/lsregister -kill -domain local -domain system -domain user
like image 182
Nick Avatar answered Sep 27 '22 19:09

Nick


Look here for a description of the CFBundleDocumentTypes Info.plist key:

http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-101685

-K

like image 29
Ken Aspeslagh Avatar answered Sep 27 '22 20:09

Ken Aspeslagh