Page 1 of 1

Call To NSFileManager

Posted: Sat Oct 03, 2015 9:30 am
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.

Re: Call To NSFileManager

Posted: Sat Oct 03, 2015 10:01 am
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

Re: Call To NSFileManager

Posted: Sat Oct 03, 2015 10:28 am
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.

Re: Call To NSFileManager

Posted: Sat Oct 03, 2015 3:16 pm
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.