3

EDIT: It seems like the issue below only occurs (rather, fails to occur... :) when I run the from within Xcode (I'm on 4.4), rather than from Finder. Is this an Xcode bug, or am I missing something?

I want to create a new (untitled) NSDocument when the app starts up, in the case that no documents were automatically restored. Both TextEdit and Safari display the behavior I want, so it seems like this should be built-in, but I can't find any option for it.

Steps for recreating my problem: if I create a new document-based app in Xcode, and run it, the first time it runs it indeed creates an untitled document. But, if I close that document, quit the app, and run it again, it doesn't give me a new document, unless I explicitly click on the app icon in the Dock. This is different than e.g. TextEdit, where if you quit and relaunch, you get a new empty document. (Changing the bundle identifier gives me another new untitled document, as expected.)

My first thought was that maybe this should occur in applicationDidFinishLaunching:, but it turns out that that method is executed before any previously opened documents are restored.

justin k.
  • 504
  • 1
  • 6
  • 15

1 Answers1

4

This behaviour is part of the document architecture. Your application delegate should implement the following method of the Protocol NSApplicationDelegate

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender;
{
    return YES;
}

You find more about this here.

Stephan
  • 4,263
  • 2
  • 24
  • 33
  • As I said in the edit, the issue only occurs when running from Xcode. `applicationShouldOpenUntitledFile:` is already implemented exactly as you've suggested. – justin k. Dec 09 '12 at 06:03
  • @justink. ahh ok ... now i got it. Ok first feedback i can reproduce it on MacOS 10.8. But only if applicationShouldTerminateAfterLastWindowClosed return NO – Stephan Dec 10 '12 at 16:42