Recent

Author Topic: Path to application  (Read 32688 times)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Path to application
« on: February 16, 2010, 11:36:27 pm »
Is there a way to obtain the path to the running application in Mac OS X? ApplicationName returns simply the name of the application, not its directory.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Path to application
« Reply #1 on: February 16, 2010, 11:45:51 pm »
I found the solution myself. The path can be obtained with ParamStr(0).

According to http://www.freepascal.org/docs-html/rtl/sysutils/applicationname.html ApplicationName and ParamStr(0) should deliver the same result by default, but obviously this is not the case.

In

Code: [Select]
  gAppName := ApplicationName;
  gAppPath := ParamStr(0);

gAppName contains the name of the application only, gAppPath the whole path.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: Path to application
« Reply #2 on: February 17, 2010, 10:06:44 am »
It is not guaranteed, since many OSes don't allow it.

(and some OSes e.g. only allow it when the file is on a native filesystem)

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
Re: Path to application
« Reply #3 on: February 17, 2010, 04:40:54 pm »
Check application.location

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Path to application
« Reply #4 on: February 21, 2010, 06:20:36 pm »
Great, it works!

Unfortunately, the Free Pascal documentation (see http://www.freepascal.org/docs-html/fcl/custapp/tcustomapplication.location.html) says that on Mac OS X application.location would not work "under certain circumstances". Im am afraid that I have to live with this uncertainty and check for a plausible value.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: Path to application
« Reply #5 on: February 22, 2010, 12:34:16 pm »
You should really not rely on being able to locate where the actual executable is.
If you really need to know this at runtime, then save it in some config file (when installing the program) on a location (eihter global or local, depending on your needs) that is custom for the OS you're using (see GetAppConfigDir).

Bart

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Path to application
« Reply #6 on: February 22, 2010, 12:44:44 pm »
You should really not rely on being able to locate where the actual executable is.
For WIntel apps the application location can be a convenient location for an 'application inifile' to store global, static constants. It's easy to tweak such an inifile and there is no chicken/egg problem as to where to find it.
Not sure though what the conventions for the Mac platform are.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: Path to application
« Reply #7 on: February 23, 2010, 01:20:34 am »
For WIntel apps the application location can be a convenient location for an 'application inifile' to store global, static constants.
As long as you limit yourself to DOS/WIndows, you'll be fine.
Once you start cross-platform, it's gonna break.

Bart

shiruba

  • New Member
  • *
  • Posts: 10
    • Galapagos Software
Re: Path to application
« Reply #8 on: March 08, 2010, 01:42:32 pm »
Another issue is that even on Windows, a non-power user won't have write access to the Program Files directory where the application is most likely stored.
Galapagos Software - Lazarus Code

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Path to application
« Reply #9 on: March 08, 2010, 03:03:20 pm »
Another issue is that even on Windows, a non-power user won't have write access to the Program Files directory where the application is most likely stored.
Sure thing.
Therefore you can use 2 inifiles:
- one for global (i.e. workstation) config items which can be in the program folder;
- one for user-specific settings that change everytime, which can be in a user's home folder.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: Path to application
« Reply #10 on: March 08, 2010, 10:14:38 pm »

Unfortunately, the Free Pascal documentation (see http://www.freepascal.org/docs-html/fcl/custapp/tcustomapplication.location.html) says that on Mac OS X application.location would not work "under certain circumstances". Im am afraid that I have to live with this uncertainty and check for a plausible value.

Won't work on FreeBSD either. (unless FreeBSD 8 changed that)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Path to application
« Reply #11 on: March 09, 2010, 12:43:32 am »
Another issue is that even on Windows, a non-power user won't have write access to the Program Files directory where the application is most likely stored.

Would the user have write access if the application were on a usb stick?
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Path to application
« Reply #12 on: March 09, 2010, 02:47:34 am »
Quote
Would the user have write access if the application were on a usb stick?
Depending on the file system, if it supports file permissions, then there's a chance that the write access is also prohibited.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Path to application
« Reply #13 on: March 13, 2010, 03:37:08 pm »
Does anybody have an alternative suggestion for storing preferences of a portable application (USB stickware)?
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Path to application
« Reply #14 on: March 15, 2010, 08:01:20 am »
Quote
Does anybody have an alternative suggestion for storing preferences of a portable application (USB stickware)?
I think there's no clean way but to inform your users by providing readme for them containing information about the permission

 

TinyPortal © 2005-2018