views:

72

answers:

1

I'm creating an NSDocument application, with two document types: Website and Web Service. This is in my Info.plist:

<key>CFBundleDocumentTypes</key>
 <array>
  <dict>
   <key>CFBundleTypeName</key>
   <string>Website</string>
   <key>CFBundleTypeExtensions</key>
   <array>
    <string>website</string>
   </array>
   <key>LSTypeIsPackage</key>
   <true/>
   <key>CFBundleTypeRole</key>
   <string>Editor</string>
   <key>LSHandlerRank</key>
   <string>Default</string>
   <key>NSDocumentClass</key>
   <string>AWWebSite</string>
  </dict>
  <dict>
   <key>CFBundleTypeName</key>
   <string>Web Service</string>
   <key>CFBundleTypeExtensions</key>
   <array>
    <string>webservice</string>
   </array>
   <key>LSTypeIsPackage</key>
   <true/>
   <key>CFBundleTypeRole</key>
   <string>Editor</string>
   <key>LSHandlerRank</key>
   <string>Default</string>
   <key>NSDocumentClass</key>
   <string>AWWebService</string>
  </dict>
 </array>

Now, whenever the user opens the application, selects the 'New' item from the menubar, or clicks the Dock icon while there are no open windows, I want to show a window with two options, each for one of the document types. Can anyone help me with this? Thanks

+1  A: 

What you need to do is override - [NSDocumentController newDocument:]. NSDocumentController is part of the responder chain and is the object that eventually handles the newDocument: message it sends.

From there, you can show whatever dialog you like and then call makeUntitledDocumentOfType:error:, addDocument:, makeWindowControllers and showWindows. This is what openUntitledDocumentAndDisplay:error: does.

But the catch is that NSDocumentController is a singleton, so you need to make sure it's your subclass that gets instantiated, not Apple's. Typically, you do this by adding an object of your subclass to MainMenu.xib or whatever NIB gets loaded first. That's usually good enough to make sure that your subclass gets created first and becomes the singleton.

Alex
Thanks alot! I get this working for the menu item but not the Dock icon and the Application startup =[
Time Machine
You could override `openUntitledDocumentAndDisplay:error:` instead; I believe this is what is called when you click the dock icon and when the app starts.
Alex
@Alex: Thanks =]
Time Machine