开发者

How to define a Uniform Type Identifier in a plist file?

My application uses the following NSApplicationDelegate function.

- (void)application:(NSApplication*)sender openFiles:(NSArray*)filenames;

I want to use this function to enable the user to drag and drop image files onto the application icon in the dock.

How do I have to define certain file types in my plist file to restrict them to be images? I found out the structure has to look something like this.

// plist file contents taken from Preview.app
[...]
<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFile</key>
        <string>jpeg.icns</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>LSIsAppleDefaultForType</key>
        <true/>
        <key>LSItemContentTypes</key>
        <array>
            <string>public.jpeg</string>
        </array>
        <key>NSDocumentClass</key>
        <string>PVDocument</string>
    </dict>
</array>

I added it to the plist file but it does not work. A popup window shows the following error message.

The document "test.jpg" could not be opened. MyApp cannot open files in the "JPEG image" format.

Further, I read in the documentation that there is public.image which would be what I want to define.

Meanwhile, I found out that the plist file only contains the key CFBundleDocumentTypes if I create a Cocoa Application with the option "Create document-based application.". Can you please开发者_运维知识库 clarify what dependencies exist for the option?


    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>png</string>
                <string>jpg</string>
                            ... add as many types as you need
            </array>
                    ... other keys
        </dict>
    </array>

Update: The CFBundleDocumentTypes key is deprecated in Mac OS X v10.5. The new key LSItemContentTypes should be used instead. The items are UTI strings:

<key>LSItemContentTypes</key>
<array>
    <string>public.png</string>
</array>


If your document types are common types, you could use UTI's About UTI's

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜