Results 1 to 9 of 9

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

  1. #1
    Join Date
    Jul 2007
    Posts
    56
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/Mac

    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.

  3. #3
    Join Date
    Jul 2007
    Posts
    56
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/Mac

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/Mac

    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.

  5. #5
    Join Date
    Jul 2007
    Posts
    56
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/Mac

    OK thanks. No big deal - just wanted to see if there was an elegant way of doing it.

  6. #6
    Join Date
    Jan 2007
    Posts
    30
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    MacOS X

    Question Re: Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/Mac

    Quote Originally Posted by will49 View Post
    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 ?

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/Mac

    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

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/Mac

    The Trolls theirselves are creating a QDir and using cdUp() 3 times in the Plug & Paint example, more specifically in MainWindow::loadPlugins(), too..
    J-P Nurmi

  9. #9
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Easy way to get app's folder on a Mac (i.e. not including "MyApp.app/Contents/Mac

    Qt Code:
    1. #ifdef Q_WS_MAC
    2. #include "CoreFoundation/CoreFoundation.h"
    3. #endif
    4.  
    5.  
    6.  
    7. #ifdef Q_WS_MAC
    8. CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
    9. CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef,kCFURLPOSIXPathStyle);
    10. const char *pathPtr = CFStringGetCStringPtr(macPath,CFStringGetSystemEncoding());
    11. CFRelease(pluginRef);
    12. CFRelease(macPath);
    13. QString filePath = QString(pathPtr);
    14. QString dirPath = filePath.left(filePath.lastIndexOf("/"));
    15. return dirPath;
    16.  
    17. #else
    18. return qApp->applicationDirPath() + "/";
    19. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 5th August 2008 at 12:12. Reason: missing [code] tags

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.