Call To NSFileManager

Mac OSX specific forum
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Call To NSFileManager

Post by grabiller »

Hi,

I need to translate this native Cocoa message [NSFileManager URLsForDirectory: NSApplicationSupportDirectory inDomains: NSUserDomainMask] to PB CocoaMessage.

This message should return the equivalent of AppData on Windows, but on Mac OSX (probably something like: "/Users/USERNAME/Library/Application Support/")

I've checked this thread: http://www.purebasic.fr/english/viewtop ... 98#p410298 but the message here is structured differently, and I don't get it and it is very frustrating :shock: .

Could someone help me on this and give a little explanation on how this message is structured ? (not the solution alone please)

Thanks in advance!

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Call To NSFileManager

Post by Shardik »

You have to take into account that the FileManager method URLsForDirectory:inDomains: returns an array of URLs. The first (and only) element in the resulting array has to be converted from an NSURL to an NSString, from an NSString into an UTF8 String and finally into a PB string:

Code: Select all

#NSApplicationSupportDirectory = 14
#NSUserDomainMask = 1

FileManager = CocoaMessage(0, 0, "NSFileManager defaultManager")
URLArray = CocoaMessage(0, FileManager,
  "URLsForDirectory:", #NSApplicationSupportDirectory, 
  "inDomains:", #NSUserDomainMask)

If URLArray
  Debug PeekS(CocoaMessage(0, CocoaMessage(0,
    CocoaMessage(0, URLArray, "objectAtIndex:", 0), "path"),
    "UTF8String"), -1, #PB_UTF8)
EndIf
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Call To NSFileManager

Post by grabiller »

Shardik wrote:You have to take into account that the FileManager method URLsForDirectory:inDomains: returns an array of URLs. The first (and only) element in the resulting array has to be converted from an NSURL to an NSString, from an NSString into an UTF8 String and finally into a PB string:

Code: Select all

#NSApplicationSupportDirectory = 14
#NSUserDomainMask = 1

FileManager = CocoaMessage(0, 0, "NSFileManager defaultManager")
URLArray = CocoaMessage(0, FileManager,
  "URLsForDirectory:", #NSApplicationSupportDirectory, 
  "inDomains:", #NSUserDomainMask)

If URLArray
  Debug PeekS(CocoaMessage(0, CocoaMessage(0,
    CocoaMessage(0, URLArray, "objectAtIndex:", 0), "path"),
    "UTF8String"), -1, #PB_UTF8)
EndIf
Thanks a lot Shardik, works perfectly.

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Call To NSFileManager

Post by wilbert »

Shardik wrote:You have to take into account that the FileManager method URLsForDirectory:inDomains: returns an array of URLs. The first (and only) element in the resulting array has to be converted from an NSURL to an NSString, from an NSString into an UTF8 String and finally into a PB string
As an alternative you can use NSSearchPathForDirectoriesInDomains which is a function that returns an array of NSString objects instead of NSURL objects.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply