Lazarus

Programming => Operating Systems => macOS / Mac OS X => Topic started by: Grahame Grieve on November 02, 2022, 10:37:24 am

Title: GetCurrentDir on MacOS
Post by: Grahame Grieve on November 02, 2022, 10:37:24 am
If I set the working directory to {folder} in the Run Parameters dialog, shouldn't that {folder} be what I get back from SysUtils.GetCurrentDir ?

Because GetCurrentDir is returning / not the folder I set. Is that expected?

Details:
* Mac M1 Max / Monterey 12.5
* Lazarus 2.2.5 (rev lazarus_2_2_4-3-g564e1e8244) FPC 3.2.2 aarch64-darwin-cocoa
Title: Re: GetCurrentDir on MacOS
Post by: Jonas Maebe on November 02, 2022, 08:32:55 pm
The system always forces the working directory of GUI applications to the root directory, probably to discourage you from trying to use it. You cannot/must not rely on it on macOS for GUI applications.
Title: Re: GetCurrentDir on MacOS
Post by: Grahame Grieve on November 02, 2022, 08:44:43 pm
oh. thanks. Where is that documented? Should the dialog box disable the working directory in that case?
Title: Re: GetCurrentDir on MacOS
Post by: Jonas Maebe on November 02, 2022, 08:48:55 pm
oh. thanks. Where is that documented?
It's more that Apple doesn't mention using the working directory as a correct way to access any files: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html

Quote
Should the dialog box disable the working directory in that case?
It should still work fine for command line programs.
Title: Re: GetCurrentDir on MacOS
Post by: Grahame Grieve on November 02, 2022, 09:30:57 pm
Lazarus can know which is which, right?
Title: Re: GetCurrentDir on MacOS
Post by: Jonas Maebe on November 02, 2022, 09:33:18 pm
I don't know if it keeps track of that in the project file. There's also nothing preventing you from creating a GUI application using the template of a command line application (with or without the LCL).
Title: Re: GetCurrentDir on MacOS
Post by: fredb on December 13, 2023, 10:49:39 am
This code to use application file path :

Code: Pascal  [Select][+][-]
  1.   LocalDir := ExtractFilePath(Application.ExeName) + DirectorySeparator;
  2.   {$IFDEF DARWIN}
  3.   LocalDir := LeftStr(LocalDir, Pos('application_name.app', LocalDir)-1) + DirectorySeparator;
  4.   {$ENDIF}
  5.  

Replace 'application_name.app' by your own application name.
Title: Re: GetCurrentDir on MacOS
Post by: madref on December 16, 2023, 12:44:36 am
this works too:
Code: Pascal  [Select][+][-]
  1. ProgramDir := LeftStr (ExtractFilePath(Application.ExeName), Length(ExtractFilePath(Application.ExeName))-15);
TinyPortal © 2005-2018