Recent

Author Topic: [Solved] Finding correct preferences folder under Cocoa  (Read 5732 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • 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

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • 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

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • 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

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 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).

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 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!

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • 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

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Finding correct preferences folder under Cocoa
« Reply #21 on: December 19, 2019, 09:36:39 pm »
This is what I use:

https://wiki.lazarus.freepascal.org/Multiplatform_Programming_Guide#Proper_macOS_file_locations

You are right, this works for both Carbon and Cocoa. It is necessary to include MacOSAll in the uses clause for LCLCocoa, too.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018