Sign in Join
Wiki copytemporary

Locating macOS app support, preferences folders

(macOS only)

Overview

Application Support and Preferences Folders

The function below will return the location the macOS user/global application support directories and the user/global application preferences directories. You would not normally need to manually locate the macOS user preferences directory because the CFPreferences read/write methods will do this for you automatically.

As a bonus, the function below will also return the user/global application support directories for other operating systems.

Note: While the FSFindFolder() function was deprecated by Apple after macOS 10.8 (Mountain Lion), it is still available in macOS 10.15 (Catalina), In the interests of longevity, you may prefer to use the NSSearchPathForDirectoriesInDomains() function instead which is available in macOS 10.0+. See Locating macOS significant directories for details.

<syntaxhighlight lang="pascal>

...

Uses

 {$IFDEF DARWIN}
 MacOSAll, Files
 {$ENDIF}

...

 {platform-independent method to retrieve application support/preferences directories
 
  Global: true (global location) _or_ false (user location)
  FolderType: kApplicationSupportFolderType _or_ kPreferencesFolderType
 
  User preferences live in the location returned by FSFindFolder(kUserDomain,kPreferencesFolderType).
  User application support items live in the location returned by FSFindFolder(kUserDomain, kApplicationSupportFolderType).
  Global preferences live in the location returned by FSFindFolder(kLocalDomain,kPreferencesFolderType).
  Global application support items live in the location returned by FSFindFolder(kLocalDomain, kApplicationSupportFolderType).
 }

function GetSupportDir(Global:boolean; FolderType:LongWord): String;

{$IFDEF DARWIN} const

 kMaxPath = 1024;

var

 theError: OSErr;
 theRef: FSRef;
 pathBuffer: PChar;

{$ENDIF} begin

 {$IFDEF DARWIN}
   theRef := Default(FSRef);   // init 

   try
     pathBuffer := Allocmem(kMaxPath);
   except on exception 
     do exit;
   end;

   try
     Fillchar(pathBuffer^, kMaxPath, #0);  // actually already done by allocmem
     Fillchar(theRef, Sizeof(theRef), #0); // actually already done by Default();
     if Global then   // kLocalDomain
       theError := FSFindFolder(kLocalDomain, FolderType, kDontCreateFolder, theRef)
     else             // kUserDomain
       theError := FSFindFolder(kUserDomain , FolderType, kDontCreateFolder, theRef);
     if (pathBuffer <> nil) and (theError = noErr) then
       begin
         theError := FSRefMakePath(theRef, pathBuffer, kMaxPath);
         if theError = noErr then 
           GetSupportDir := UTF8ToAnsi(StrPas(pathBuffer)) + '/' + ApplicationName + '/';
       end;
   finally
     Freemem(pathBuffer);
   end
 {$ELSE}
   //
   // Other operating systems
   //
   GetSupportDir := GetAppConfigDirUTF8(Global);
 {$ENDIF}

end;

Actual preference folder locations

Preference files should be property list files and named something like com.companyname.appname.plist or org.developername.appname.plist. Using the CFPreferences read/write methods will do this for you automatically and will also use the correct folder locations.

See also


From the Free Pascal wiki, page "Locating macOS app support, preferences folders", as it stood on 2022-02-13 after a change by Thaddy, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by Thaddy .

Edited on wiki.freepascal.org by 2 people, 2019–2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
TrevNew macOS content :-
TrevApplication Support and Preferences Folders: Fixed comment (minor)
TrevAdded platform template; updated code comments (minor)
TrevAdded section on actual folder locations
TrevActual preference folder locations: Fixed typo (minor)
TrevActual preference folder locations: Minor elaboration (minor)
TrevApplication Support and Preferences Folders: Added Apple function deprecation note and alternative function reference
TrevAdded see also section (minor)
TrevApplication Support and Preferences Folders: Fixed a typo (minor)
TrevApplication Support and Preferences Folders: Updated page link (minor)
TrevSee also: Added another category (minor)
TrevSee also: Add entry
TrevSee also: Add page links
TrevSee also: Remove redundant page link (minor)
ThaddyApplication Support and Preferences Folders

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.