What are the "Viewer" and "Editor" roles in Info.plist for?
14/09/04 09:28
In the Info.plist file, you can set up a CFBundleTypeRole for each document type, which can be either "Viewer" or "Editor". This entry describes how you can use these in Cocoa to allow importing/exporting various kinds of files, as well as implement template files ("stationeries") without having to write much code.
If you specify the "Viewer" role for your document type, it means that your application can open and read a particular file format, but can not save in this file format. Furthermore, Cocoa will open files of this type as "untitled" documents.
"Editor", on the other hand, means that your application can read, edit and write files of a particular type, and will give you the usual document behavior.
Now, what if your application can read a particular file format and export to it, but it is a lossy conversion? The user is not technically editing a file of this type. What you do is that you declare this file format's role as "viewer", and then add a NSExportableAs key to another document type. This key contains an array of strings with the names of other file formats to which this document type can be exported.
In addition, you add a "Save To..." menu item to your "File" menu, which will take care of calling dataRepresentationOfType: or a similar call to actually cause the export. All you have to do is look at the type parameter to find out what file format you're supposed to save to.
It works the same way for loadDataRepresentation:ofType: and importing "viewer-only" files. You can specify the same document class as you use for your native documents here, and just do an if( [type isEqualToString: @"foo"] ) to find out what file format you're expected to read.
You can even use this to implement stationeries, by making stationeries a read-only type and then doing a setType: to change their type to a different type that your app can save to. (You will need to do this in readFrom... method *after* calling the inherited method, or it won't stick)
Nice links for further reading:
NSDocument FAQ
"Editor", on the other hand, means that your application can read, edit and write files of a particular type, and will give you the usual document behavior.
Now, what if your application can read a particular file format and export to it, but it is a lossy conversion? The user is not technically editing a file of this type. What you do is that you declare this file format's role as "viewer", and then add a NSExportableAs key to another document type. This key contains an array of strings with the names of other file formats to which this document type can be exported.
In addition, you add a "Save To..." menu item to your "File" menu, which will take care of calling dataRepresentationOfType: or a similar call to actually cause the export. All you have to do is look at the type parameter to find out what file format you're supposed to save to.
It works the same way for loadDataRepresentation:ofType: and importing "viewer-only" files. You can specify the same document class as you use for your native documents here, and just do an if( [type isEqualToString: @"foo"] ) to find out what file format you're expected to read.
You can even use this to implement stationeries, by making stationeries a read-only type and then doing a setType: to change their type to a different type that your app can save to. (You will need to do this in readFrom... method *after* calling the inherited method, or it won't stick)
Nice links for further reading:
NSDocument FAQ