How to create a document type for import only in Cocoa?

There, the file type my application imports, but does not save. I added the entry to the document types and set it to be read-only, but this does not lead to the import behavior I'm looking for. Instead, my application will simply open the file, and when I save the original file, it will be overwritten in my own format.

How do I configure my document or document types to make it so that a new document is created with data from the original document instead of the original that was opened?

+4
source share
3 answers

1. Declare file types as document types

As part of the Xcode project, add a Document Type for all file formats supported by your application. Set the Role of each type depending on your application capabilities:

  • Mark file types with read / write support as Editor ;
  • Mark import only file types as View .

Set the Class for the type of document with which you want to process each file type. A single document class can handle several types of files.

The following example shows three types of files: font-pestle, otf, and ttf. The first, font-pestle, is its own application format. This type has the role of Editor .

The other two formats, otf and ttf, can be imported but not written by the application; therefore they are marked as Viewer .

Example Document Types

2. Additional file types in a subclass of NSDocument

When adding document types, the application will automatically allow users to open files of the specified types.

You need to add the file type processing code to the document class. Ideally, add the branch code to the readFromData:ofType:error: method:

 - (BOOL)readFromData:(NSData*)someData ofType:(NSString*)typeName error:(NSError**)outError { if ([NSWorkspace.sharedWorkspace type:@"eu.miln.font-pestle" conformsToType:typeName] == YES) { // read native format } else if ([NSWorkspace.sharedWorkspace type:@"public.opentype-font" conformsToType:typeName] == YES) { // read import only format // disassociate document from file; makes document "untitled" self.fileURL = nil; // associate with primary file type self.fileType = @"eu.miln.font-pestle"; } else // ... } 

Important self.fileURL = nil; . By setting fileURL to nil, you say that the document is not associated with any file on disk and should be considered as a new document.

To enable automatic saving, implement the NSDocument autosavingFileType method to return the main file type.

+11
source

Alex, thanks for your answer, but I found a way that I like more:

 - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError { *outError = nil; if ([typeName isEqualToString:@"SomeReadOnlyType"]) { // .. (load data here) [self setFileURL:nil]; return result; } else { // .. (do whatever you do for other documents here) } } 

That way, you can still use the document system provided by Cocoa, instead of rewinding my own.

I also documented the solution here: http://www.cocoadev.com/index.pl?CFBundleTypeRole a bit down the page.

+4
source

I do not believe that the import function is supported by default in Cocoa. When the user clicks the Open button in the open panel, the framework calls openDocumentWithContentsOfURL:display:error: on the NSDocumentController . This is where the document system determines the type of file you open and consults the Info.plist file to find out which subclass of NSDocument use to open the document.

You can subclass NSDocumentController and override the openDocumentWithContentsOfURL:display:error: method to intercept the types of files you want to import rather than open. In the NSDocument subclass NSDocument write a new initializer with a name like initWithImportedContentsOfURL:type:error: (or something with a better name :-)) to create a new untitled document and read the contents of the imported file.

+1
source

Source: https://habr.com/ru/post/1286050/


All Articles