Recent

Author Topic: [SOLVED] Read file inside app bundle MacOs  (Read 2125 times)

madref

  • Hero Member
  • *****
  • Posts: 1102
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[SOLVED] Read file inside app bundle MacOs
« on: May 12, 2024, 12:57:55 am »
This peace of code did work perfectly on OS Monterey.
Code: Pascal  [Select][+][-]
  1.   ProgramDir := LeftStr (ExtractFilePath(Application.ExeName), Length(ExtractFilePath(Application.ExeName))-15);
  2.   if ProgramDir[Length(ProgramDir)] <> '/' then ProgramDir := ProgramDir + '/';
  3.   ResourceDir := ProgramDir + 'Contents/Resources/';
  4.   RapportDir := ResourceDir + 'Reports/';
  5.   UserDir := GetUserDir + 'Documents/Referee Database 5/';
  6.   LogoDir := ResourceDir + 'Logos/';
  7.   // Waar staat het bestand met de opties.
  8.   cfgFile := ResourceDir + 'Referee5_DB.xml' ;
  9.   if FileExists(cfgFile) then
  10.     begin
  11.       XMLConfig_DB.Filename := cfgFile ;
  12.       // Check version
  13.       Code := AnsiString(XMLConfig_DB.GetValue ('/DATABASE/Version/Value', ''));
  14.       k := StrToInt(Code);
  15.       v := StrToInt(Copy(Prg_Versie,1,1));
  16.       if k <> v then begin
  17.         KleurPop := Rood;
  18.         Form_Melding.Melding(mUitroep1, bOk, 'Options File is for different version. Please go to the Option Form and set all your options!!', PrgNaam);
  19.       end;  // if
  20.       RefereeDBF := AnsiString(XMLConfig_DB.GetValue ('/DATABASE/Current/Value', ''));
  21.       k := XMLConfig_DB.GetValue ('/SETUP/Color/Value', 1);
  22.       case k of
  23.         0 : KleurPop := Rood;
  24.         1 : KleurPop := Oranje;
  25.         2 : KleurPop := Blauw;
  26.         3 : KleurPop := Groen;
  27.         4 : KleurPop := Paars;
  28.         5 : KleurPop := Geel;
  29.         6 : KleurPop := Zwart;
  30.       end;  // case k
  31.       Shirt_Style := XMLConfig_DB.GetValue ('/SETUP/Style/Value', 1);
  32.       Cipher:= TDCP_rc4.Create(Self);
  33.       Cipher.InitStr(cfgFile,TDCP_sha256);         // initialize the cipher with a hash of the passphrase
  34.       Code := AnsiString(XMLConfig_DB.GetValue ('/SETUP/Base_MS/Value', ''));
  35.       smtpHost := MyDecryption(Code);
  36.       Code := AnsiString(XMLConfig_DB.GetValue ('/SETUP/Base_MP/Value', ''));
  37.       smtpPort := MyDecryption(Code);
  38.       Code := AnsiString(XMLConfig_DB.GetValue ('/SETUP/Base_UN/Value', ''));
  39.       smtpUser := MyDecryption(Code);
  40.       Code := AnsiString(XMLConfig_DB.GetValue ('/SETUP/Base_UP/Value', ''));
  41.       smtpPassword := MyDecryption(Code);
  42.       Code := AnsiString(XMLConfig_DB.GetValue ('/SETUP/Base_NM/Value', ''));
  43.       SenderName := MyDecryption(Code);
  44.       Code := AnsiString(XMLConfig_DB.GetValue ('/SETUP/Base_NE/Value', ''));
  45.       SenderEMail := MyDecryption(Code);
  46.       Code := AnsiString(XMLConfig_DB.GetValue ('/SETUP/Base_TI/Value', ''));
  47.       SenderTitel := MyDecryption(Code);
  48.       Code := AnsiString(XMLConfig_DB.GetValue ('/SETUP/Base_RP/Value', ''));
  49.       SenderRapportEmail := MyDecryption(Code);
  50.       Cipher.Burn;
  51.       Cipher.Free;
  52.     end
  53.   else begin
  54.     KleurPop := Rood;  // Standaard keuze
  55.     smtpHost := '';
  56.     smtpPort := '';
  57.     smtpUser := '';
  58.     smtpPassword := '';
  59.     SenderName := '';
  60.     SenderEMail := '';
  61.     SenderTitel := '';
  62.     SenderRapportEmail := '';
  63.     // Maak een XML-file aan.
  64.  
  65.  
  66.     AssignFile(F,cfgFile);
  67.     try
  68.       Rewrite(F);
  69.       Writeln(F,'<?xml version="1.0" encoding="utf-8"?>');
  70.       Writeln(F,'<REFEREE_5>');
  71.       Writeln(F,'  <DATABASE>');
  72.       Writeln(F,'  </DATABASE>');
  73.       Writeln(F,'  <SETUP>');
  74.       Writeln(F,'    <Color Value="0"/>');
  75.       Writeln(F,'  </SETUP>');
  76.       Writeln(F,'</REFEREE_5>');
  77.     finally
  78.       CloseFile(F);
  79.     end;  // try
  80.   end;  // if
  81.  


But after upgrading to OS Sonoma this is a total disaster.


How can I read a file inside my app bundle in OS Sonoma
« Last Edit: May 12, 2024, 06:41:58 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7.4
Lazarus 4.99 (rev main_4_99-1378-ga4855f6fa5) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

jamie

  • Hero Member
  • *****
  • Posts: 6880
Re: Read file inside app bundle MacOs
« Reply #1 on: May 12, 2024, 02:19:51 am »
your code can be clean up a little to make it easier to debug.

The first two lines you have can be replaced with the

ExtractFilePath(Application......);

That will remove everything to the right up to the last "/" and will include the "/" too.

The only true wisdom is knowing you know nothing

madref

  • Hero Member
  • *****
  • Posts: 1102
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Read file inside app bundle MacOs
« Reply #2 on: May 12, 2024, 02:33:45 am »
yes... but that still leaves the problem
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7.4
Lazarus 4.99 (rev main_4_99-1378-ga4855f6fa5) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

dbannon

  • Hero Member
  • *****
  • Posts: 3326
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Read file inside app bundle MacOs
« Reply #3 on: May 12, 2024, 02:41:34 am »
Without seeing the bundle or the errors generated, not a lot anyone can do here I suspect.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jamie

  • Hero Member
  • *****
  • Posts: 6880
Re: Read file inside app bundle MacOs
« Reply #4 on: May 12, 2024, 02:56:04 am »
yes... but that still leaves the problem

Problem being what? I noticed you are subtracting 15 chars from the end. What I suggested will remove that.

Are you sure the path length hasn't changed and your -15 is wrong? which is why I suggested cleaning up that code and let the function take care of that part.
The only true wisdom is knowing you know nothing

madref

  • Hero Member
  • *****
  • Posts: 1102
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Read file inside app bundle MacOs
« Reply #5 on: May 12, 2024, 03:03:06 am »
If you had read it properly... I said that in OS Monterey that DID work. But under OS Sonoma is DOES NOT work anymore


The error I get is in the attachment and the bundle also.


All I want to do is access the file Referee_DB5.xml as read and when the program exits as a write because this is where I store some information.
« Last Edit: May 12, 2024, 03:05:29 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7.4
Lazarus 4.99 (rev main_4_99-1378-ga4855f6fa5) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

jamie

  • Hero Member
  • *****
  • Posts: 6880
Re: Read file inside app bundle MacOs
« Reply #6 on: May 12, 2024, 03:13:30 am »
Yup, as I said. check your paths. Use the debugger.
The only true wisdom is knowing you know nothing

Fred vS

  • Hero Member
  • *****
  • Posts: 3538
    • StrumPract is the musicians best friend
Re: Read file inside app bundle MacOs
« Reply #7 on: May 12, 2024, 04:23:07 am »
This works here on Sonoma:

Code: Pascal  [Select][+][-]
  1. var
  2.  BinDir, ResourceDir: string;
  3. ...
  4. BinDir := IncludeTrailingBackslash(ExtractFilePath(ParamStr(0)));
  5. ResourceDir := copy(BinDir, 1, length(BinDir) -6) + 'Resources/'; // length(BinDir) -6 ---> 'MacOs/' ---> where the binary is located
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

madref

  • Hero Member
  • *****
  • Posts: 1102
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Read file inside app bundle MacOs
« Reply #8 on: May 12, 2024, 06:40:58 am »
Found the solution for MacOSx Sonoma.
Apparently you also have to include the extension of the application to it.


So the solution is:
Code: Pascal  [Select][+][-]
  1.   ProgramDir := Application.ExeName + '.app';
  2.   if ProgramDir[Length(ProgramDir)] <> '/' then ProgramDir := ProgramDir + '/';
to this is faster.
Code: Pascal  [Select][+][-]
  1.   ProgramDir := Application.ExeName + '.app/';


THIS IS TOTALLY WRONG !!! and TRon was partially right
« Last Edit: May 14, 2024, 11:29:04 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7.4
Lazarus 4.99 (rev main_4_99-1378-ga4855f6fa5) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

dbannon

  • Hero Member
  • *****
  • Posts: 3326
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Read file inside app bundle MacOs
« Reply #9 on: May 12, 2024, 06:43:28 am »
Yep, the application bundle is just a directory with an extension 'app'. So would need that in any version of MacOS.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

TRon

  • Hero Member
  • *****
  • Posts: 4337
Re: Read file inside app bundle MacOs
« Reply #10 on: May 12, 2024, 06:48:09 am »
So the solution is:
..
to this is faster.
..
And you really should be using something like IncludeTrailingPathDelimiter or at least DirectorySeparator even if it applies for your (single) OS only.


PS: a general remark: no wonder I am (mostly) unable to locate something mac specific as most of the threads that are specific to it seem to be located somewhere else than the dedicated macos subforum.... tuvok would say: logical  :)
« Last Edit: May 12, 2024, 07:13:28 am by TRon »
Today is tomorrow's yesterday.

dbannon

  • Hero Member
  • *****
  • Posts: 3326
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Read file inside app bundle MacOs
« Reply #11 on: May 12, 2024, 08:42:51 am »
.....
And you really should be using something like IncludeTrailingPathDelimiter or at least DirectorySeparator even if it applies for your (single) OS only.
Agreed !  I generally use :=AppendPathDelim(Dir), its shorter than the others mentioned and only appends if necessary.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jamie

  • Hero Member
  • *****
  • Posts: 6880
Re: [SOLVED] Read file inside app bundle MacOs
« Reply #12 on: May 12, 2024, 02:59:31 pm »
My understanding of the "ExtractFilePath" from the help indicates that it also includes the last "/" ?

Is the help wrong or did I misread it ?
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 4337
Re: [SOLVED] Read file inside app bundle MacOs
« Reply #13 on: May 12, 2024, 03:05:55 pm »
Is the help wrong or did I misread it ?
No you understood correctly... It is macos that is doing things out of the ordinary (for non macos users)

the appbundle directory is application.exename + ".app" (and the slash appended to actually indicate the directory).
Today is tomorrow's yesterday.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Read file inside app bundle MacOs
« Reply #14 on: May 12, 2024, 03:15:59 pm »
And you really should be using something like IncludeTrailingPathDelimiter or at least DirectorySeparator even if it applies for your (single) OS only.
Absolute, initial path by one of the named methods, following sub-levels separated by PathDelim
Quick updated to a PathDelim only variant.
Code: Pascal  [Select][+][-]
  1. BasePath := ParamStr(0) + '.app' + PathDelim;
  2. SomePath := BasePath + 'sublevel' + PathDelim + 'lastsub' + PathDelim;
« Last Edit: May 12, 2024, 03:21:00 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018