Recent

Author Topic: Application directory and exe name  (Read 3082 times)

apeoperaio

  • Sr. Member
  • ****
  • Posts: 282
Application directory and exe name
« on: December 21, 2020, 01:37:52 pm »
I would like to know if there is a way to get my application directory on macOS.

On Windows and Linux I can use ParamStr(0) and Application.ExeName but on macOS they don't work in the same way as in Windows and Linux (documented here: https://www.freepascal.org/docs-html/rtl/system/paramstr.html).

Here two different tests.

On macOS if I add the path of my application to PATH:
export PATH=/Applications/myapp.app/Contents/MacOS:$PATH

If I use ParamStr(0) I get:
myapp
If I use Application.Exename I get:
/Applications/myapp.app/Contents/MacOS/myapp

If I create a link to myapp:
ln -s /Users/myname/Documents/myapp myapplnk
using ParmaStr(0) I get:
myapplnk
Using Application.Exename I get:
/Users/myname/Documents/myapplnk

Is there a way to get the full path of my exe even using macOS commands?



trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: Application directory and exe name
« Reply #1 on: December 21, 2020, 01:46:21 pm »
I'm not completely sure what you're trying to do, but check my Wiki articles:

* Locating macOS app bundle, resources directories - [on re-reading your post you're probably after this article]
* Locating macOS significant directories
* Locating macOS app support, preferences folders

One of them should have what you want - if not please clarify what you are after and why.
« Last Edit: December 21, 2020, 01:53:21 pm by trev »

apeoperaio

  • Sr. Member
  • ****
  • Posts: 282
Re: Application directory and exe name
« Reply #2 on: December 21, 2020, 05:14:59 pm »
Dear @trev,
Thank you for the information and links.

What I am trying to do is to get the full path of my application, regardless where my executable is.

I tried:

NSBundle.mainBundle.executablePath.UTF8String

This function provides me the full path of my application executable, except if I call my application using a symbolic link.
If I call my application through a symbolic link NSBundle.mainBundle.executablePath.UTF8String gives me the full path to my symbolic link.

Is there a function that can be used to get the full path of my application?

Thank you.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1067
Re: Application directory and exe name
« Reply #3 on: December 21, 2020, 06:25:15 pm »
Application.Exename uses the official function for that. The fact that it returns a symbolic link's location if that's how the application is started, is therefore by design. It also enables the Lazarus trick of creating an application bundle that contains a symbolic link to an executable outside the bundle.

Starting an application bundle via a symbolic link that resides outside that bundle is simply not something that is supported on macOS (at least not if the application needs any resources that are inside the application bundle). Conversely, you can have two different application bundles with one having a symbolic link to an exe in the other bundle, and you'll be able to start both application bundles independently, each considering their own bundle as "the" application location. Hence, if you try to hack your way around the way the system behaves by default, you are going to have a different view on where your exe is located vs what the system considers to be its location. That is not a good idea.

apeoperaio

  • Sr. Member
  • ****
  • Posts: 282
Re: Application directory and exe name
« Reply #4 on: December 22, 2020, 09:57:48 am »
Got it, thank you

 

TinyPortal © 2005-2018