Recent

Author Topic: [SOLVED][MACOS] Loading file with relative path problem  (Read 694 times)

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
[SOLVED][MACOS] Loading file with relative path problem
« on: August 13, 2020, 10:54:24 pm »
Hi to all i have a little problem, and can say how to solve it

I have several applications that I compile under different OS (Windows, Linux and Mac)
In order not to duplicate my resource files, I have a folder structure like below

-Root
   - Demos
      - Resources
        - images
        - sounds
      - App1
         - Bin
            - Windows
               - compileMode
                  - cpu/App1.exe
            - Linux
               - compileMode
                  - cpu/App1
            - MacOS
               - compileMode
                  - cpu/App1.app
      - App2
         - Bin
            - Windows
               - compileMode
                  - cpu/App2.exe
            - Linux
               - compileMode
                  - cpu/App2
            - MacOS
               - compileMode
                  - cpu/App2.app
       - etc...

In my code i have

Code: Pascal  [Select][+][-]
  1. FTextureBuffer.LoadFromFile('../../../../../media/images/texturemaphq.tga');

It work under Windows and Linux but not on MacOS
Note i say the folder of MacOS app is in "Content/MacOS"

My problem is how to accessing to the Resources folder from the current folder of the executable ?

I tried by adding "../" twice but it's not work.

The problem is that the folder is in the back. If it will be a subfolder I wouldn't have any problems

Thanks in advance
« Last Edit: August 14, 2020, 12:19:14 pm by BeanzMaster »

jwdietrich

  • Hero Member
  • *****
  • Posts: 1139
    • formatio reticularis
Re: [MACOS] Loading file with relative path problem
« Reply #1 on: August 13, 2020, 11:06:01 pm »
You might want to check for the working directory with GetCurrentDir.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.10 | FPC 3.2.0 | PPC, Intel, ARM | macOS, Windows, Linux

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: [MACOS] Loading file with relative path problem
« Reply #2 on: August 13, 2020, 11:30:44 pm »
You might want to check for the working directory with GetCurrentDir.

Get the current dir don't solve the problem. Like i said resources folder is not a subfolder. Under Linux and Windows we get directly the right folder but not under MacOS. I think i'll must do a special function to get the absolute path.

winni

  • Hero Member
  • *****
  • Posts: 1899
Re: [MACOS] Loading file with relative path problem
« Reply #3 on: August 14, 2020, 12:00:18 am »
Hi!

start with

fname := application.exename;

Then start counting from the end the number of slashes plus one.
Then you got the offset were to delete the right part of fname.

Now you got the absolute path.
That should solve it.

On a normal system.
I don't know nothing about the Mac.

Winni

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1003
  • Former Delphi 1-7, 10.2 User
Re: [MACOS] Loading file with relative path problem
« Reply #4 on: August 14, 2020, 03:09:41 am »
If the issue is locating the macOS application bundle's resources directory, then see the Wiki Locating the macOS application resources directory.

(It might have been better to post in the Forum » Programming » Operating Systems » macOS / Mac OS X forum as I nearly missed this post.)
o Lazarus v2.1.0 r63871, FPC v3.3.1 r46876, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware Fusion VM)
o FPC 3.0.4, FreeBSD 12.2-STABLE r365646 amd64
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 20.04 (Parallels VM)

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: [MACOS] Loading file with relative path problem
« Reply #5 on: August 14, 2020, 10:18:07 am »
Hi
If the issue is locating the macOS application bundle's resources directory, then see the Wiki Locating the macOS application resources directory.

I say that and
 is  exactly what i won't do because i have more than 20 applications demos and also because i don't provide any executable only source code

(It might have been better to post in the Forum » Programming » Operating Systems » macOS / Mac OS X forum as I nearly missed this post.)

No problem i don't take care. It will be for the next problem ;)

start with

fname := application.exename;

Then start counting from the end the number of slashes plus one.
Then you got the offset were to delete the right part of fname.

Now you got the absolute path.
That should solve it.


That's exactly what i was thinking  :)

Cheers

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: [MACOS] Loading file with relative path problem
« Reply #6 on: August 14, 2020, 12:18:51 pm »
If someone need something similar i'm doing like this

Code: Pascal  [Select][+][-]
  1. function GetApplicationPath : string;
  2. begin
  3.   {$IFDEF DARWIN}
  4.   result := Copy(Application.ExeName, 1, Pos(ApplicationName + '.app', Application.ExeName) - 1);
  5.   {$ELSE}
  6.   Result :=  Application.Location;
  7.   {$ENDIF}
  8.   Result :=  IncludeTrailingPathDelimiter(Result);
  9. end;
  10.  
  11. function GetAbsolutePathFromApp(SearchPath : String) : String;
  12. Var
  13.   RelTag : String;
  14.   AppPath : String;
  15.   TmpRelPath : String;
  16.   cnt,i : Integer;
  17. begin
  18.   RelTag := '..' + pathdelim;
  19.   AppPath := GetApplicationPath;
  20.   TmpRelPath := FixPathDelimiter(SearchPath);
  21.   if (Pos(RelTag, TmpRelPath) > 0) then
  22.   begin
  23.     cnt := 0;
  24.     while (Pos(RelTag, TmpRelPath) > 0) do
  25.     begin
  26.       TmpRelPath := Copy(TmpRelPath,4,Length(TmpRelPath));
  27.       inc(cnt);
  28.     end;
  29.  
  30.     if cnt > 0 then
  31.     begin
  32.       for i:= 1 to cnt do
  33.       begin
  34.         AppPath := ExpandFileName(IncludeTrailingPathDelimiter(AppPath) + '..');
  35.       end;
  36.     end;
  37.  
  38.     Result := IncludeTrailingPathDelimiter(IncludeTrailingPathDelimiter(AppPath) + TmpRelPath);
  39.   end
  40.   else
  41.   begin
  42.     Result := IncludeTrailingPathDelimiter(IncludeTrailingPathDelimiter(AppPath) + SearchPath);
  43.   end;
  44. end;  

 

TinyPortal © 2005-2018