Lazarus

Programming => Operating Systems => macOS / Mac OS X => Topic started by: dbannon on September 23, 2019, 02:25:24 pm

Title: Mac's current dir
Post by: dbannon 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
Title: Re: Mac's current dir
Post by: madref 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.  
Title: Re: Mac's current dir
Post by: trev on December 03, 2019, 05:20:59 am
You can use this:

Replace it with this which has several fixes and works with Cocoa 64 bit: https://wiki.freepascal.org/Multiplatform_Programming_Guide#Proper_macOS_file_locations
Title: Re: Mac's current dir
Post by: trev 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