Creating an application that can open files of a given format
Asked Answered
E

3

5

I've got an app, written in Obj-C. The info.plist has a list of file types that the app can open. I'm pretty sure that this is working because when I try to drag a file of an unacceptable type, the app doesn't highlight, but when I drag a file of an acceptable type, it does highlight, and lets me drop.

When I drop, the app starts up, correctly, however, then I get a dialog saying:

The document "foo.tiff" could not be opened. DocView cannot open files in the "TIFF File" format.

I DO have this in my info.plist

<key>CFBundleTypeExtensions</key>
<array>
   <string>tif</string>
   <string>tiff</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>TIFFFile.icns</string>
<key>CFBundleTypeName</key>
<string>TIFF File</string>
<key>CFBundleTypeOSTypes</key>
<array>
   <string>TIFF</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Documents/</string>

Thanks.

Ergonomics answered 15/6, 2010 at 15:23 Comment(2)
is the <string>tif</string a typo? if not, may be one problem ...Carotenoid
yeah, it must have gotten deleted when I was re-formatting sorry. Corrected.Ergonomics
A
5

Does your app actually handle the file opening?

If it's an NSDocument application, you need to implement one of the file reading methods such as readFromData:ofType:error:. In an ordinary NSApplication your app delegate should handle it in application:openFile:. In both cases you need to return YES to acknowledge that you've successfully opened the file.

If you have implemented this, is the message being sent?

Afoul answered 15/6, 2010 at 15:59 Comment(3)
I have a Document class which is subclassed off of NSDocument. I have both readFromData:ofType:errors: and readFromURL:ofType:errors. Neither seems to be called.Ergonomics
Fair enough. JWWalker has probably nailed it, anyway.Afoul
yeah, he application:openFile is the way I needed to go. Thanks!Ergonomics
P
2

First, the part of the Info.plist that you show is within the CFBundleDocumentTypes array, not at the top level of the Info.plist, right?

Second, under LSHandlerRank you have Documents/, which is not a legal value, nor is Documents.

Third, you probably need to add NSDocumentClass.

Panache answered 15/6, 2010 at 16:7 Comment(5)
Where do I need to add NSDocumentClass? I have a Document class which is subclassed off of NSDocument already...Ergonomics
It's another key in the dictionary that includes the entries you posted:<key>NSDocumentClass</key> <string>YourDocumentClassName</string>Afoul
@Brian So which one do you want to get instantiated and sent the read... message to open a TIFF file?Afoul
@Afoul The problem is that I don't actually KNOW until I've actually opened the file... I have three kinds of .tiff files, multipage tiffs, single page images, and documents that are in multiple files, one per page (img-001 img-002 img-003, etc) and I don't know that until I've opened the file...Ergonomics
@Brian: In that case I guess you don't want to specify NSDocumentClass. Receive the file in an app delegate method like application:openFile: and then figure out what kind of document to use.Panache
J
0

Just another thing that sometimes gets me is that you need to include $(PRODUCT_MODULE_NAME). before your class name. If you use the pulldown menu in 10.2.1 in a Swift project Xcode doesn't seem to put a fully qualified name for your class in that field. In prior versions it did.

I also suspect, but haven't proven that the class and any classes the document inherits need to be Public or Open.

Jourdain answered 20/5, 2019 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.