File Path for debug and runtime

I have tried all day searching for the answer to the path puzzle. I am sure it is a simple answer but it has eluded me.

I have a Databases Folder included with my application. The following code works fine while in the debug mode, but I get the old NilObjectException in the compiled app. I suspect the path to the Database folder is incorrect.

I use CopyDBfolder to place the Databases folder in the App Parent Folder.

// set Database file path Dim pathToDB As FolderItem #If Debugbuild Then pathToDB =GetFolderItem("").Child("Databases").Child("Locations_DB.rsd") ' this works fine in debug mode #else pathToDB =GetFolderItem("").Child("Databases").Child("Locations_DB.rsd") ' what needs to go here this code causes NilObjectException #endif

The Databases folder is in the MacOS folder in the Contents. I want the db file in the bundle. Should I put it elsewhere in the bundle? Also I need it to work with Windows.

Thanks for your help.

The image did not make it onto the post. I will have to figure out how this works.
It is a shared image on DropBox.
https://www.dropbox.com/s/zqnt1frppat2cat/DB%20file%20path.jpg

On Mac,

For the non-debug build, use Application.ExecutableFile which will get the file that lives at:

MyApp.app/Contents/MacOS/MyApp

You can then use child/parent calls to navigate to your Databases folder.

Windows is different, since there is no Bundle.

Michael,

Would this be the correct line of code?

pathToDB = app.ExecutableFile.Child("Databases").Child("Locations_DB.rsd")

Thanks
David

You may also wish to enclose a compound path statement in a try/catch. Otherwise, if any of the components are nil or do not exist, the app will fire an exception.

try
     pathToDB = app.ExecutableFile.Child("Databases").Child("Locations_DB.rsd")
catch
     pathToDb = ""
end

if pathToDB = "" then
    'do your not found action
else
    'do your found path action
end

[quote=36511:@David Parrish]Michael,

Would this be the correct line of code?

pathToDB = app.ExecutableFile.Child("Databases").Child("Locations_DB.rsd")

Thanks
David[/quote]

I believe you are missing two calls to .Parent() - the first .Parent will get the folder which holds the Executable (“MacOS”), the next will get the Bundle folder itself (“Contents”) :

pathToDB = app.ExecutableFile.Parent.Parent.Child("Databases").Child("Locations_DB.rsd")

Hi Guy,

pathToDB is a folderItem so I changed the " " to nil but…
it did not locate my db file.

Would it be better to place the Databases folder in another place?
Would that make it easier for me to make it work with Windows?

I used the Build Step -> Copy Files to place the Databases folder in the App Parent Folder
Is there documentation on this anywhere??
There are other options, Resource Folder, Framework Folder, Bundle Parent Folder, Contents Folder
These correspond with the Mac bundle, do they also work with windows in any way? When I add the Copy Folders to a windows build it shows the same options.

Is there a better approach for including database files with an app?

Michael, just saw your latest post while I was typing this. I will try your suggestion.

Thanks all!

[quote=36520:@Michael Diehr]I believe you are missing two calls to .Parent() - the first .Parent will get the folder which holds the Executable (“MacOS”), the next will get the Bundle folder itself (“Contents”) :

pathToDB = app.ExecutableFile.Parent.Parent.Child("Databases").Child("Locations_DB.rsd")

I am using this code to test…

[code]Dim pathToDB As FolderItem
try
pathToDB = app.ExecutableFile.Parent.Parent.Child(“Databases”).Child(“Locations_DB.rsd”)
catch
pathToDb = nil
end

if pathToDB = nil then
'do your not found action
MsgBox “not found”
else
'do your found path action
MsgBox “found”
end[/code]

It still does not find my db file???

[quote=36521:@David Parrish]Hi Guy,
Is there documentation on this anywhere??
There are other options, Resource Folder, Framework Folder, Bundle Parent Folder, Contents Folder
These correspond with the Mac bundle, do they also work with windows in any way? When I add the Copy Folders to a windows build it shows the same options. Is there a better approach for including database files with an app?
[/quote]

It’s complicated - you can put a database in the App bundle on Mac (or in the Program Files folder on Windows) but you will run into security/permissions issues doing this. Is better to think of these folders are read-only locations. If you are making an app for the Mac App Store, for example, writing to a database inside your app bundle will prevent your app from getting approved.

Is your database read-only or read/write?

The database is read/write.
Should I use SpecialFolder.ApplicationData?

How do I get the Databases Folder in there during the build? Do I use a script? Obviously new to this.

Thanks

Put the initial database in the bundle and when you first run see if the db already exists in the location you expect it to be in.
If not copy if from the bundle to that location.

Thats what the IDE does with the language reference as it does get written to so HAS to be in a read/write location.

The upside to this is IF the user deletes that then you can put a fresh new copy there & keep on working.

Thanks Norman