I have packaged a macOS binary of an application which loads resources in a folder just outside of the .app bundle. The directory structure looks like
Foo/
Foo.app/
Contents/
...
resources/
I would like to change the working directory of the application to the parent directory Foo/
, and this is what I have come up with so far.
#if defined(APPLE)
#include "CoreFoundation/CoreFoundation.h"
#include <unistd.h>
#include <libgen.h>
#endif
int main() {
#if defined(APPLE)
// macOS workaround for setting the working directory to the location of the .app
{
CFBundleRef bundle = CFBundleGetMainBundle();
CFURLRef bundleURL = CFBundleCopyBundleURL(bundle);
char path[PATH_MAX];
Boolean success = CFURLGetFileSystemRepresentation(bundleURL, TRUE, (UInt8 *)path, PATH_MAX);
assert(success);
CFRelease(bundleURL);
chdir(dirname(path));
}
#endif
...
}
This appears to work on macOS 10.7 and 10.10 (which is all I have to test on), but on 10.12, users have reported that resources are not loading, so I suspect that the above hack no longer works correctly.
Is there a better way of changing to the directory of the .app bundle that is being run?