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 m开发者_运维技巧y program.
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
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
If you right click on your .app file and choose "Show package contents", there will be a folder called Contents
, and in that folder, there will be a file called info.plist
and a folder called Resources
(if any of these don't exist, create them). If you want to associate the file extension .myfileextension
with your program and you want files with that extension to have the icon contained in a file called icon.icns
, copy the file icon.icns
into the Resources
folder and add the following code to the info.plist
file right before the </dict>
tag:
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeIconFile</key>
<string>icon.icns</string> <!-- change this -->
<key>CFBundleTypeExtensions</key>
<array>
<string>myfileextension</string> <!-- change this -->
</array>
<key>CFBundleTypeName</key>
<string>File extension description</string> <!-- change this -->
<key>LSHandlerRank</key>
<string>Owner</string>
</dict>
</array>
The lines marked <!-- change this -->
in the above code should be changed according to what properties you want the extension to have. icon.icns
should be changed to whatever name the icon you put in the Resources
folder and you want the file extension to have is called, myfileextension
should be changed to the file extension that you want to associate with your program (without the dot), and File extension description
should be changed to the description you want the file extension to have (for example for .doc files it would be "Microsoft Word document").
Also, you can check here to see what the other values mean and if you need to change them. There are also other values listed there that you can add if you need them.
I described the whole thing in detail at https://moosystems.com/articles/8-double-click-on-files-in-finder-to-open-them-in-your-python-and-tk-application.html.
This involves bundling your application via py2app, adding certain key to your Info.plist file and installing event handlers in your app.
精彩评论