2

I have come across an (outdated) code snippet that uses File Manager functions (included in Carbon) to locate the shared application Preferences folder (which in most cases is just "/Library/Preferences"). It is something like:

#include <Carbon/Carbon.h>

...

FSSpec spec = { 0 };   // data type which specifies name and location of a file or folder
FSRef ref;             // data type which references in some sense a file or a folder      
OSErr err = fnfErr;

// find a 'preferences' type folder (specified by kPreferencesFolderType)        
// with the necessary permissions (specified by kUserDomain)
err = FindFolder(kUserDomain, ,
               1, &spec.vRefNum, &spec.parID);

// operate some conversions to put the folder path inside a string where you can 
// then append the app name to it.
FSpMakeFSRef(&spec, &ref);
FSRefMakePath(&ref, (UInt8*)filename, FL_PATH_MAX);

(kUserDomain and kPreferencesFolderType are enum values defined in CarbonCore/Folders.h.)

Unfortunately, many "FS" File Manager functions appear to be deprecated, especially those using FSSpec (https://developer.apple.com/library/mac/documentation/Carbon/reference/File_Manager/Reference/reference.html).

I was therefore wondering: what would it be the current proper way to locate the application preferences folder (without hard wiring "Library/Preferences/AppName" into the code)? Thanks

Emerald Weapon
  • 2,392
  • 18
  • 29
  • 1
    There's also `FSFindFolder`, making it easier to avoid `FSSpec`, though it's also deprecated. – JWWalker Jul 14 '14 at 22:33
  • Does this answer your question? [How do I store desktop application data in a cross platform way for python?](https://stackoverflow.com/questions/1084697/how-do-i-store-desktop-application-data-in-a-cross-platform-way-for-python) – thakis Jan 08 '20 at 13:53
  • @thakis Too long since I was versed in this topic... but I guess not because this question was not related to python. – Emerald Weapon Jan 09 '20 at 10:41

1 Answers1

1

I would dare imagine you want to use something that takes a NSSearchPathDirectory — presumable NSFileManager -URLsForDirectory:inDomains:, probably with NSLibraryDirectory and you'll need to add the Preferences bit yourself. Based on those options Apple exposes, I really don't think you're intended to read and write from the path yourself — it's just for the user defaults, with the exposed NSApplicationSupportDirectory being where you'd just throw other supporting data.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • Originally I wanted to use a pure C++ interface (so no NS-wathever). After some research it seems to me that no such a thing is not provided anymore, so the better way is to write a simple objective-c++ extension to my C++ code. Your answer pointed me in the right direction. – Emerald Weapon Jul 18 '14 at 10:18
  • Pedantically, you could technically do it in pure C by interrogating the runtime directly for the `IMP` and then calling that. But it would amount to using the Objective-C method without the Objective-C syntax — it'd be long-winded and ugly. – Tommy Jul 18 '14 at 22:17