After googling a while without result here my question:

After googling a while without result here my question:
How can i get the app path in osx?
If i use
TPath.GetDirectoryName(ParamStr(0) ) i get
'/Users/jeanmarck/PAServer/scratch-dir/JeanmarcK-DEV-BOOk/Installer.app/Contents/MacOS'

But i want only the path to '/Users/jeanmarck/PAServer/scratch-dir/JeanmarcK-DEV-BOOk' because i want read a config file from there.

Is there any out of the box solution?

Comments

  1. The value returned by ParamStr(0) really is where the executable lies. Since the 'bundle' structure is completely standard, just go up enough folders:

    function GetBundleParentDir: string;
    var
    I, DelimsToGo: Integer;
    begin
    Result := ParamStr(0);
    {$IFDEF MACOS}
    DelimsToGo := 4;
    {$ELSE}
    DelimsToGo := 2; //assuming EXE has own folder
    {$ENDIF}
    for I := Result.Length - 1 downto 0 do
    if Result.Chars[I] = PathDelim then
    begin
    Dec(DelimsToGo);
    if DelimsToGo = 0 then
    Result := Result.Substring(0, I);
    end;
    end;

    Alternatively, for a Cocoa approach:

    uses MacApi.Foundation, MacApi.Helpers;

    function GetBundleParentDir: string;
    var
    Bundle: NSBundle;
    begin
    Bundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
    Result := ExtractFileDir(NSStrToStr(Bundle.bundlePath));
    end;

    ReplyDelete

Post a Comment