Recent

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

dbannon

  • Hero Member
  • *****
  • Posts: 1297
    • 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: 831
  • ..... 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 2.0.6 / FPC 3.0.4
Lazarus Trunc / FPC Trunc
Mac OS X Mojave

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1282
  • Former Delphi 1-7, 10.2 User
Re: Mac's current dir
« Reply #2 on: December 03, 2019, 05:20:59 am »
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48840   macOS 11.2.2 aarch64 Xcode 12.4
Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 3.2.0 Win10 Parallels VM

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1282
  • 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
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48840   macOS 11.2.2 aarch64 Xcode 12.4
Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 3.2.0 Win10 Parallels VM

 

TinyPortal © 2005-2018