Recent

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

apeoperaio

  • Full Member
  • ***
  • Posts: 206
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: 1279
  • 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 »
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48688  macOS 11.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

apeoperaio

  • Full Member
  • ***
  • Posts: 206
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: 828
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

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

 

TinyPortal © 2005-2018