Search code examples
swiftfindernsworkspace

"Ask for Finder Items" in Swift


I am trying to open a file-browsing window similar to the one shown in the screenshot where you can "select files and click open/select" in my Swift application when the user clicks on the "Browse" button.

temporary Finder window

In Automator, this is done by using the "Ask for Finder Items" action, and in Swift

NSWorkspace().selectFile("\(fileName)", inFileViewerRootedAtPath: "/Library")

opens up a new instance of Finder.app itself; what should I do if I would like to emulate the behavior seen in the Automator action?


Solution

  • You're looking for the NSOpenPanel class. It has several configurable properties for which kinds of files can be selected, and when the user finishes selecting files, you can access them via the URLs property. You can also read more about it in the File System Programming Guide.