19

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.

2
  • this question belongs on our sister site, Stack Overflow. it will be migrated there shortly. Commented Jun 3, 2010 at 21:41
  • As of 2025 (macOS 15.2 Sequoia), none of the given answers still apply. It seems to be a complex process to change/query a file type's associated application (as bizarre as that seems), unless going thru the GUI by getting file-info and changing the associated application via "open with". Best I can come up with (as a perpetual terminal user) is to just specify the application to run, eg via open -a TextEdit.app foo.txt vs open -a MacVim.app foo.txt (I'm not adding this as an answer, as this doesn't even remotely address the question). Looking forward to being corrected. Commented Jan 22 at 1:56

4 Answers 4

14

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
Sign up to request clarification or add additional context in comments.

2 Comments

In sonoma (14.5), defaults says that Domain com.apple.LaunchServices doesn't exist. But it seems to be accessible with a filepath: defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist
definitely not still valid as of 2025, and could potentially do more damage than good. (Afaik, there's no good solution today?) Fwiw, the defaults command includes this caveat for -array-add option: "array cannot be deleted or rearranged. New items cannot be added anywhere but the end of an array. Arrays that require something more complex than items appended to the end should be added from scratch with -array"
7

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

2 Comments

Bad link. Presumably this is where to go now: developer.apple.com/library/content/documentation/General/…
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.
2

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.

Comments

0

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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.