What is the easiest way with Swift 4 for a macOS application to find the important users directories like: Desktop, Documents, Downloads etc.?
2 Answers
TLDR:
let desktopDir = try? FileManager.default.url(for: .desktopDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let documentDir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let downloadsDir = try? FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
There are two methods to get this information, both from the FileManager
class:
This one:
func urls(for directory: FileManager.SearchPathDirectory,
in domainMask: FileManager.SearchPathDomainMask) -> [URL]
Returns an array of URLs for the specified common directory in the requested domains. […] The directories are ordered according to the order of the domain mask constants, with items in the user domain first and items in the system domain last.
Documentation: https://developer.apple.com/documentation/foundation/filemanager/1407726-urls
(already mentioned in @leo-dabus' answer)
And this one:
(which I prefer since it's more versatile and returns a single URL)
func url(for directory: FileManager.SearchPathDirectory,
in domain: FileManager.SearchPathDomainMask,
appropriateFor url: URL?,
create shouldCreate: Bool) throws -> URL
Locates and optionally creates the specified common directory in a domain.
Documentation: https://developer.apple.com/documentation/foundation/filemanager/1407693-url
For either one:
The valid domains, defined in https://developer.apple.com/documentation/foundation/filemanager.searchpathdomainmask are:
.userDomainMask
: The user’s home directory—the place to install user’s personal items (~).
.localDomainMask
: Local to the current machine—the place to install items available to everyone on this machine.
.networkDomainMask
: Publicly available location in the local area network—the place to install items available on the network (/Network).
.systemDomainMask
: Provided by Apple—can’t be modified (/System) .
.allDomainsMask
: All domains.
There are lots of possibilities for the directory
parameter, defined in https://developer.apple.com/documentation/foundation/filemanager.searchpathdirectory :
.applicationDirectory
: Supported applications (/Applications).
.demoApplicationDirectory
: Unsupported applications and demonstration versions.
.developerApplicationDirectory
: Developer applications (/Developer/Applications).
.adminApplicationDirectory
System and network administration applications.
.libraryDirectory
: Various user-visible documentation, support, and configuration files (/Library).
.developerDirectory
: Developer resources (/Developer).
.userDirectory
: User home directories (/Users).
.documentationDirectory
: Documentation.
.documentDirectory
: Document directory.
.coreServiceDirectory
: Location of core services (System/Library/CoreServices).
.autosavedInformationDirectory
: Location of user’s autosaved documents (Library/Autosave Information).
.desktopDirectory
: Location of user’s desktop directory.
.cachesDirectory
: Location of discardable cache files (Library/Caches).
.applicationSupportDirectory
: Location of application support files (Library/Application Support).
.downloadsDirectory
: Location of the user’s downloads directory.
.inputMethodsDirectory
: Location of Input Methods (Library/Input Methods).
.moviesDirectory
: Location of user's Movies directory (~/Movies).
.musicDirectory
: Location of user's Music directory (~/Music).
.picturesDirectory
: Location of user's Pictures directory (~/Pictures).
.printerDescriptionDirectory
: Location of system's PPDs directory (Library/Printers/PPDs).
.sharedPublicDirectory
: Location of user's Public sharing directory (~/Public).
.preferencePanesDirectory
: Location of the PreferencePanes directory for use with System Preferences (Library/PreferencePanes).
.applicationScriptsDirectory
: Location of the user scripts folder for the calling application (~/Library/Application Scripts/),
.itemReplacementDirectory
: Passed to the FileManager methodurl(for:in:appropriateFor:create:)
in order to create a temporary directory.
.allApplicationsDirectory
: All directories where applications can occur.
.allLibrariesDirectory
: All directories where resources can occur.
.trashDirectory
: Location of the trash directory.

- 2,399
- 2
- 18
- 28
You can use FileManager urls method
func urls(for directory: FileManager.SearchPathDirectory, in domainMask: FileManager.SearchPathDomainMask) -> [URL]
and pass FileManager.SearchPathDirectory
.desktopDirectory
, .documentDirectory
or .downloadsDirectory
for the user domain mask and get the first url returned:
let desktopDirectory = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let downloadsDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!

- 229,809
- 59
- 489
- 571
-
5Why this returns me for .documentDirectory path file:///Users/username/Library/Containers/... and not path file:///Users/username/Documents/ ? – Dawy Apr 30 '19 at 20:53
-
2Dawy, I had this too. Most likely you need to give your app permission to access Downloads, Movies, etc. There are `Capabilities` tab and `File Access` area there. Check out the options there. – Stanislav Pankevich Oct 04 '19 at 08:40
-
I'm struggling with this right now - it's a new problem, I've built successfully on Catalina before. Currently, my Capabilities "File Access" section doesn't mention anything about Documents, and I can't get the user's home directory Documents. Very frustrating. – Apollo Grace Feb 04 '20 at 18:21
-
2Aha! got it. I'd accidentally somehow turned on Sandboxing in my app's Entitlements. – Apollo Grace Feb 04 '20 at 18:24
-
By default MacOS apps are sandboxed which means no access to Documents folder directly unless the user gives explicit access. So if you receive a documents URL of this lovely sort : `file:///Users/bioche/Library/Containers/com.bioche.UIApp/Data/Documents/pointFree/report.txt` check that in the entitlements file, the `App Sandbox` property is set to NO – Bioche Apr 10 '20 at 19:21