1

I have an app, which is a single-window, non-document-based app.

I want to make it respond to NSWorkspace-openFile:withApplication:, but only when the path is to a folder, and also implement the File->Open menu. I'm having trouble tracking down how to do this (without becoming a document-based application).

Michael Johnston
  • 5,298
  • 1
  • 29
  • 37
  • I've discovered the first step which is to add LSItemContentTypes [public.directory] to CFBundleDocumentTypes in Info.plist – Michael Johnston Aug 16 '11 at 00:51
  • the second step was to implement application:openFile and openDocument: in my app controller. However, although my app does respond correctly to NSWorkspace-openFile:withApplication:, when I use the File->Open menu, the Open button in the dialog is still grayed out for directories – Michael Johnston Aug 16 '11 at 02:09
  • The final step was to subclass NSDocumentController, override runModalOpenPanel(openPanel, forTypes:extensions), and then do openPanel.setCanChooseDirectories(true) and call super – Michael Johnston Aug 21 '11 at 06:53

2 Answers2

1

Just check what action the Open menu item is connected to in Interface Builder. If I remember correctly, it would be connected to the "First Responder" object and the method open:. Is that right?

In this case, just implement the open: method in your AppDelegate class. (To understand why the method goes to the delegate, read about "nil-targeted actions" in Hillegass' book, or here: http://www.cocoadev.com/index.pl?NilTargetedAction. The thing to remember is that a control connected to "First Responder" in IB is actually IB's way of denoting that the target is nil.)

Note that you will have to implement the open panel yourself using NSOpenPanel -- see some code for example here: NSOpenPanel setAllowedFileTypes

If this is the same thing as that you're doing in openFile:withApplication:, you will probably want to create a common private method and call that method from both openFile:withApplication: and open:.

Community
  • 1
  • 1
Enchilada
  • 3,859
  • 1
  • 36
  • 69
1

You have to configure your NSOpenPanel to accept directories:

[myOpenPanel setCanChooseDirectories:YES];
omz
  • 53,243
  • 5
  • 129
  • 141
  • So there is no way to make the default NSDocument methods open a folder? – Michael Johnston Aug 18 '11 at 21:18
  • 1
    this is true, but the important piece is WHERE to do this, which is in a subclass of NSDocumentController, by overriding runModalOpenPanel(openPanel, forTypes:extensions), doing openPanel. setCanChooseDirectories(true) and calling super – Michael Johnston Aug 21 '11 at 06:51