Mac application bundle and ExtractFilePath(Application.ExeName) lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- FangQ · 2018-03-22 21:37 In a portable program, I use ExtractFilePath(Application.ExeName) to find the folder containing the executable path, and then write output in a subfolder. This works well for Linux and Windows, but failed on my Mac. The program is launched using the application bundle (progname.app). It turns out the ExtractFilePath(Application.ExeName) extract a temporary path that is super long. I am wondering if there is a workaround for scenarios like this, how can I find the path of the application where the progname.app is located? thanks ---------------------------------------------------------------------- mischi · 2018-03-22 21:42 How about using Apple's API within an ifdef darwin? ---------------------------------------------------------------------- FangQ · 2018-03-22 21:52 @mischi, I am not sure which specific Apple API you were referring to. any examples? thanks ---------------------------------------------------------------------- Phil · 2018-03-22 23:02 Quote from: FangQ on March 22, 2018, 10:37:55 pmIn a portable program, I use ExtractFilePath(Application.ExeName) to find the folder containing the executable path, and then write output in a subfolder. Your app should not store things in the app bundle. Bad idea. Period. You should create the folder in one of the locations that macOS provides, typically under ~/Library/Application Support. Create your folder there using your app's bundle ID as the folder name. https://developer.apple.com/library/content/documentation/FileManageme nt/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirec tories.html You may be doing other things that non-Mac developers often try to drag in from the Windows or Linux worlds. A good way to root out these bad practices is just to go through the exercise of sandboxing your app. You may be surprised at how ill-disciplined your app is. But don't worry, in many cases these problems are easy to fix once you're spent a few minutes reading Apple's docs and learning about programming best practices on macOS. Here's a Pascal-oriented article on sandboxing that will help you get started: https://macpgmr.github.io/ ---------------------------------------------------------------------- FangQ · 2018-03-23 03:31 Quote from: Phil on March 23, 2018, 12:02:29 amYour app should not store things in the app bundle. Bad idea. Period. in fact, I don't, and not intend to, store anything inside the app bundle folder. as I mentioned, I want my program to be portable (such as ready-to-run on an USB drive), meaning that people unzip, and run without needing installation. I want to store the output of the program "along side" with the app, something like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AppUnzipFolder/  |-- myapp   (binary)  |-- myapp.app/  |       |-Contents  |               |-MacOS  |               |- ...  |-- Output         |- output1 as you see, I do not put my output file inside myapp.app, but I just want to find out the folder containing the app, and create an Output folder. Quote from: Phil on March 23, 2018, 12:02:29 amYou should create the folder in one of the locations that macOS provides, typically under ~/Library/Application Support. Create your folder there using your app's bundle ID as the folder name. https://developer.apple.com/library/content/documentation/FileManageme nt/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirec tories.html I did some search earlier, and found that GetUserDir or GetAppConfigDir may be an alternative, but still, making my software portable and self-contained is a feature I would love to keep. Quote from: Phil on March 23, 2018, 12:02:29 amYou may be doing other things that non-Mac developers often try to drag in from the Windows or Linux worlds. A good way to root out these bad practices is just to go through the exercise of sandboxing your app. You may be surprised at how ill-disciplined your app is. But don't worry, in many cases these problems are easy to fix once you're spent a few minutes reading Apple's docs and learning about programming best practices on macOS. Here's a Pascal-oriented article on sandboxing that will help you get started: https://macpgmr.github.io/ again, I still don't think having an installation-free and portable package design (such as USB storage based) is an "ill-disciplined" idea. I consider it as a feature, and if there are mechanisms that I can use in lazarus to keep this feature, I will be happy. ---------------------------------------------------------------------- mischi · 2018-03-23 10:24 The first step is to move the binary into the bundle. i.e. into the directory Contents/macOS. Lazarus does not handle this correctly. Here is the link to Apple's Filesystem API: https://developer.apple.co m/library/content/documentation/FileManagement/Conceptual/FileSystemPr ogrammingGuide ---------------------------------------------------------------------- Trenatos · 2018-04-10 15:23 I've tried figuring out how to get Lazarus to move it into the proper bundle directory when building, but have not succeeded.   :/ As for paths, I usually use ifdef darwin / linux / windows depending on what I need, such as paths being slightly different, that way cross compile works fine. I store files in the bundle and it works just fine, afaik a bundle is just a directory handled specially by OSX/MacOS. I also store files using the get config functions, search the word "config" on this page and you'll see what I mean:  https://www.freepascal.org/docs-html/rtl/sysutils/index-5.html ---------------------------------------------------------------------- Phil · 2018-04-10 15:29 Quote from: Trenatos on April 10, 2018, 05:23:44 pmI've tried figuring out how to get Lazarus to move it into the proper bundle directory when building, but have not succeeded.   :/ Right, Lazarus can't do that - a defect by design, I guess. If you create a new project with LazXProj's File | New item, it will automatically run a little console app after compiling that updates the app bundle properly. https://macpgmr.github.io/MacXPlatform/LazXProj.html ---------------------------------------------------------------------- mischi · 2018-04-10 15:39 Quote from: Trenatos on April 10, 2018, 05:23:44 pmI store files in the bundle and it works just fine, afaik a bundle is just a directory handled specially by OSX/MacOS.correct. Quote from: Trenatos on April 10, 2018, 05:23:44 pmI also store files using the get config functions, search the word "config" on this page and you'll see what I mean:  https://www.freepascal.org/docs- html/rtl/sysutils/index-5.html I am not really sure, but i do not think that these return paths, which are in conformance with apple guide lines. Briefly, user specific preference files should be ~/Library/Preferences, system wide ones in /Library/Preferences. Resource files, which are not changed by the user, but might be changed from one version to another should go in the Resources folder in the bundle. Files, which might changed and should be kept as they are with the next version should go into ~/Library/Application Support/YourProgram/ or /Library/Application Support/YourProgram/. Log files into  ~/Library/Logs and /Library/Logs. I used a now outdated API for preferences some time ago: https://sourc eforge.net/p/heatwizard/code-0/HEAD/tree/trunk/UPreferenceData.pas MiSchi ---------------------------------------------------------------------- Phil · 2018-04-10 15:43 Quote from: mischi on April 10, 2018, 05:39:57 pmI am not really sure, but i do not think that these return paths, which are in conformance with apple guide lines. Briefly, user specific preference files should be ~/Library/Preferences, system wide ones in /Library/Preferences. Resource files, which are not changed by the user, but might be changed from one version to another should go in the Resources folder in the bundle. Files, which might changed and should be kept as they are with the next version should go into ~/Library/Application Support/YourProgram/ or /Library/Application Support/YourProgram/. Log files into  ~/Library/Logs and /Library/Logs. Correct. The config functions are for a generic Unix system. macOS is not a generic Unix system and should not be treated like one. It's like treating humans as chordates instead of as humans. ---------------------------------------------------------------------- Trenatos · 2018-04-10 15:48 Write once, compile maybe, and please don't trust functions to return proper cross platform data just because they compile  I like FreePascal but man some stuff drives me bonkers. Just for the record, this page https://www.freepascal.org/docs- html/rtl/sysutils/getappconfigfile.html doesn't mention anything about being Unix-targeted, in fact they only mention the word Windows, not Linux, Unix, Mac, OSX, etc. ---------------------------------------------------------------------- Phil · 2018-04-10 15:52 Quote from: Trenatos on April 10, 2018, 05:48:15 pmWrite once, compile maybe, and please don't trust functions to return proper cross platform data just because they compile  I like FreePascal but man some stuff drives me bonkers. LazXProj's units take care of a lot of these things for you in a cross-platform way - app config settings (preferences), version info, etc. The point is to allow non-Mac developers to create proper Mac apps without sweating the details, while also being useful on Windows and Linux. ---------------------------------------------------------------------- mischi · 2018-04-10 18:10 Quote from: Phil on April 10, 2018, 05:43:41 pmmacOS is not a generic Unix system and should not be treated like one. It's like treating humans as chordates instead of as humans. I had to lookup chordates. Gave me a big smile and made my day. ---------------------------------------------------------------------- jwdietrich · 2018-04-10 19:20 Quote from: Phil on April 10, 2018, 05:29:18 pm Right, Lazarus can't do that - a defect by design, I guess. I thought that this defect by design is intended because a binary outside the bundle is more easy to maintain. On the other hand, your console app demonstrates that other ways are possible (and sensible). ---------------------------------------------------------------------- Phil · 2018-04-10 19:32 Quote from: jwdietrich on April 10, 2018, 09:20:42 pmI thought that this defect by design is intended because a binary outside the bundle is more easy to maintain. I have no knowledge of the design decision. No other IDE on Mac does it that way. ---------------------------------------------------------------------- Ozz_Nixon · 2018-06-02 15:59 It's simple: ExtractFilePath(Paramstr(0)) That returns the folder where your progname binary is. e.g. /path to the progname.app/Contents/MacOS/ ---------------------------------------------------------------------- Gilles · 2018-06-20 05:01 I used to do that (I know, it is deprecated) : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      function f_GetAppDir (var L_dir: SInt32; var x_vrn: SInt16): t_OSErr;        var            L_psn: ProcessSerialNumber;            xx_cat: FSCatalogInfo;            xx_ref: FSRef;            L_oss: OSStatus;          xErr: OSErr;            L_err: t_OSErr;    begin        L_err := 1;                    { - }                        L_oss := GetProcessBundleLocation(L_psn, xx_ref);                if L_oss = noErr then        begin            xErr := FSGetCatalogInfo(xx_ref, kFSCatInfoVolume + kFSCatInfoParentDirID,                                                   @xx_cat,                                                   nil, nil, nil);                                    if xErr = noErr then            begin                L_dir := xx_cat.parentDirID;                x_vrn := xx_cat.volume;    { - }              L_err := k_noErr            end            else              L_err := xErr        end        else     { - }         f_GetAppDir = L_err    end;  DEPRECATED, but it works on Mac OS 64 bits ---------------------------------------------------------------------- RayoGlauco · 2018-06-20 07:07 Maybe I am saying something everybody knows, but there is a function called ProgramDirectory: http://lazarus- ccr.sourceforge.net/docs/lcl/fileutil/programdirectory.html