Get current location

I need to know the folderi tem from where I started an application

Used to:
f = GetFolderItem("")

but in API2 it creates a warning, why it was changed, who knows ???

many thanks

You should start with Application.ExecutableFile and work out what you need from there. On macOS this gives you the binary location within the .app package, so if you need a path relative to the .app package, then you’ll have to do some traversal.

var f as FolderItem = app.ExecutableFile
1 Like

I displayed

and got this, looks very strange to me
that does not help

the documentation says:

You can get the location of the folder in which the app is running, by getting the Parent of the executable file:

Var f As FolderItem
f = App.ExecutableFile.Parent

but the result is not what it says

What is the path you are expecting?

the path should be:

I start the app as /Users/jkienbrandt/MyAppExample/

You will get the folder item from where the app is startet with
var f as new FolderItem("")
It’s the version with API 2.0

I usually use this in the open event.

1 Like

thanks Michael, that’s working

but why is the following not working ?

var f as FolderItem
f = new FolderItem("")

I’ve tested both:
var f as new FolderItem("")
var f as FolderItem
f = new FolderItem("")
The result in my test was the same.


your example does work, but in API2, I get a warning that constructor is deprecated …

so how do it right ? well Xojo might know it, they invented it, even it was so easy to use method ‘GetFolderItem’, what’s wrong with that method ?
Nothing, except it has to be complicated !!!

I have XOJO Release 2020, Version 2.1
The API 2.0 documentation says: " If you pass the empty string to FolderItem.Constructor, it returns the FolderItem for the folder that contains the application."

The GetFolderItem method is API 1.0 and deprecated since XOJO Version 2019r2.

I’m using Xojo version 2020r2.
yes it works but still get the warning about constructor deprecated.
Do you get this warning ?

No. No warning. I’ve done the “Analyze Project” and got only warnings for unused parameters, unused local variables and unused event parameters.

thanks Michael,

that’s how it is,
using the old or new method the outcome is the same …

thanks for your help

If you’re concerned about the deprecation warning (which is visible when you enable the warning Item1 has a replacement), then you can do the traversal from App.ExecutableFile.

var f as FolderItem = app.ExecutableFile.Parent
#if TargetMacOS then
    f = f.Parent.Parent.Parent

What the deprecation warning is really telling you, though, is that you should provide the PathMode parameter:

var f as new FolderItem( "", FolderItem.PathModes.Native )
1 Like

I’m not concerned about the deprecated warning, the question is why do I get the warning if it should be the right way to do so in API2.
I still prefer a proper methode to get the application folder than having to code parent.parent.parent, what a strange way, how many parents do we have ?

Because this.

if I should provide a pathmode, than I should get an compile error

Did you ever look at the inside of a macOS app? The executable is very much inside the app bundle and needs the 3 parents. There is nothing strange in this.