macosdelphipathc++builderc++builder-xe6

How to get user-independent Library path for Mac OS X?


Using XE6, the Embarcadero documentation here states that TPath::GetLibraryPath () "Returns the path to a directory to store any data that your application needs store, regardless of the user".

Yet, in the Sample Path table, they list OS X's for this function as

/Users/username/Library

which seems user-specific. The first question is whether this is, in fact, the correct folder to use to store data regardless of the user. The second question is - if not - how to get the folder to the user-non-specific Library in OS X?


Solution

  • I use something like this for retrieving this folder.

    uses
      Macapi.Foundation, Macapi.Helpers;
    
    type
      ENSFileManagerException = class(Exception);
    
    function GetFolder_ProgramData: string;
    var
      FileManager: NSFileManager;
      ID: Cardinal;
      Domain: Cardinal;
      Url: NSURL;
      Bundle: NSBundle;
    begin
      ID := NSApplicationSupportDirectory; // ProgramData on Windows
      Domain := NSLocalDomainMask;
    
      FileManager := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager);
      Url := FileManager.URLForDirectory(ID, Domain, nil, False, nil);
      if URL <> nil then
        Result := NSStrToStr(Url.path)
      else
        raise ENSFileManagerException.CreateFmt('Could not retrieve folder for ID %d', [ID]); //Error.localizedDescription.UTF8String);
    end;