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.
-
this question belongs on our sister site, Stack Overflow. it will be migrated there shortly. – quack quixote Jun 03 '10 at 21:41
4 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

- 8,741
- 9
- 52
- 83

- 3,096
- 3
- 20
- 25
Look here for a description of the CFBundleDocumentTypes Info.plist key:
-K

- 11,484
- 2
- 36
- 42
-
2Bad link. Presumably this is where to go now: https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-101685 – Tom Bogle Feb 07 '18 at 18:51
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Donald Duck Jun 19 '18 at 18:02
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.

- 8,409
- 22
- 75
- 99
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.

- 190
- 10
-
This post is about a Python app (bundling it up as a Mac app). It might include the needed info, but it doesn't seem to directly scratch the itch. – Tom Bogle Feb 07 '18 at 18:45
-
The defaults command mentioned in the accepted answer above works fine, but you would need to call this after the installation of your app, while linking your app to a file suffix like described in my article automatically manages this whenever you copy the application to a Mac. But yes, my article describes a larger Python build process, too. On the other hand, the process will be the same for any application you build for macOS. – André Aulich Feb 09 '18 at 10:39