Recent

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

dbannon

  • Hero Member
  • *****
  • Posts: 788
    • 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 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

madref

  • Hero Member
  • *****
  • Posts: 700
  • ..... A day not Laughed is a day not Lived !!
    • 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 2.0.2 / FPC 3.0.4
Lazarus Trunc / FPC 3.0.4
Mac OS X Mojave

trev

  • Sr. Member
  • ****
  • Posts: 258
  • Former Delphi 7 and Delphi 10.2 User
Re: Mac's current dir
« Reply #2 on: December 03, 2019, 05:20:59 am »
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

  • Sr. Member
  • ****
  • Posts: 258
  • Former Delphi 7 and Delphi 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
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)