Locating macOS significant directories

From Lazarus wiki

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)


To locate various significant directories in macOS you can use the native macOS Foundation function

NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);

which creates a list of path strings for the specified directories in the specified domains. The list is in the order in which you should search the directories.

Note: The directory returned by this method may not exist. This method simply gives you the appropriate location for the requested directory. Depending on the application’s needs, it may be up to the developer to create the appropriate directory and any in between.

Determining where to store files

Before we go any further, let's recap where the Apple Guidelines indicate your application should store its files:

  • Use the Application Support directory (this is backed up by Time Machine), appending your <bundle_ID>, for:
    • Resource and data files that your application creates and manages for the user. You might use this directory to store application state information, computed or downloaded data, or even user created data that you manage on behalf of the user.
    • Autosave files.
  • Use the Caches directory (this is not backed up by Time Machine), appending your <bundle_ID>, for cached data files or any files that your application can recreate easily.
  • Use CFPreferences to read and write your application's preferences. This will automatically write preferences to the appropriate location and read them from the appropriate location. This data is backed up by Time Machine.
  • Use the application Resources directory (this is backed up by Time Machine) for your application-supplied image files, sound files, icon files and other unchanging data files necessary for your application's operation.
  • Use NSTemporaryDirectory (this is not backed up by Time Machine) to store temporary files that you intend to use immediately for some ongoing operation but then plan to discard later. Delete temporary files as soon as you are done with them.

Get significant directories function

{$modeswitch objectivec1} 



function GetSignificantDir(DirLocation: qword; DomainMask: qword; count: byte): string;
  paths : NSArray;
  pathStr : ShortString;
  status: Boolean = false;  // initialise error status
  paths := NSSearchPathForDirectoriesInDomains(DirLocation, DomainMask, True);
  if(count < paths.count) then
      status := CFStringGetPascalString(CFStringRef(paths.objectAtIndex(count)),@pathStr,255,CFStringGetSystemEncoding());
      if(status = True) then
        Result := pathStr
        raise Exception.Create('Error in GetSignificantDir()');
    Result := '';

Table of Directory Locations

Directory Locations
Directory name Directory Description
NSApplicationDirectory Supported applications (/Applications)
NSDemoApplicationDirectory Unsupported applications and demonstration versions
NSDeveloperApplicationDirectory Developer applications (/Developer/Applications)
NSAdminApplicationDirectory System and network administration applications
NSLibraryDirectory Various user-visible documentation, support, and configuration files (/Library)
NSDeveloperDirectory Developer resources (/Developer)
NSUserDirectory User home directories (/Users)
NSDocumentationDirectory Documentation
NSDocumentDirectory Document directory
NSCoreServiceDirectory Core services (System/Library/CoreServices)
NSAutosavedInformationDirectory The user’s autosaved documents (Library/Autosave Information)
NSDesktopDirectory The user’s desktop directory
NSCachesDirectory Discardable cache files (Library/Caches)
NSApplicationSupportDirectory Application support files (Library/Application Support)
NSDownloadsDirectory The user’s downloads directory
NSInputMethodsDirectory Input Methods (Library/Input Methods)
NSMoviesDirectory The user’s Movies directory (~/Movies)
NSMusicDirectory The user’s Music directory (~/Music)
NSPicturesDirectory The user’s Pictures directory (~/Pictures)
NSPrinterDescriptionDirectory The system’s PPDs directory (Library/Printers/PPDs)
NSSharedPublicDirectory The user’s Public sharing directory (~/Public)
NSPreferencePanesDirectory The PreferencePanes directory for use with System Preferences (Library/PreferencePanes)
NSApplicationScriptsDirectory The user scripts folder for the calling application (~/Library/Application Scripts/<code-signing-id>
NSItemReplacementDirectory The constant used to create a temporary directory
NSAllApplicationsDirectory All directories where applications can be stored
NSAllLibrariesDirectory All directories where resources can be stored
NSTrashDirectory The user's trash directory

Table of Path Domains

Path Domains
Domain name Domain Description
NSUserDomainMask The user’s home directory—the place to install user’s personal items (~).
NSLocalDomainMask The place to install items available to everyone on this machine
NSNetworkDomainMask The place to install items available on the network (/Network)
NSSystemDomainMask A directory for system files provided by Apple (/System)
NSAllDomainsMask All domains

Code Examples

You can now locate various significant directories and display them as demonstrated by the code examples below.

User caches directory

procedure TForm1.MenuItem5Click(Sender: TObject);
  ShowMessage('User caches dir: ' + GetSignificantDir(NSCachesDirectory,NSUserDomainMask,0));

User trash directory

procedure TForm1.MenuItem16Click(Sender: TObject);
  ShowMessage('User trash dir: '
          + GetSignificantDir(NSTrashDirectory,NSUserDomainMask,0));

Users directory

Maybe not what you expect... this is the /Users directory :-)

procedure TForm1.MenuItem10Click(Sender: TObject);
  ShowMessage('Users dir: ' +  GetSignificantDir(NSUserDirectory,NSLocalDomainMask,0));

To retrieve the current user's home directory, you need to use the NSHomeDirectory() function instead:

procedure TForm1.MenuItem11Click(Sender: TObject);
  ShowMessage('User''s dir: ' + NSStringToString(NSHomeDirectory));

All application directories

procedure TForm1.MenuItem7Click(Sender: TObject);
  count: byte;
  for count := 0 to 12 do
   if GetSignificantDir(NSAllApplicationsDirectory, NSAllDomainsMask, count) <> '' then
      ShowMessage('Application dir ' + IntToStr(count) + ': '
           + GetSignificantDir(NSAllApplicationsDirectory,NSAllDomainsMask,count))

All user application directories

procedure TForm1.MenuItem8Click(Sender: TObject);
  count: byte;
  for count := 0 to 12 do
   if GetSignificantDir(NSAllApplicationsDirectory, NSUserDomainMask, count) <> '' then
      ShowMessage('User application dir ' + IntToStr(count) + ': '
           + GetSignificantDir(NSAllApplicationsDirectory,NSUserDomainMask,count))

All system application directories

procedure TForm1.MenuItem9Click(Sender: TObject);
  count: byte;
  for count := 0 to 12 do
   if GetSignificantDir(NSAllApplicationsDirectory, NSSystemDomainMask, count) <> '' then
      ShowMessage('System application dir ' + IntToStr(count) + ': '
           + GetSignificantDir(NSAllApplicationsDirectory,NSSystemDomainMask,count))