Forum > macOS / Mac OS X
How can I get application directory?
luigi65:
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
luigi65:
I have found this solution:
--- Code: --- g_path := ExtractFilePath(Application.ExeName);
{$IFDEF DARWIN}
g_path := LeftStr(g_path, Pos('myapp.app', g_path)-1);
{$ENDIF}
--- End code ---
so in g_path I have the application directory, also if I run from bundle file.
There is a better solution?
Jaco:
Hi,
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.
Jaco
function slash(value:string):string;
begin
if (value='') then result:=''
else begin
{$IFDEF WINDOWS}
if (value[length(value)]<>'\') then result:=value+'\'
{$ELSE}
if (value[length(value)]<>'/') then result:=value+'/'
{$ENDIF}
else result:=value;
end;
end;
function getinstalldir:string;
begin
result:=slash(extractfiledir(paramstr(0)));
end;
function getbasedir:string;
Begin
{$IFDEF WINDOWS}
result:=getinstalldir;
{$ELSE}
result:=copy(getinstalldir,1,pos(extractfilename(paramstr(0))+'.app/Contents/MacOS',getinstalldir)-1);
{$ENDIF}
end;
Vincent Snijders:
Instead of the function slash, you can use AppendPathDelim.
Phil:
Some confusion and misinformation here:
Luigi: From a terminal command line in your project's folder, start your app like this:
open myapp.app
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 Program.app" 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.
Thanks.
-Phil
Navigation
[0] Message Index
[#] Next page