Forum > macOS / Mac OS X

How can I get application directory?

(1/3) > >>

How can I get the current working directory of the application?

GetCurrentDir work fine if I run the application from IDE, but if I launch the application from the bundle file .app (after a compilation) then this function always return the root / directory.

There is a method to have the application directory that work also from bundle file?

Thanks & goodbye

I have found this solution:

--- Code: ---  g_path := ExtractFilePath(Application.ExeName);
  g_path := LeftStr(g_path, Pos('', g_path)-1);

--- End code ---

so in g_path I have the application directory, also if I run from bundle file.

There is a better solution?


What I use are the following function:

Getinstalldir gives you what it is: the path to the executable.
Getbasedir gives you the path to the bundle (Mac), or just the same as getinstalldir for Win.

Hope this helps you out.


function slash(value:string):string;
if (value='') then result:=''
              else begin
                   {$IFDEF WINDOWS}
                   if (value[length(value)]<>'\') then result:=value+'\'
                   if (value[length(value)]<>'/') then result:=value+'/'
                                                  else result:=value;

function getinstalldir:string;

function getbasedir:string;

Vincent Snijders:
Instead of the function slash, you can use AppendPathDelim.

Some confusion and misinformation here:

Luigi: From a terminal command line in your project's folder, start your app like this:


That should give the same results for GetCurrentDir as double-clicking the .app in Finder. Not sure why you're always seeing / with GetCurrentDir when the app is launched from the Laz IDE. Maybe there's a problem there with how it's launching the app.

Not sure what you mean by "working directory". Normally this is a synonum for "current directory". If you mean the location of the .app, that's not what GetCurrentDir returns.

Why do you need to know that? An .app can be dragged and dropped anywhere in the file system, although typically it will be located under /Applications. So I'm not sure that knowing where it's located is something you would normally be using.

However, if do need to know that for some reason, don't use the supplied code. It won't work if the app bundle is named differently from its executable, which is common. For example "My" with "myprog" executable. You can just chop off the final 3 folder names, searching back from the end of ParamStr(0) for 3 occurences of PathDelim.

Also, remember that Laz creates your app bundle with a symlink in Contents/MacOs that points back to the actual executable in your project folder. You would never distribute or move an app bundle that's set up like that - it's only done so for convenience. Instead, you would delete the symlink and copy the executable file in its place.




[0] Message Index

[#] Next page

Go to full version