As one of the most common and regular stuff that you need to implement on every application of every platform, you should be able to select files and directories from the system easily, following official documentation of course. Unfortunately, there's not a lot of sources where you can obtain indications or examples of how to implement a file/directory picker in MacOS and the ones you find are usually outdated because they don't mention for which version of Swift are they.
In this article, i want to share with you some examples of how to use the native MacOS file/directory picker using Swift 5 that i use regularly on my applications.
A. Implementing the file picker
When you first attempt to open an app from an unverified developer, Apple will block it, displaying an alert box instead. MacOS will always prevent apps it doesn’t recognize from launching without your approval. It’s also possible that your macOS security settings prevent any apps from being launched that aren’t from the App Store. Swift is everywhere. And now it’s open for everyone. Swift is free and open source, and it’s available to a wide audience of developers, educators, and students under the Apache 2.0 open source license. We’re providing binaries for macOS and Linux that can compile code for iOS, macOS, watchOS, tvOS, and Linux. Get Practical Server Side Swift. Swift on the server is an amazing new opportunity to build fast, safe and scalable backend apps. Write your very first web-based application by using your favorite programming language. Learn how to build a modular blog engine using the latest version of the Vapor 4 framework. Download Glance – Quick Look Plugin for macOS 10.15 or later and enjoy it on your Mac. See what's in your files without opening them! Glance boosts your productivity by providing Quick Look previews for files that macOS doesn't support out of the box. Power up your desktop. Includes free updates for SwiftUI and new Swift versions – click to read my update policy. Whether you have existing iOS skills or you're starting fresh, you can learn macOS coding by building real-world projects with Swift 5.2 and Xcode – all you need is in this course. Add an App Store icon. Open an existing Swift package. Link a target to a package product. Upload a macOS app to be notarized.
All the job is handled basically by the NSOpenPanel class. Instead of implementing your own file browser, the Open panel class is used as a very convenient way to allow the user to look for a file or directory in the system. The easiest way to display this dialog and handle the user selection is described on the following snippet:
You simply need to create an instance of the NSOpenPanel
and call the runModal
method to display it. Previously, you may need to change some default properties as the possiblity of selecting multiple files etc. The method will return a code that can be compared with the NSApplication.ModalResponse
struct, a set of button return values for modal dialogs. With a condition you should check if the user has selected a file and clicked on ok, finally you should verify that the url property of the dialog instance is not nil and you will be able to obtain the path of the file selected by the user.
A.1. Filtering by file extension
In some cases, the user shouldn't be able to pick any file in the system but files with specifical formats. For example, a regular case is the fact of picture manipulation software, where the user should be able to select only pictures with extensions as png, jpg or jpeg. The following snippet should display a filepicker that allows the user to select images only (like the image of the article):
A.2. Selecting multiple files
Swift Macos Example
If you want to allow the user to select multiple files at time, be sure to set the allowsMultipleSelection option of the dialog to true. Then the user should be able to select whatever and how many files he wants. Be sure as well to change the code to manipulate the result. Instead of working with the dialog.url
property, use the dialog.urls as this contains the array with the selected files. You can do whatever you want with the results, extract the path from every item of the array with the path property as shown in the following example:
B. Implementing the directory picker
For the implementation of the directory picker, we will use the same NSOpenPanel
class but you will need to change the 2 following properties to the dialog during its initialization:
This will allow the user to select only directories in the system. The rest of the logic is basically the same as with the filepicker. For example:
B.1. Select a single directory
Macos Adding App To Open Swift Performance
The following snippet shows the implementation of a picker that allows the user to select a single directory only:
Macos Adding App To Open Swift Without
B.2. Select multiple directories
Macos Adding App To Open Swift Now
The following snippet shows the implementation of a picker that allows the user to select multiple directories at the same time:
Macos Adding App To Open Swift Today
Happy coding !