O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.5. Selecting Files with Open Panels

As you know, AppKit's NSDocument class handles your application's FileOpen menu command for you. It reads the list of supported file extensions and file types out of your application's Info.plist file, and displays files of that type in an open panel. When you select one of those files, AppKit instantiates the document class appropriate for the selected file, and passes the file contents to the document.

Sometimes you need to drive the open panel yourself. You might be working on an application that doesn't have documents, or your document may itself refer to one or more files. In either case, working with the open panel is fairly easy.

The NSOpenPanel class defined by AppKit provides all of the functionality necessary to run an open panel modally, either as a sheet or as a separate dialog window. NSOpenPanel is itself based on the NSSavePanel class, used for driving a corresponding save file window. While NSSavePanel provides some of the basic implementation, NSOpenPanel extends this to include filtering for specific files and file types, returning multiple files, selecting directories as well as files, and other features.

In the following Try It Out, you use an open panel to add image files to a slide show document. You can add slides to an existing document by choosing the Slide ShowAdd Slides command or by creating a new slide show document ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required