10

I added a applicationShouldOpenUntitledFile method to my application delegate, returning NO as Apple's documentation specifies. However, I'm still getting a new document on startup. What's wrong?

@implementation AppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog( @"This is being called" );
}

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    NSLog( @"This never is" );
    return NO;  
}

@end

5 Answers 5

12

You're running Lion. When you ran before adding the applicationShouldOpenUntitledFile handler, a new document was created. Now, with 10.7's "Restore windows when quitting and re-opening apps", your application is restoring that untitled window, and not creating a new one as you suppose.

Close that window and re-run your application, and applicationShouldOpenUntitledFile will be called and will suppress the creation of a new untitled file.

7
  • Correct - this is actually specified in Lion's release notes. Sep 27, 2011 at 17:18
  • But not in the documentation for NSApplicationDelegate, which is where I was looking. :) NSApplicationDelegate describes the old behaviour as if it still applies without any mention at all of the new behaviour. I imagine that'll get fixed in a future SDK. Sep 27, 2011 at 17:57
  • The release notes are the documentation. Sure, something might not yet have been updated in the API reference, but you should always read the release notes for any OS updates. Sep 27, 2011 at 20:28
  • Do you have a specific suggestion for improving this question/answer? I tried to add a link to Apple's documentation, but it's documented by implication rather than explicitly stated anywhere. Sep 27, 2011 at 21:15
  • 1
    You mean besides pointing out where this information is mentioned in my first comment? No. Sep 27, 2011 at 21:56
7
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
    // Schedule "Checking whether document exists." into next UI Loop.
    // Because document is not restored yet. 
    // So we don't know what do we have to create new one.
    // Opened document can be identified here. (double click document file)
    NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(openNewDocumentIfNeeded) object:nil];
    [[NSOperationQueue mainQueue] addOperation: op];
}

-(void)openNewDocumentIfNeeded
{
    NSUInteger documentCount = [[[NSDocumentController sharedDocumentController] documents]count];

    // Open an untitled document what if there is no document. (restored, opened).       
    if(documentCount == 0){
        [[NSDocumentController sharedDocumentController]openUntitledDocumentAndDisplay:YES error: nil];
    }
}
1
  • This is also useful for non-document aware applications that want the same basic behaviour. Thanks for this post! Aug 8, 2015 at 21:45
2

I'm using Xcode 8.3.2 and compiling for Os X 10.11 using a storyboard for a document based app. I noted that, if you set the window controller as initial controller, a window is created without any document and without calling applicationShouldOpenUntitledFile.

I solved removing the "is initial controller" checkbox in the storyboard.

1

If you're not running Lion / 10.7 or later, this can still happen if you have some other window open (even a non-Document window) when applicationShouldOpenUntitledFileshould be called.

I have a Document-based app where the AppDelegate class opens a global logging window, both for debugging purposes and for user status messages. If I have the program display that window on startup while running on OS X 10.6, applicationShouldOpenUntitledFile never gets called, even with no document windows displayed. If I turn that window off, the call is made.

1

Since OSX Lion, the app's state restoration may interfere with your custom preferences for this exercise.

Citing an update to Aaron Hillegass and Adam Preble's book Cocoa Programming for MacOSX:

Note that Mac OS X Lion's state-restoration features may make it tricky to observe the new document preference. You can disable state restoration by editing the Run scheme in Xcode. Open the product menu and select Edit Scheme. Select the Run RaiseMan.app scheme, change to the Options pane, and check Disable state restoration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.