LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.
Moderators: LCMark, LCfraser
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Sun Sep 03, 2023 7:39 am
Hello,
Trying to use
fileExistsAtPath:isDirectory:, I saw in the Apple documentaion that
isDirectory parameter should not be a boolean but a
pointer to a Boolean. I can't figure how to pass a
pointer to a function definition like:
Code: Select all
private foreign handler ObjC_NSFolderExistsAtPath(in pFileManager as ObjcId, in pFilename as ObjcId, in pBoolean as CBool) \
returns CBool binds to "objc:NSFileManager.-fileExistsAtPath:isDirectory:"
to be called like:
Code: Select all
put ObjC_NSFolderExistsAtPath(tFileManager, StringToNSString(pFilename), pBoolean) into tResultExists
https://developer.apple.com/documentati ... guage=objc
Thank you for you help.
Last edited by
Zax on Tue Sep 05, 2023 8:06 am, edited 1 time in total.
-
LCMark
- Livecode Staff Member

- Posts: 1232
- Joined: Thu Apr 11, 2013 11:27 am
Post
by LCMark » Sun Sep 03, 2023 10:18 am
@Zax: So in this case, the second parameter's pointer indicates that it is a 'out' parameter - i.e. the method writes a value into it on return. Thus you want to bind to it as 'out' rather than 'in':
Code: Select all
private foreign handler ObjC_NSFolderExistsAtPath(in pFileManager as ObjcId, in pFilename as ObjcId, out rIsDirectory as CBool) \
returns CBool binds to "objc:NSFileManager.-fileExistsAtPath:isDirectory:"
The method returns true if an item exists at pFilename, and then returns true in rIsDirectory if it is a folder, and false it is a (normal) file.
Note: Not all parameters which take a pointer are 'out' parameters - the Obj-C docs tend to use the phrase `Upon return` to indicate parameters which return a value into them, rather than expecting one to be passed.
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Sun Sep 03, 2023 11:59 am
Thank you LCMark for your quick answer, I no longer have errors with your script.
But my function doesn't seem to make difference between files and folders:
Code: Select all
put FileOrFolderExists(myFilePath, true)
returns true if a
file or a folder exists, and false if not
Code: Select all
put FileOrFolderExists(myFilePath, false)
returns the same result
Full code:
Code: Select all
use com.livecode.foreign
use com.livecode.objc
private foreign handler ObjC_NSDefaultFileManager() returns ObjcId binds to "objc:NSFileManager.+defaultManager"
private foreign handler ObjC_NSFolderExistsAtPath(in pFileManager as ObjcId, in pFilename as ObjcId, out rIsDirectory as CBool) \
returns CBool binds to "objc:NSFileManager.-fileExistsAtPath:isDirectory:"
public handler FileOrFolderExists(in pFilename as String, in pCheckOnlyFolders as Boolean) returns any
variable tFileManager as ObjcObject
variable resultExists as any
unsafe
put ObjC_NSDefaultFileManager() into tFileManager
put ObjC_NSFolderExistsAtPath(tFileManager, StringToNSString(pFilename), pCheckOnlyFolders) into resultExists
end unsafe
return resultExists
end handler
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Tue Sep 05, 2023 8:07 am
OK, I finally understand!
To make my code easier to understand, I wrote two functions:
Code: Select all
private foreign handler ObjC_NSFileOrFolderExistsAtPath(in pFileManager as ObjcId, in pFilename as ObjcId, out rIsDirectory as CBool) \
returns CBool binds to "objc:NSFileManager.-fileExistsAtPath:isDirectory:"
public handler FileExists(in pFilename as String) returns any
variable tFileManager as ObjcObject
variable rIsDirectory as Boolean
variable resultExists as any
unsafe
put ObjC_NSDefaultFileManager() into tFileManager
put ObjC_NSFileOrFolderExistsAtPath(tFileManager, StringToNSString(pFilename), rIsDirectory) into resultExists
end unsafe
if resultExists then
// ----------- file or folder exists
if rIsDirectory then
return false // is dir
else
return true // file
end if
else
// ----------- file or folder NOT exists
return false
end if
end handler
public handler FolderExists(in pFoldername as String) returns any
variable tFileManager as ObjcObject
variable rIsDirectory as Boolean
variable resultExists as any
unsafe
put ObjC_NSDefaultFileManager() into tFileManager
put ObjC_NSFileOrFolderExistsAtPath(tFileManager, StringToNSString(pFoldername), rIsDirectory) into resultExists
end unsafe
if resultExists then
// ----------- file or folder exists
if rIsDirectory then
return true // is dir
else
return false // file
end if
else
// ----------- file or folder NOT exists
return false
end if
end handler