Correct using of relative path on MacOS

Mac OSX specific forum
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Correct using of relative path on MacOS

Post 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?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Correct using of relative path on MacOS

Post by Andre »

Really no one with further suggestions or a better way? :?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Airr
User
User
Posts: 49
Joined: Tue Oct 04, 2005 4:29 am
Contact:

Re: Correct using of relative path on MacOS

Post 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.
"Programming is like Poetry. Sometimes the words just escape you..." -me, to my manager.
Post Reply