问题
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?
回答1:
<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>
回答2:
If your document types are common types, you could use UTI's
About UTI's
来源:https://stackoverflow.com/questions/7310309/how-to-define-a-uniform-type-identifier-in-a-plist-file