Recent

Author Topic: Path to application on Mac?  (Read 4657 times)

Spoonhorse

  • Full Member
  • ***
  • Posts: 123
Path to application on Mac?
« on: December 31, 2022, 10:23:48 am »
I’m using Lazarus on the Mac for the first time, trying to port/finish a little app in preparation for porting a bigger app.

In order to locate the resources for the app, I figured I needed to know where it was located. (If I’m wrong, and I’m setting y’all an XY problem, please let me know.)

But whether I use Paramstr(0) or the more approved Application.ExeName, then on the Mac instead of getting the actual file path /Users/myname/Documents/Hamsters/project1, I instead get /Users/myname/Documents/Hamsters/project1.app/Contents/MacOS/project1

This doesn’t exist. >:D

Any advice? Thanks for your help.


wp

  • Hero Member
  • *****
  • Posts: 12364
Re: Path to application on Mac?
« Reply #2 on: January 02, 2023, 12:23:46 pm »
I'm not a mac specialist, but this questions might be related to application bundles: https://wiki.freepascal.org/Multiplatform_Programming_Guide#macOS_2

Spoonhorse

  • Full Member
  • ***
  • Posts: 123
Re: Path to application on Mac?
« Reply #3 on: January 02, 2023, 03:09:56 pm »
Thank you both. OK, Macs are weird, converting my Big Project's source from Windows to Windows-and-Mac is going to be a doozy I can see. I think I can finish off this little one now though.

Spoonhorse

  • Full Member
  • ***
  • Posts: 123
Re: Path to application on Mac?
« Reply #4 on: January 02, 2023, 03:24:40 pm »
It still seems strange though.

In both this little practice project and in the big one, what I want to do among other things is (a) include some example files of the type that the app uses, accessible from the File menu of the app (b) include a file that remembers which file you have open and opens it when you open the app, so I can ship it "remembering" that it's looking at '/examples/example1.foo' in the app directory.

This has all been a great deal of trouble to try to get it to work under all circumstances and yet it seems such an obvious and modest thing to want to do. All I need is for the app to be able to find out where it is. Why should this be difficult?

wp

  • Hero Member
  • *****
  • Posts: 12364
Re: Path to application on Mac?
« Reply #5 on: January 02, 2023, 04:54:18 pm »
All I need is for the app to be able to find out where it is. Why should this be difficult?
The application is in Application.Location.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1761
Re: Path to application on Mac?
« Reply #6 on: January 02, 2023, 07:11:02 pm »
I use this. Its not an example of elegance, but it works.
Code: Pascal  [Select][+][-]
  1. function SafeGetApplicationPath: String;
  2. var
  3.   StartPath: String;
  4.   {$ifdef Darwin}
  5.   x:integer;
  6.   {$endif}
  7. begin
  8.  {$ifdef LCL}
  9.  StartPath:=Application.ExeName;
  10.  {$else}
  11.  StartPath:=Paramstr(0);
  12.  {$endif}
  13.  {$ifdef Darwin}
  14.  // we need the .app itself !!
  15.  x:=pos('/Contents/MacOS',StartPath);
  16.  if x>0 then
  17.  begin
  18.    Delete(StartPath,x,MaxInt);
  19.    (*
  20.    x:=RPos('/',StartPath);
  21.    if x>0 then
  22.    begin
  23.      Delete(StartPath,x+1,MaxInt);
  24.    end;
  25.    *)
  26.  end;
  27.  {$endif}
  28.  if FileIsSymlink(StartPath) then
  29.  begin
  30.    try
  31.      StartPath:=GetPhysicalFilename(StartPath,pfeException);
  32.    except
  33.    end;
  34.  end;
  35.  result:=StartPath;
  36. end;

Spoonhorse

  • Full Member
  • ***
  • Posts: 123
Re: Path to application on Mac?
« Reply #7 on: January 03, 2023, 02:50:49 am »
wp --- alas, that gives the same results on the Mac as everything else I've tried.

Spoonhorse

  • Full Member
  • ***
  • Posts: 123
Re: Path to application on Mac?
« Reply #8 on: January 03, 2023, 02:57:50 am »
Don Alfredo --- thanks. That works! I shall put it with the other useful code snippets I don't quite understand ...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5678
  • Compiler Developer
Re: Path to application on Mac?
« Reply #9 on: January 03, 2023, 09:32:02 pm »
In order to locate the resources for the app, I figured I needed to know where it was located. (If I’m wrong, and I’m setting y’all an XY problem, please let me know.)

But whether I use Paramstr(0) or the more approved Application.ExeName, then on the Mac instead of getting the actual file path /Users/myname/Documents/Hamsters/project1, I instead get /Users/myname/Documents/Hamsters/project1.app/Contents/MacOS/project1

This doesn’t exist. >:D

That is the correct path, because on macOS an App is in fact simply an archive that contains the binary and other required resources. Which also provides the answer to your question: the resources are supposed to be located inside the App archive as well (except if you have multiple Apps sharing the same resources; that would probably be a task for a Framework then).

 

TinyPortal © 2005-2018