Page 1 of 1

Correct using of relative path on MacOS

Posted: Sun Jan 02, 2011 6:57 pm
by Andre
Because of the problems on MacOS to find files in other directories than the executable, differences between running from the IDE and as executable I'm using the following procedure:

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  CompilerIf #PB_Compiler_Debugger = 1   ; Debugger is on
    path$ = #PB_Compiler_FilePath
  CompilerElse   ; Debugger is off (executable)
    path$ = RemoveString(GetPathPart(ProgramFilename()),"YourApp.app/Contents/MacOS/")  ; change 'YourApp' into the program name --> an example for a full path: /Users/geoworld/GeoWorld2/Developing/PureBasic0.app/Contents/MacOS/
  CompilerEndIf
CompilerElse  ; Windows
  path$ = GetPathPart(ProgramFilename())
CompilerEndIf
; MessageRequester("Program Path =", path$)
Do you know any easier way to handle relative paths on MacOS?

For example I want to load some images, which are completely outside of the directory containg the PB code. Here for example a (Windows compatible) path from a PB code in the CodeArchiv:

Code: Select all

  LoadImage(0,"..\Gfx\PureBasicLogoNew.png")
How can I easily use such relative paths ("../xyz") with resources in "above" directories also with PB for MacOS?

Re: Correct using of relative path on MacOS

Posted: Tue Jan 25, 2011 11:28 pm
by Andre
Really no one with further suggestions or a better way? :?

Re: Correct using of relative path on MacOS

Posted: Fri Feb 04, 2011 6:13 am
by Airr
I'm not sure I fully understand what you're seeking, but I'll take a stab at it.

Assuming that you have the PureBasicLogoNew.png inside of your Resources folder, you could retrieve the path to the application bundle (in this case, PureBasic0.app) by using the following Procedure:

Code: Select all

ImportC ""
  CFURLCopyFileSystemPath (CFURLRef, CFURLPathStyle)
EndImport


Procedure.s getBundlePath()
  bundle.l = CFBundleGetMainBundle_()
  mainBundleURL = CFBundleCopyBundleURL_(bundle)
  cfStringRef = CFURLCopyFileSystemPath(mainBundleURL, 0)
  buffer = CFStringGetCStringPtr_(cfStringRef, 0)

  CFRelease_(mainBundleURL)
  CFRelease_(cfStringRef)

  path$ = PeekS(buffer)
  ProcedureReturn path$
EndProcedure


tmp$ = getBundlePath()
MessageRequester("Path To Application Bundle", tmp$))
Then you should be able to do:

Code: Select all

LoadImage(0,getBundlePath() + "/Contents/Resources/PureBasicLogoNew.png")
It's been a while since I've used PB, so I don't recall if you can inline the procedure like that, but give it a shot. If not, just assign the return value to a string and pass that, I think....

The Procedure should return the bundle path with and without the debugger running.


HTH!

AIR.

EDIT: Forgot to release mainBundleUrl and cfStringRef. Added above.