Sign in Join
Wiki copytemporary

Locating macOS app support, preferences folders: what changed

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

Title: Locating macOS app support, preferences folders

OldNewLine
1+*(macOS only)*
2+
3+## Overview
4+
5+## Application Support and Preferences Folders
6+
7+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 [[Mac Preferences Read and Write|CFPreferences read/write methods]] will do this for you automatically.
8+
9+As a bonus, the function below will also return the user/global application support directories for other operating systems.
10+
11+> **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.
12+
13+\<syntaxhighlight lang="pascal\>
14+
15+...
16+
17+Uses
18+
19+` {$IFDEF DARWIN}`\
20+` MacOSAll, Files`\
21+` {$ENDIF}`
22+
23+...
24+
25+` {platform-independent method to retrieve application support/preferences directories`\
26+` `\
27+`  Global: true (global location) _or_ false (user location)`\
28+`  FolderType: kApplicationSupportFolderType _or_ kPreferencesFolderType`\
29+` `\
30+`  User preferences live in the location returned by FSFindFolder(kUserDomain,kPreferencesFolderType).`\
31+`  User application support items live in the location returned by FSFindFolder(kUserDomain, kApplicationSupportFolderType).`\
32+`  Global preferences live in the location returned by FSFindFolder(kLocalDomain,kPreferencesFolderType).`\
33+`  Global application support items live in the location returned by FSFindFolder(kLocalDomain, kApplicationSupportFolderType).`\
34+` }`
35+
36+function GetSupportDir(Global:boolean; FolderType:LongWord): String;
37+
38+{\$IFDEF DARWIN} const
39+
40+` kMaxPath = 1024;`
41+
42+var
43+
44+` theError: OSErr;`\
45+` theRef: FSRef;`\
46+` pathBuffer: PChar;`
47+
48+{\$ENDIF} begin
49+
50+` {$IFDEF DARWIN}`\
51+`   theRef := Default(FSRef);   // init `
52+
53+`   try`\
54+`     pathBuffer := Allocmem(kMaxPath);`\
55+`   except on exception `\
56+`     do exit;`\
57+`   end;`
58+
59+`   try`\
60+`     Fillchar(pathBuffer^, kMaxPath, #0);  // actually already done by allocmem`\
61+`     Fillchar(theRef, Sizeof(theRef), #0); // actually already done by Default();`\
62+`     if Global then   // kLocalDomain`\
63+`       theError := FSFindFolder(kLocalDomain, FolderType, kDontCreateFolder, theRef)`\
64+`     else             // kUserDomain`\
65+`       theError := FSFindFolder(kUserDomain , FolderType, kDontCreateFolder, theRef);`\
66+`     if (pathBuffer <> nil) and (theError = noErr) then`\
67+`       begin`\
68+`         theError := FSRefMakePath(theRef, pathBuffer, kMaxPath);`\
69+`         if theError = noErr then `\
70+`           GetSupportDir := UTF8ToAnsi(StrPas(pathBuffer)) + '/' + ApplicationName + '/';`\
71+`       end;`\
72+`   finally`\
73+`     Freemem(pathBuffer);`\
74+`   end`\
75+` {$ELSE}`\
76+`   //`\
77+`   // Other operating systems`\
78+`   //`\
79+`   GetSupportDir := GetAppConfigDirUTF8(Global);`\
80+` {$ENDIF}`
81+
82+end;
83+
84+</syntaxhighlight>
85+
86+## Actual preference folder locations
87+
88+- 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.
89+
90+- 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.
91+
92+- Preferences which apply to all users, particularly if they apply before a user logs in, may normally be found in `/Library/Preferences`.
93+
94+- Preferences which apply to an individual user, only after they have logged in, may normally be found in `~/Library/Preferences`.
95+
96+Preference 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.
97+
98+## See also
99+
100+- [[Locating the macOS application resources directory]]
101+- [[Locating the macOS tmp directory]]
102+- [[Locating macOS significant directories]].
103+- [[Mac Preferences Read and Write|Reading and Writing macOS Preferences]].
104+
105+---
106+*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).*
107+Category: Code Snippets
108+Category: Platform-sensitive development
109+Category: macOS

Back to history