How to set path to MacOS .app Resources folder

I recently took a suggestion for my MacOS version of my OF game, to build the file resources into the .app, which seems to be typical MacOS software practice, and is done in magic settings in xCode.

It worked fine at first, moving the files from OF’s Data into the .app, but requiring no code changes…

… but then I made minor unrelated code changes, added a few files, and tried to build a new version…

… and for mysterious xCode reasons, while the files were still packaged inside the .app, the program in the later version went looking for the files in an external Data folder which I now wasn’t shipping, and failed.

So I added some code to find and use the Resources folder inside the .app, and now it works. I thought I’d just share what I did, in case it helps other people in future trying to do this. I found a couple of other mentions of this problem here ( ofToDataPath() ) on this forum and near solutions here: How to get the path to my app? but that last one shows how to point to the path to the program itself. To get to the Resources folder, I needed to navigate up two levels and then down into Resources, like:

#ifdef TARGET_OSX
    sAppPath = ofFilePath::getAbsolutePath(ofFilePath::getCurrentExeDir());
    sAppPath = sAppPath.substr(0, sAppPath.find_last_of("/"));
    sAppPath = sAppPath.substr(0, sAppPath.find_last_of("/"));
    sAppPath += "/Resources/";
#endif

Which seems like it would be a good thing for there to be an OF method for, since this seems to be a mainstream place to put data files on MacOS. A MacOS dev said he thought there might be a more official/universal way to get the Resources folder.

Hi,
I just do the following in the main.cpp file. The important stuff is what’s in between the #ifdef


#include "ofMain.h"
#include "ofApp.h"

int main( ){
	ofSetupOpenGL(1024,768,OF_WINDOW);			// <-------- setup the GL context

	#ifdef TARGET_OSX
		string newRoot = "../Resources/data/";
		ofEnableDataPath();
		ofSetDataPathRoot(newRoot);
	#endif
	
	// this kicks off the running of my app
	// can be OF_WINDOW or OF_FULLSCREEN
	// pass in width and height too:
	ofRunApp(new ofApp());

}
1 Like

Oh that’s better! I looked in the docs but hadn’t noticed such methods!

Thanks Roy!

1 Like