Sign in Join
Wiki copytemporary

Locating macOS app support, preferences folders: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11*(macOS only)*
22
33## Overview
44
55## Application Support and Preferences Folders
66
77The 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 [[Mac Preferences Read and Write|CFPreferences read/write methods]] will do this for you automatically.
88
99As a bonus, the function below will also return the user/global application support directories for other operating systems.
1010
1111> **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.
1212
1313\<syntaxhighlight lang="pascal\>
1414
1515...
1616
1717Uses
1818
1919` {$IFDEF DARWIN}`\
2020` MacOSAll, Files`\
2121` {$ENDIF}`
2222
2323...
2424
2525` {platform-independent method to retrieve application support/preferences directories`\
2626` `\
2727`  Global: true (global location) _or_ false (user location)`\
2828`  FolderType: kApplicationSupportFolderType _or_ kPreferencesFolderType`\
2929` `\
3030`  User preferences live in the location returned by FSFindFolder(kUserDomain,kPreferencesFolderType).`\
3131`  User application support items live in the location returned by FSFindFolder(kUserDomain, kApplicationSupportFolderType).`\
3232`  Global preferences live in the location returned by FSFindFolder(kLocalDomain,kPreferencesFolderType).`\
3333`  Global application support items live in the location returned by FSFindFolder(kLocalDomain, kApplicationSupportFolderType).`\
3434` }`
3535
3636function GetSupportDir(Global:boolean; FolderType:LongWord): String;
3737
3838{\$IFDEF DARWIN} const
3939
4040` kMaxPath = 1024;`
4141
4242var
4343
4444` theError: OSErr;`\
4545` theRef: FSRef;`\
4646` pathBuffer: PChar;`
4747
4848{\$ENDIF} begin
4949
5050` {$IFDEF DARWIN}`\
5151`   theRef := Default(FSRef);   // init `
5252
5353`   try`\
5454`     pathBuffer := Allocmem(kMaxPath);`\
5555`   except on exception `\
5656`     do exit;`\
5757`   end;`
5858
5959`   try`\
6060`     Fillchar(pathBuffer^, kMaxPath, #0);  // actually already done by allocmem`\
6161`     Fillchar(theRef, Sizeof(theRef), #0); // actually already done by Default();`\
6262`     if Global then   // kLocalDomain`\
6363`       theError := FSFindFolder(kLocalDomain, FolderType, kDontCreateFolder, theRef)`\
6464`     else             // kUserDomain`\
6565`       theError := FSFindFolder(kUserDomain , FolderType, kDontCreateFolder, theRef);`\
6666`     if (pathBuffer <> nil) and (theError = noErr) then`\
6767`       begin`\
6868`         theError := FSRefMakePath(theRef, pathBuffer, kMaxPath);`\
6969`         if theError = noErr then `\
7070`           GetSupportDir := UTF8ToAnsi(StrPas(pathBuffer)) + '/' + ApplicationName + '/';`\
7171`       end;`\
7272`   finally`\
7373`     Freemem(pathBuffer);`\
7474`   end`\
7575` {$ELSE}`\
7676`   //`\
7777`   // Other operating systems`\
7878`   //`\
7979`   GetSupportDir := GetAppConfigDirUTF8(Global);`\
8080` {$ENDIF}`
8181
8282end;
8383
84-</syntaxhighlight>
85-
8684## Actual preference folder locations
8785
8886- Preferences for [[Sandboxing for macOS|sandboxed]] applications with the *App Groups* entitlement may be found in `~/Library/Group Containers/[app identifier]/Library/Preferences`, where \[app identifier\] is something like com.companyname.appname or org.developername.appname but may be prefaced by alphanumeric characters.
8987
9088- Preferences for other [[Sandboxing for macOS|sandboxed]] and all non-sandboxed applications may be found in `~/Library/Containers/[app identifier]/Data/Library/Preferences`, where \[app identifier\] is something like com.companyname.appname or org.developername.appname.
9189
9290- Preferences which apply to all users, particularly if they apply before a user logs in, may normally be found in `/Library/Preferences`.
9391
9492- Preferences which apply to an individual user, only after they have logged in, may normally be found in `~/Library/Preferences`.
9593
9694Preference files should be [[macOS property list files|property list files]] and named something like com.companyname.appname.plist or org.developername.appname.plist. Using the [[Mac Preferences Read and Write|CFPreferences read/write methods]] will do this for you automatically and will also use the correct folder locations.
9795
9896## See also
9997
10098- [[Locating the macOS application resources directory]]
10199- [[Locating the macOS tmp directory]]
102100- [[Locating macOS significant directories]].
103101- [[Mac Preferences Read and Write|Reading and Writing macOS Preferences]].
104102
105103---
106104*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](/wiki-contact).*
107105Category: Code Snippets
108106Category: Platform-sensitive development
109107Category: macOS

Back to history