Recent

Author Topic: Mac's current dir  (Read 2431 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2794
    • tomboy-ng, a rewrite of the classic Tomboy
Mac's current dir
« on: September 23, 2019, 02:25:24 pm »
Hmm, I wonder if this is what you expect ?
Quote
procedure TForm1.FormShow(Sender: TObject);
begin
    memo1.append('Current = ' + GetCurrentDirUTF8());
    memo1.append('PWD = ' + GetEnvironmentVariable('PWD'));
end;
     
Running from within Lazarus on my Linux box I get -
Quote
Current = /home/dbannon/Pascal/ImportRTF
PWD = /home/dbannon/Pascal/ImportRTF
However, if I do the same thing on my Mac, I see -
Quote
Current = /
PWD = /Users/dbannon/bin/Lazarus/fixes_2/
The latter is, you guessed it, where my Lazarus install is. Not the directory the project files are saved in. But the really interesting one is the former, GetCurrentDirUTF8() returns root ?

If I run the app from the commandline, still on the Mac, the PWD entry gives a sensible result, but GetCurrentDirUTF8() still lies. Its a LazFileUtils function, should be OK on the Mac ?

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Mac's current dir
« Reply #1 on: October 30, 2019, 12:52:35 am »
You can use this:
Code: Pascal  [Select][+][-]
  1. // -----------------------------------------------------------------------------
  2. // Platform-independend method to search for the location of preferences folder}
  3. // -----------------------------------------------------------------------------
  4. function GetPreferencesFolder: String;
  5. const kMaxPath = 1024;
  6. var {$IFDEF LCLCarbon}
  7.     theError: OSErr;
  8.     theRef: FSRef;
  9.     {$ENDIF}
  10.     pathBuffer: PChar;
  11. begin
  12.   {$IFDEF LCLCarbon}
  13.     try
  14.       pathBuffer := Allocmem(kMaxPath);
  15.     except on exception do exit;
  16.     end;  // try
  17.     try
  18.       Fillchar(pathBuffer^, kMaxPath, #0);
  19.       Fillchar(theRef, Sizeof(theRef), #0);
  20.       theError := FSFindFolder(kOnAppropriateDisk, kPreferencesFolderType, kDontCreateFolder, theRef);
  21.       if (pathBuffer <> nil) and (theError = noErr) then
  22.       begin
  23.         theError := FSRefMakePath(theRef, pathBuffer, kMaxPath);
  24.         if theError = noErr then GetPreferencesFolder := UTF8ToAnsi(StrPas(pathBuffer)) + '/';
  25.       end;  // if
  26.     finally
  27.       Freemem(pathBuffer);
  28.     end; // try
  29.   {$ELSE}
  30.     GetPreferencesFolder := GetAppConfigDir(false);
  31.   {$ENDIF}
  32. end;     // GetPreferencesFolder
  33.  
or
Code: Pascal  [Select][+][-]
  1.          ProgramDir := LeftStr (ExtractFilePath(Application.ExeName), Length(ExtractFilePath(Application.ExeName))-15);
  2.          UserDir := GetUserDir;
  3.  
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

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Mac's current dir
« Reply #2 on: December 03, 2019, 05:20:59 am »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Mac's current dir
« Reply #3 on: December 03, 2019, 05:40:09 am »
Hmm, I wonder if this is what you expect ?
[...]
However, if I do the same thing on my Mac, I see -
Quote
Current = /
PWD = /Users/dbannon/bin/Lazarus/fixes_2/
The latter is, you guessed it, where my Lazarus install is. Not the directory the project files are saved in. But the really interesting one is the former, GetCurrentDirUTF8() returns root ?

If I run the app from the commandline, still on the Mac, the PWD entry gives a sensible result, but GetCurrentDirUTF8() still lies. Its a LazFileUtils function, should be OK on the Mac ?

Code: Pascal  [Select][+][-]
  1. program getcurdir;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses SysUtils;
  6.  
  7. begin
  8.  writeln(SysUtils.GetCurrentDir);
  9.  writeln(GetEnvironmentVariable('PWD'));
  10. end.
  11.  

produces:

Quote
trev@macmini6 [/Users/trev/Documents/LAZPROJECTS/test5] $ ./getcurdir
/Users/trev/Documents/LAZPROJECTS/test5
/Users/trev/Documents/LAZPROJECTS/test5
trev@macmini6 [/Users/trev] $ /Users/trev/Documents/LAZPROJECTS/test5/getcurdir
/Users/trev
/Users/trev

 

TinyPortal © 2005-2018