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
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.
-(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];
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With