Copy db file on App.Open for Mac

Hi. I’m getting this to work no problem on Windows. However when testing on a Mac, the file is not copying over as expected. Here is my code:

[code] // Find original DB file
Dim sourceDB As FolderItem
sourceDB = GetFolderItem(“Resources”).Child(“hangDEMOdb2.sqlite”)

If sourceDB <> Nil And sourceDB.Exists Then
// Create destination folder
Dim appData As FolderItem = SpecialFolder.ApplicationData
Dim appFolder As FolderItem = appData.Child(“com.visualveggiessoftware.masappwraptest”)
appFolder.CreateAsFolder

// Copy DB to destination
sourceDB.CopyFileTo(appFolder)

End If[/code]

I was wondering if the Resources folder is unique to Windows and I should use something else for Mac. If I am to use a different destination folder for Mac in the CopyFiles (Contents, App Parent, Framework, Bundle Parent), what would the GetFolderItem name be?
sourceDB = GetFolderItem("???").Child(“hangDEMOdb2.sqlite”)

Also, and not sure if this matters, when I did Insert > Build Step > Copy Files, my CopyFiles is below Build in the hierarchy list in the Build Settings for OS X. Is this right, or should Build be under CopyFiles?

Thank you!
Ryan

sourceDB looks for a folder named “Resources” in the same folder as your application. That folder won’t automatically exist. If you want to do it this way, create that folder beside your app, place your “hangDEMOdb2.sqlite” file in that folder, and this code should run as you expect.

Thanks Roger. I see when a project is built for WIndows, a Resources folder is created, so that makes better sense as to why it is working on Win. But with Mac, it produces just the app.tar file. What is the preferred method of copying over a file on a Mac?

I’m starting to use App Wrapper to create the installers for my Mac apps, and you cannot just include specific files in the .pkg file. I’ll need to create my empty database files and copy over this pre-filled file at first run if the files are not already present. Any thoughts on best practice for this?

After you build your application, (this is on a Mac machine) right-click on the app and select “Show Package Contents”. You will see a folder named Resources. Copy your database file to that folder.
This is the way I call my Help Folder which I place in that location for distribution

#if DebugBuild dim f As FolderItem = GetFolderItem("QMP_Help").Child(loc) #else dim f As FolderItem = app.ExecutableFile.Parent.Parent.Child("Resources").Child("QMP_Help").Child(loc) #endif
If I’m running the app in the IDE, this code looks for my help folder beside the app. If the app has been built, this code looks for the help folder in the Resources folder of the App bundle.
If you drug just your DB file to the Resources folder, your code would be…
app.ExecutableFile.Parent.Parent.Child("Resources").Child("hangDEMOdb2.sqlite").

Thanks so much Roger! That was just what I needed. To make this copy correctly to both Mac and Windows, I altered the first part of the code:

#If TargetMachO sourceDB = app.ExecutableFile.Parent.Parent.Child("Resources").Child("hangDEMOdb2.sqlite") #elseif TargetWin32 sourceDB = GetFolderItem("Resources").Child("hangDEMOdb2.sqlite") #endif

Works like a charm now! Thanks again!

This is correct, it should be below the “Build” process, otherwise the application bundle hasn’t been built yet.

I would suggest downloading Tim’s TPSF module from this page.
https://forum.xojo.com/15318-modules-for-you-updated-9-28
His module includes the correct code for getting the Resources folder from the application.