27

In Finder.app, you can hit Enter to edit the name of the currently selected file or folder.

OS X will automatically select only the file name part, not the extension:

This is very useful, because that way you can just start typing the new name, without worrying about accidentally changing the file extension.

However, this only seems to work for “known” file types. If you try it with an unknown extension, the whole file name (including the extension) gets selected:

I tried mapping an application that can open all .bar files via +I → “Change All”, but I still get the behavior described above.

Where is the list of “known” file types stored? Is there a way to make OS X recognize new file types?

3 Answers 3

8

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.

6
  • 1
    CFBundleTypeExtensions is a legacy key which became deprecated in OS X 10.5. The preferred method since 2007 is to declare new UTIs, as mentioned here and described in my answer. Commented Mar 4, 2015 at 22:48
  • 11
    I'm sorry, I have to downvote this because this is bad advice. Not only is it unwise to tamper with an app bundle (running the risk of bizarre side effects or worse), but it can also completely wreck the copy of the app. You note that this process does invalidate the code signature but fail to point out that on macOS versions that support sandboxing (10.7+), a sandboxed app with an invalid signature cannot be launched or used in any way.
    – Bob
    Commented Jan 25, 2017 at 17:50
  • 1
    @binarybob, What I don't understand is why don't you simply rightclick the file xxx.bar and select open with?
    – Pacerier
    Commented Dec 10, 2017 at 22:10
  • 2
    @Pacerier App sandboxing is a security feature of macOS that completely isolates apps from the system and each other, with a very short list of "entitlements" that details specifically what apps are allowed to do. That list of entitlements is embedded in the code signature and validated every time the app is run, ensuring that apps' capabilities cannot be tampered with, either by accident or by an attack. See this document for a technical explanation.
    – Bob
    Commented Dec 11, 2017 at 1:52
  • 1
    Let me reinforce @Bob's comment. Since at least macOS Catalina, and probably earlier, tampering with any file inside a package (= macOS app) will immediately and irreversibly prevent that app from launching. The only way to restore it is to install it from scratch (overwriting whatever changes you've made). See the reply by @TachyonVortex for a more convoluted, but safer alternative to this issue. Commented Mar 31, 2021 at 16:54
45

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.

3
  • Is there a way to specify the editor that is asociated with the file extension?
    – BadAtLaTeX
    Commented Mar 19, 2020 at 0:25
  • To the OP: this ought to be flagged as the accepted suggestion. See @Bob's comment. Commented Mar 31, 2021 at 17:02
  • To @gr4nt3d: see apple.stackexchange.com/a/323743/27729 below for what you wish to do. Commented Mar 31, 2021 at 17:02
2

Follow these steps:

  1. Select the file with unrecognized extension (say, .txt).

  2. Right click and select Get Info or use the keyboard shortcut Commandi to display the information window.

  3. Click on the disclosure indicator besides Open with: to open detailed view. Select the desired app from the drop down. This will associate the document with the chosen application (For e.g. TextEdit).

  4. Click on the Change All... button. It will first show a dialogue box asking to confirm your intent and will link all .txt documents to open with TextEdit, and leave the extension un-selected when you hit return on the document's name.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .