Recent

Author Topic: GetCurrentDir on MacOS  (Read 1461 times)

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
GetCurrentDir on MacOS
« 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
« Last Edit: November 02, 2022, 10:53:47 am by Grahame Grieve »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: GetCurrentDir on MacOS
« Reply #1 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.

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: GetCurrentDir on MacOS
« Reply #2 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?

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: GetCurrentDir on MacOS
« Reply #3 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.

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: GetCurrentDir on MacOS
« Reply #4 on: November 02, 2022, 09:30:57 pm »
Lazarus can know which is which, right?

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: GetCurrentDir on MacOS
« Reply #5 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).

fredb

  • Newbie
  • Posts: 1
Re: GetCurrentDir on MacOS
« Reply #6 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.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: GetCurrentDir on MacOS
« Reply #7 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);
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

 

TinyPortal © 2005-2018