Apple - How can I make OS X recognize new file extensions?

UTIs and Launch Services

Where is the list of “known” file types stored?

The official term for "file types" is Uniform Type Identifiers (UTIs), and the database of UTIs is maintained by Launch Services.

Using the Terminal, you can view the contents of the Launch Services database using the lsregister tool, which is stored at:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister

Instead of specifying that long path every time you want to run lsregister, you can temporarily add its directory to your PATH:

PATH=/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support:"$PATH"

and then you can run lsregister by simply entering:

lsregister

To view the contents of the Launch Services database:

lsregister -dump

Declaring New UTIs

Is there a way to make OS X recognize new file types?

This Apple article, "Declaring New Uniform Type Identifiers", provides useful information on how to do this. Here's the complete process:

1. Create a dummy application

Create a dummy application to register with Launch Services:

  • Open AppleScript Editor (located in OS X's Utilities folder).
  • Save a new empty script somewhere (eg: on your Desktop), setting its File Format to Application.

2. Open its Info.plist file

  • Locate your new dummy application in the Finder.
  • Right-click on it, and select Show Package Contents from the popup menu.
  • Open its Contents folder.
  • Open the Info.plist file in a text editor (eg: TextEdit).

3. Add your new UTI

There's a choice of two keys:

  • UTExportedTypeDeclarations – for your own custom filename extensions.
  • UTImportedTypeDeclarations – for filename extensions which already exist but aren't recognised by OS X.

Choose the key which is most appropriate for your needs.

Then in the Info.plist file, before the final two lines (</dict></plist>), add this code:

<key>KEY</key>
<array>
    <dict>
        <key>UTTypeIdentifier</key>
        <string>IDENTIFIER</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>EXTENSION</string>
            </array>
        </dict>
    </dict>
</array>

making the following changes:

  • Replace KEY with your chosen key (either UTExportedTypeDeclarations or UTImportedTypeDeclarations).
  • Replace IDENTIFIER with a suitable reverse-DNS identifier:
    • Exported – Something like com.mycompany.mytype
    • Imported – For id Software's Doom WAD format, a suitable identifier would be com.idsoftware.wad
  • Replace EXTENSION with the filename extension (without the leading dot), eg: wad.

This is the minimum code necessary for Launch Services to accept your new UTI. You can also add the following optional properties:

  • UTTypeConformsTo – See the Appendix below.
  • UTTypeDescription – A user-visible description, which will be displayed in the Finder.
  • UTTypeIconFile – If you add an Apple Icon Image file to the Contents/Resources folder in your dummy application, and add its filename in this property, then files which have your new filename extension will use this icon.
  • UTTypeReferenceURL – The URL of a reference document describing this type.

Here's a complete example showing all the optional properties:

<key>UTImportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeIdentifier</key>
        <string>com.idsoftware.wad</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>wad</string>
            </array>
        </dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>Doom WAD file</string>
        <key>UTTypeIconFile</key>
        <string>DoomWAD.icns</string>
        <key>UTTypeReferenceURL</key>
        <string>http://en.wikipedia.org/wiki/Doom_WAD</string>
    </dict>
</array>

4. Register your new UTI with Launch Services

Run this command in the Terminal:

lsregister <PATH_TO_APP>

replacing <PATH_TO_APP> with the path to your dummy application, eg:

lsregister ~/Desktop/MyDummyApp.app

If you now create a file with your new filename extension, and then select it in the Finder and hit Enter, the Finder should automatically select only the filename part, not the extension.

If you view the contents of the Launch Services database:

lsregister -dump

you should find your new UTI listed:

type    id:            50364
        bindableKey:   12608
        generation:    1
        uti:           com.idsoftware.wad
        description:   Doom WAD file
        flags:         imported  active  apple-internal  untrusted
        icon:          DoomWAD.icns
        conforms to:   public.data
        tags:          .wad

If you later want to unregister your new UTI, pass the -u option to lsregister:

lsregister -u ~/Desktop/MyDummyApp.app

Appendix: UTI hierarchy and conformance

OS X declares a hierarchy of UTIs which are listed here: System-Declared Uniform Type Identifiers.

Here are a few UTIs in the hierarchy:

  • public.content
    • public.text
      • public.rtf
      • public.html
      • public.xml
      • public.plain-text
      • public.source-code
        • public.c-source
        • public.c-header
        • com.sun.java-source
    • public.image
      • public.jpeg
      • public.tiff
      • public.png
    • public.audiovisual-content
      • public.movie
      • public.audio

Each UTI "conforms to" its parent UTI:

For example, the UTI public.html, which defines HTML text, conforms to the base text identifier, public.text. In this case, conformance lets applications that can open general text files identify HTML files as ones it can open as well. (ref)

When creating a new UTI, it's a good idea to set the UTTypeConformsTo property to one of the existing UTIs.

For example, if your new UTI is a type of source code, then you should set the UTTypeConformsTo property to public.source-code, so that it can be opened by any applications which can open public.source-code or public.plain-text or public.text files.


This SuperUser post worked for me:

You need to browse to the application that you’d like to use to open these files, and edit its bundle. Right-click the application to handle these file extensions and select Show Package Contents. Navigate to Contents, and edit Info.plist. You might need Property List Editor, which is part of Apple’s developer tools. If you’re lucky, it’s in XML format. Edit this file’s CFBundleDocumentTypes and add an entry for the extension that you want.

I used TextEdit as the default application to open .bar files. The only thing I did differently was change the XML that went in TextEdit’s Info.plist to:

    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>bar</string>
        </array>
        <key>CFBundleTypeIconFile</key>
        <string>BarDocument</string>
        <key>CFBundleTypeName</key>
        <string>Bar Document</string>
        <key>CFBundleTypeRole</key>
        <string>Document</string>
    </dict>

…and then I used LSRefresh.app to refresh TextEdit in the launch services database. Then it worked as you suggested, although it will probably break the code signature of Textedit (and any other signed application) on Lion.

Tags:

Macos

Uti

Finder