Recent

Author Topic: Obtaining path for storing app preferences  (Read 115 times)

Cascade

  • New Member
  • *
  • Posts: 19
Obtaining path for storing app preferences
« on: October 02, 2024, 11:56:51 am »
I like to populate a structure with useful paths at app startup - e.g. path to current user’s home folder, where to store app preferences, that kind of thing.

One of the built in functions, SysUtils.GetAppConfigDir, is returning ‘/Users/(Username)/.config/…’ on macOS.  I was expecting it to return the Application Support folder.  I’ve come up with this little bit of code as an alternative - seems to work, so thought I’d share in case it proves useful:

Code: Pascal  [Select][+][-]
  1. {$MODESWITCH ObjectiveC1}
  2.  
  3. interface uses
  4.   Classes, SysUtils, CocoaAll, CocoaUtils;
  5.  
  6.  
  7. function GetMacOSAppUserDataFolder:String;
  8. var
  9.   paths:NSArray;
  10.   s:NSString;
  11. begin
  12.   paths:=NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
  13.     NSUserDomainMask, True); //Fetch Application Support folder from user domain.
  14.   s:=NSString(paths.firstObject); //Cast first item found to NSString.
  15.   result:=NSStringToString(s).TrimRight(['/'])+'/'; //Ensure one trailing slash.
  16.   end;

This returns ‘/Users/(Username)/Library/Application Support/’

 

TinyPortal © 2005-2018