Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an import-only document type in Cocoa?

There's a file type my application import but not save. I've added an entry to the document types and set it to read-only, but that doesn't yield the import behaviour that I'm looking for. Instead, my app will just open the file and when I save the original file is overwritten in my own file format.

How to set up my document or document types to make it so that a new document is created with the data from the original document, instead of the original being opened?

like image 207
Sijmen Mulder Avatar asked Jun 11 '09 06:06

Sijmen Mulder


2 Answers

1. Declare the file types as Document Types

Within your Xcode project, add a Document Type for all the file formats your application supports. Set the Role of each type according to your application's abilities:

  • Mark read/write capable file types as Editor;
  • Mark import only file types as Viewer.

Set the Class to the document type you want to handle each file type. One document class can handle multiple file types.

In the example below, there are three file types declared: font-pestle, otf, and ttf. The first, font-pestle, is the native format of the application. This type has the role Editor.

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

Example Document Types

2. Additional file types in your NSDocument subclass

With the Document Types added, the application will automatically allow users to open files of the specified types.

You need to add file type handling code to your document class. In the ideal case, add the branching 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 // ...

}

The self.fileURL = nil; is important. By setting fileURL to nil, you are saying the document is not associated with any on-disk file and should be treated as a new document.

To allow auto-saving, implement the NSDocument method autosavingFileType to return the primary file type.

like image 95
Graham Miln Avatar answered Sep 28 '22 01:09

Graham Miln


Alex, thanks for your answer, but I found a way that I like a bit 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)
    }
}

This way it's still possible to use the document system provided by Cocoa instead fo rolling my own.

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

like image 23
Sijmen Mulder Avatar answered Sep 28 '22 00:09

Sijmen Mulder