So it appears I was still being lazy. After some off-list tests, we found Yvan's error was caused by trying to get the contents of a folder he didn't have permission for. This version will check for that, and add it to the output accordingly:
use scripting additions
use framework "Foundation"
-- this where we'll store the stuff as we collect it; we could use a list, but it could get very long
set finalNSArray to current application's NSMutableArray's array()
-- make NSURL of where to start
set thePath to POSIX path of (choose folder)
set anNSURL to current application's |NSURL|'s fileURLWithPath:thePath
-- call our handler
my listURL:anNSURL inArray:finalNSArray
-- join the strings in the array, and convert it to an AS string
return (finalNSArray's componentsJoinedByString:linefeed) as text
on listURL:anNSURL inArray:finalNSArray
-- get file manager
set theNSFileManager to current application's NSFileManager's defaultManager()
-- define options to use
set enumerationOptions to current application's NSDirectoryEnumerationSkipsHiddenFiles
-- get list of items in directory
set {theItems, theError} to (theNSFileManager's contentsOfDirectoryAtURL:anNSURL includingPropertiesForKeys:{(current application's NSURLIsPackageKey), (current application's NSURLIsDirectoryKey)} options:enumerationOptions |error|:(reference))
-- check we were allowed access to the folder
if theItems is missing value then
-- probably a permissions problem, so record it and return
(finalNSArray's addObject:(current application's NSString's stringWithFormat_("Folder '%@' cannot be accessed. Error: %@", anNSURL's |path|(), theError's localizedDescription())))
return
else
set theItems to theItems as list
end if
-- create lists to store results; it might be quicker to use arrays, but let's keep it simple
set theFiles to {}
set theFolders to {}
-- loop through the items
repeat with i from 1 to count of theItems
set anItem to item i of theItems
-- is it a directory?
set {theResult, theValue, theError} to (anItem's getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(reference))
if theValue as boolean then
-- is it a package?
set {theResult, theValue, theError} to (anItem's getResourceValue:(reference) forKey:(current application's NSURLIsPackageKey) |error|:(reference))
if not theValue as boolean then
set end of theFolders to anItem
else -- it's a file, so store name
set end of theFiles to anItem's |path|()'s lastPathComponent()
end if
else -- it's a file, so store name
set end of theFiles to anItem's |path|()'s lastPathComponent()
end if
end repeat
-- add folder line
(finalNSArray's addObject:(current application's NSString's stringWithFormat_("Folder '%@' contains %@ files and %@ folders:", anNSURL's |path|(), count of theFiles, count of theFolders)))
-- add list of filenames
(finalNSArray's addObjectsFromArray:theFiles)
-- now do the same thing with each of the folders
repeat with i from 1 to count of theFolders
(my listURL:((item i of theFolders)'s URLByResolvingSymlinksInPath()) inArray:finalNSArray)
end repeat
end listURL:inArray: