1

In a simple rtf editor —based on NSDocument subclass—, when app starts, it creates an untitled file. This a desirable behaviour. But if I quit the app with this opened, unedited and unsaved document (empty!), the application will restore this document at the next launch.

How can I set this document so that it cannot be restored. If I uncheck the window's controller window "Restorable" property in IB, no document is restored ever, which is not the desirable behaviour: edited-saved documents that were not closed by user need being restored; untitled-unedited documents should not!

Denis
  • 775
  • 7
  • 22
  • Do you mean it's ending up with two untitled documents, the one from launching the app and the other being restored? Or is there only one? If there's only one, what's the difference between it being new or being restored? – Ken Thomases Nov 20 '18 at 01:04
  • No. When there is only one document opened (the empty untitled one) as I quit, it is restored but no new untitled is created. But if I have one edited/saved document and the empty one as I quit, both are restored… What is strange is that the initial position window is always ignored, any new or restored window cascade from the middle of the screen… – Denis Nov 20 '18 at 11:09

1 Answers1

0

I found a solution. First, I subclassed NSDocumentController and added in its implementation file:

+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler
{
    NSInteger restorable = [state decodeIntegerForKey:@"restorable"];
    if (!restorable) {
        completionHandler(nil, [NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]);
    }
    else {
        [super restoreWindowWithIdentifier:identifier state:state completionHandler:completionHandler];
    }
}

Then I added code in my NSDocument subclass implementation file

- (void) encodeRestorableStateWithCoder:(NSCoder *) coder {

    if (self.fileURL){
        [coder encodeInteger:1 forKey:@"restorable"];
    } else {
        [coder encodeInteger:0 forKey:@"restorable"];
    }
    [super encodeRestorableStateWithCoder:coder];
}

So the flag is set to 0 for any untitled document with a nil fileURL and there will be no restoration a launch. Other documents are restored.

Denis
  • 775
  • 7
  • 22