PDA

View Full Version : Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/MacOS")



will49
3rd October 2007, 18:23
Is there an elegant/easy way to get the application's base path on Mac OS, without the standard "MyApp.app/Contents/MacOS"? If possible I don't really want to make a specific condition for Mac OS.

I tried using QCoreApplication::applicationDirPath(), but it always includes this.

So I was thinking to try and remove it by making a QDir and using cdUp() 3 times.

Or just search for the 3rd '/' from the right and take the left string from there.

marcel
3rd October 2007, 18:52
What about QCoreApplication::applicationFilePath? This does not enter the bundle on Mac, and all you have to do is strip the file name on all platforms.

If this doesn't work for you, then you can use applicationDirPath and create a globally accessible function in your application,where you do platform specific things, such as 3 cdUp's on Mac.

will49
3rd October 2007, 19:01
QCoreApplication::applicationFilePath still enters the bundle. The only difference is there is

..../MyApp.app/Contents/MacOS/MyApp
with applicationFilePath
and

..../MyApp.app/Contents/MacOS/
with applicationDirPath

marcel
3rd October 2007, 19:08
Then you're stuck with 3 cdUp's, but you can still make a general function that does that on Mac and nothing on Win and Linux.

will49
3rd October 2007, 19:10
OK thanks. No big deal - just wanted to see if there was an elegant way of doing it.

igor
4th October 2007, 21:58
QCoreApplication::applicationFilePath still enters the bundle. The only difference is there is

..../MyApp.app/Contents/MacOS/MyApp
with applicationFilePath
and

..../MyApp.app/Contents/MacOS/
with applicationDirPath

Just out of curiosity, and because I do not have a Windows development environment handy, what are the returns on a Windows box ?

And while I'm asking, on a Linux box ?

marcel
4th October 2007, 22:04
They are the same on windows and linux: applicationDirPath returns the application directory, e.g. /usr/bin or c:\appdir
applicationFilePath returns the absolute file path of the application, e.g. c:\appdir\app.exe or /usr/bin/app

jpn
5th October 2007, 10:05
The Trolls theirselves are creating a QDir and using cdUp() 3 times in the Plug & Paint example (http://doc.trolltech.com/4.3/tools-plugandpaint.html), more specifically in MainWindow::loadPlugins(), too..

yogeshgokul
5th August 2008, 12:09
#ifdef Q_WS_MAC
#include "CoreFoundation/CoreFoundation.h"
#endif



#ifdef Q_WS_MAC
CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef,kCFURLPOSIXPathS tyle);
const char *pathPtr = CFStringGetCStringPtr(macPath,CFStringGetSystemEnc oding());
CFRelease(pluginRef);
CFRelease(macPath);
QString filePath = QString(pathPtr);
QString dirPath = filePath.left(filePath.lastIndexOf("/"));
return dirPath;

#else
return qApp->applicationDirPath() + "/";
#endif