Storing Document Types Information in the Application's Property List

Applications use an information property list file, which is stored in the application’s bundle and named, by default, Info.plist, to specify various information that can be used at runtime. Document-based applications use this property list to specify the document types the application can edit or view. This information is used by the application and system entities such as the Finder and Launch Services (an API for launching applications in Mac OS X) as well.

For example, when the NSDocumentController object creates a new document or opens an existing document, it searches the property list for such items as the document class that handles a document type, the uniform type identifier (UTI) for the type, and whether the application can edit or only view the type. (Similarly, Launch Services may use information about the icon file for the type.) Supplying this information in a property list allows an application to support the Open and Save panels with little effort.

Document types information is associated with the CFBundleDocumentTypes key as an array of dictionaries, each of which contains the key-value pairs that define the document type. For a list of possible keys and their values, see Property List Key Reference.

Although you can create and edit information property lists directly in the Property List Editor application (located in /Developer/Utilities/Applications), Xcode also provides convenient options for viewing and editing document types information (and other property list information). Figure 1 shows the Properties pane of the Xcode inspector for the TextEdit target. In this view, you can select and edit any of the document types as well as add and delete entries. You can also edit the property list file in the plain text XML format shown in Listing 1. The property list file for the TextEdit application is named Info-TextEdit.plist (the name can be specified among the target’s build settings).

Note: The Properties pane is visible only for targets that create products with Info.plist files, and you cannot configure Info.plist entries for legacy targets, such as Jam-based Project Builder targets, in the target inspector. To edit the Info.plist entries for Jam-based targets in Xcode, select the target in the Groups & Files list and double-click the target to launch the target editor.

Figure 1  Document types information for TextEdit application in Xcode

Document Types information for TextEdit application in Xcode

TextEdit defines a subclass of NSDocument named Document to handle all of its document types, as shown in the Class column in Figure 1.

The top section of the Properties pane allows you to edit basic information about the product, such as the name of the associated executable, the identifier, type and creator, version information, and an icon to associate with the finished product. The name of the icon here must match the name of an icon file (extension .icns) that resides in the Resources folder of the product bundle.

The Principal Class and Main Nib File options are specific to Cocoa applications and bundles, and Automator actions. The Principal Class field corresponds to the information property list key NSPrincipalClass. The Main Nib File field specifies the nib file that’s automatically loaded when the application is launched. It corresponds to the information property list key NSMainNibFile.

The Document Types table allows you to specify which documents your finished product can handle. You can add and remove document types from this list using the plus and minus buttons. You should list the application's primary document type first because the document controller uses that type by default when the user requests a new document. Here is what’s in the Document Types table:

To edit a document type, click the type’s line in the Document Types list and double-click the individual fields in each column to add or change the document type information.

Listing 1 shows information for one of the document types from the information property list file for the TextEdit application (which is available in /Developer/Examples/TextEdit) in text-only XML format. The property list is considerably simplified by the use of the UTI for the document type, which is listed under the key LSItemContentTypes.

This listing omits eleven additional document types handled by the TextEdit application, which are shown in the Xcode inspector view in Figure 1.

Listing 1  Document types information for TextEdit application in XML format

<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeIconFile</key>
            <string>rtf.icns</string>
            <key>CFBundleTypeName</key>
            <string>NSRTFPboardType</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>public.rtf</string>
            </array>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>LSIsAppleDefaultForType</key>
            <true/>
            <key>NSDocumentClass</key>
            <string>Document</string>
        </dict>
...


© 2001, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-11-17)