list folder with enumeratorAtURL_includingPropertiesForKey_options_

I have tried to use the method: enumeratorAtURL_includingPropertiesForKeys_options_errorHandler_()
and I can’t get it working.
I tried to translate the objective C code in the documentation without success.

I tried also the method : contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error_ and that works fine, but I need a recursive search and the options parameters.

Is the enumerator method supported under ApplescriptObjc?
If so what am I doing wrong. If this method is usable than how to combine it with the method: getResourceValue_forKey_error_(&filename, current application’s NSURLNameKey, missing value)

wat is the way to replace the &filename with an ApplescriptObjc parameter.

I hope somebody has a solution or an explanation why it is not working.

Thanks

Hi,

try this


set moviesFolder to POSIX path of (path to movies folder)
tell current application
	set fileManager to it's NSFileManager's defaultManager
	set theURL to it's |NSURL|'s fileURLWithPath_(moviesFolder)
	set nameKey to it's NSURLNameKey
	set skipHiddenFiles to it's NSDirectoryEnumerationSkipsHiddenFiles
end tell

set directoryEnumerator to fileManager's enumeratorAtURL_includingPropertiesForKeys_options_errorHandler_(theURL, {nameKey}, skipHiddenFiles, missing value)
repeat
	set nextObject to directoryEnumerator's nextObject()
	if nextObject is missing value then exit repeat
	log nextObject's |path|() as text
end repeat


Hallo Stefan,

Thanks for the reply.

I tried it but I get an error : unrecognized function nextObject. (error -10000)

I looks like the result of fileManager’s enumeratorAtURL_includingPropertiesForKeys_options_errorHandler_() is not of class NSDirectoryEnumerator.
When I log the class of the result then I get : <NSAppleEventDescriptor: ‘bool’> which is a boolean value.

Ik hoop you can find the problem.

I tested the script on 10.6.8/Xcode 4.2 and it worked fine.

I changed the definition of NSFileManager and NSURL to the class “” syntax


set moviesFolder to POSIX path of (path to movies folder)
tell current application
	set fileManager to its class "NSFileManager"'s defaultManager
	set theURL to its class "NSURL"'s fileURLWithPath_(moviesFolder)
	set nameKey to its NSURLNameKey
	set skipHiddenFiles to its NSDirectoryEnumerationSkipsHiddenFiles
end tell

set directoryEnumerator to fileManager's enumeratorAtURL_includingPropertiesForKeys_options_errorHandler_(theURL, {nameKey}, skipHiddenFiles, missing value)
repeat
	set nextObject to directoryEnumerator's nextObject()
	if nextObject is missing value then exit repeat
	log nextObject's |path|() as text
end repeat


Hi,

Until now I have been testing on a MacBook Pro with MacOSX (Lion) 10.7.2 and Xcode 4.2/4.2.1.
I indeed tried it after your last suggestion, on MacOSX 10.6.8 and Xcode 4.0 and it worked very good.

I suppose this could be a bug/change in the environment.
If somebody knows about it, please report it back to us.

If I do not get any response, then I will report it to Apple.

Thanks Stefan for your fast reaction.

I tried Stefan’s code (both versions) in Xcode 4.2 under Lion (10.7.2) and neither worked. I get the same error message about the unrecognized function, which is not surprising, because when I log directoryEnumerator I get 0.

Ric

This confirms the problem. Thanks.
I will report it to Apple.

After doing some tests I figured out that enumeratorAtURL_includingPropertiesForKeys_options_errorHandler_() returns a boolean false instead of a NSDirectoryEnumerator object.

The “old” method enumeratorAtPath_() works in Lion.

Unfortunately I didn’t find anything in the release notes.

In general, with these type of functions that return something “by reference”, you just use the word reference in applescript. Unfortunately, in this case it doesn’t work – not sure why. It usually works as long as the function also has a normal return like a bool (often) or a string. So the way you use it is to get item 2 of the result or set a list as the variable to get the results. A couple of examples:

log item 2 of scanner's scanDouble_(reference) --where scanner is an instance of the NSScanner class

set {theString, theEnc} to current application's NSString's stringWithContentsOfFile_usedEncoding_error_("<path to a file>", reference, missing value)

set {yesNo, aRow, aColumn} to aMatrix's getRow_column_forPoint_(reference, reference, loc)

In the case we’re discussing here the call:

 set {theBool,theAnswer} to theURL's getResourceValue_forKey_error_(reference,current application's NSURLNameKey,missing value)

doesn’t work (I get an error saying unable to set argument 2 - the type o^@ is not supported). Fortunately, to get the name, you can just use theURL’s lastPathComponent().

Ric

The problem is that it’s typed to id*, not id. Just the potential for non-object results seems to upset ASObjC. The workaround in this case is to use resourceValuesForKeys_error_().

Hi Stefan,

Thank you for posting your solution regarding this post. Your code introduces a whole new dimension to appleOBJ-C programing. In apple script manpulating files and folders is quite interesting, but in Objective-c it is taken to a whole new level of complexity.

Perhaps shane Stanley has some time to write few chapters about what was addressed here but of course time permitting.

Regards,
t.

I have posted the problem to Apple and hope this problem will be solved under 10.7.xx and Xcode 4.2.x in the near future.

This functionality opens rich possibilities in listing files and folders under ASOC.

enumeratorAtURL_includingPropertiesForKeys_options_errorHandler_() error under OSX 10.7.xx

I have a message from Apple that they believe this issue has been addressed in OS X 10.8.
I did not yet test it, so if anybody does please report back.