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