Swift Cocoa: NSOpenPanel

If you've ever saved something on a Mac, it's a good chance you interacted with an implementation of NSOpenPanel. Which has been around since 2001. To developers this friendly object is the ticket to the apple file system. If NSOpenPanel is conformed to properly, Mac apps are empowered to interact with the user's system under semi-supervision.


Our app is sandboxed, but we declare read and write for selected files. Below are my app target's capabilities.

In the app a user is able to drag an image into an image well. After that action returns an image, I am able to prepare an instance of NSOpenPanel. Below the panel will only allow the user to select directories. In the real app I represent the image as png data and write to an appended path component much like this.

This is everything necessary to touch files outside an app sandbox.


If you're looking to use the finder to open the files outside the sandbox, that's easy too.

And the last thing I found helpful was populating the open recently tab. After having your user interact with the panel maybe you want to hold some shortcut to that place. This is how you do that.

And finally you'll need to handle the case that a user tries to open one of those shortcuts.

In this example, I use the same finder open method to show the user something I just saved using NSOpenPanel.