Recent

Author Topic: Finding correct preferences folder under Cocoa  (Read 1739 times)

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: Finding correct preferences folder under Cocoa
« Reply #15 on: October 18, 2019, 02:19:01 am »
@trev

Excellent, thanks for those references.

My preference files are cross-platform json. I just need a legal place to save them.
« Last Edit: October 18, 2019, 02:26:15 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: Finding correct preferences folder under Cocoa
« Reply #16 on: November 13, 2019, 05:02:47 pm »
The code for GetSupportDir does not compile, stopping at:

Code: Pascal  [Select]
  1. if Global then   // kLocalDomain
  2.   theError := FSFindFolder(kLocalDomain, FolderType, kDontCreateFolder, theRef)
  3. else             // kUserDomain
  4.   theError := FSFindFolder(kUserDomain , FolderType, kDontCreateFolder, theRef);
  5.  

with error: 'Got "FILES.FSRef" expected "MACOSALL.FSRef"'

Unless I change:
Code: Pascal  [Select]
  1. Uses
  2.   {$IFDEF DARWIN}
  3.   MacOSAll, Files
  4.   {$ENDIF}

to
Code: Pascal  [Select]
  1. Uses
  2.   {$IFDEF DARWIN}
  3.   Files, MacOSAll
  4.   {$ENDIF}

I assume this is ok, but I am not a macOS expert. It seems to work, maybe the wiki should be modified.
« Last Edit: November 13, 2019, 05:07:20 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: Finding correct preferences folder under Cocoa
« Reply #17 on: November 13, 2019, 05:35:38 pm »
Not quite. My tests on Mac, Win, and Lin suggest you need something like this:

Code: Pascal  [Select]
  1. { GetSupportDir
  2.   Return path to user application support directory. }
  3. function GetSupportDir: string;
  4. begin
  5.   {$IFDEF DARWIN}
  6.   result := GetSupportDir(false, kApplicationSupportFolderType)
  7.     + ApplicationName + PathDelim;
  8.   {$ELSE}
  9.   result := GetAppConfigDirUTF8(false);
  10.   {$ENDIF}
  11. end;

to get the application support directory. Otherwise the Mac code gives you the 'Application Support' directory, while Win and Lin give the application support directory in that directory.
« Last Edit: November 16, 2019, 03:53:30 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

trev

  • Full Member
  • ***
  • Posts: 240
  • Former Delphi 7 and Delphi 10.2 User
Re: Finding correct preferences folder under Cocoa
« Reply #18 on: November 30, 2019, 01:52:37 am »
The code for GetSupportDir does not compile, stopping at:

Code: Pascal  [Select]
  1. if Global then   // kLocalDomain
  2.   theError := FSFindFolder(kLocalDomain, FolderType, kDontCreateFolder, theRef)
  3. else             // kUserDomain
  4.   theError := FSFindFolder(kUserDomain , FolderType, kDontCreateFolder, theRef);
  5.  

with error: 'Got "FILES.FSRef" expected "MACOSALL.FSRef"'

Unless I change:
Code: Pascal  [Select]
  1. Uses
  2.   {$IFDEF DARWIN}
  3.   MacOSAll, Files
  4.   {$ENDIF}

to
Code: Pascal  [Select]
  1. Uses
  2.   {$IFDEF DARWIN}
  3.   Files, MacOSAll
  4.   {$ENDIF}

I assume this is ok, but I am not a macOS expert. It seems to work, maybe the wiki should be modified.

Weird. The former works for me with no errors (it was a cut and paste from my application).
o Lazarus v2.1.0 r61775, FPC v3.3.1 r42640, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

trev

  • Full Member
  • ***
  • Posts: 240
  • Former Delphi 7 and Delphi 10.2 User
Re: Finding correct preferences folder under Cocoa
« Reply #19 on: November 30, 2019, 02:23:21 am »
Not quite. My tests on Mac, Win, and Lin suggest you need something like this:

Code: Pascal  [Select]
  1. { GetSupportDir
  2.   Return path to user application support directory. }
  3. function GetSupportDir: string;
  4. begin
  5.   {$IFDEF DARWIN}
  6.   result := GetSupportDir(false, kApplicationSupportFolderType)
  7.     + ApplicationName + PathDelim;
  8.   {$ELSE}
  9.   result := GetAppConfigDirUTF8(false);
  10.   {$ENDIF}
  11. end;

to get the application support directory. Otherwise the Mac code gives you the 'Application Support' directory, while Win and Lin give the application support directory in that directory.

Nice catch. Indeed I was doing that in my application too.

I've now updated the Wiki code for GetSupportDir() to make the result for macOS the same as the rest so that you no longer need to add the ApplicationName. I've also fixed a compiler warning about an un-intialised variable. See: https://wiki.lazarus.freepascal.org/Multiplatform_Programming_Guide#Proper_macOS_file_locations

Thanks for posting!
o Lazarus v2.1.0 r61775, FPC v3.3.1 r42640, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: Finding correct preferences folder under Cocoa
« Reply #20 on: November 30, 2019, 02:52:01 am »
Excellent, great to see the update!

Cheers,
VTwin
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4