Re: AppleScriptObjC -- List Folder Handler
Re: AppleScriptObjC -- List Folder Handler
- Subject: Re: AppleScriptObjC -- List Folder Handler
- From: Nigel Garvey <email@hidden>
- Date: Sun, 02 Dec 2018 00:42:42 +0000
Shane Stanley wrote on Sat, 01 Dec 2018 15:42:23 +1100:
>on listFolder:targetDir returningAs:returnType -- "furl", "path",
"name",
>"displayed name"
> set NSDirectoryEnumerationSkipsHiddenFiles to a reference to 4
> set NSFileManager to a reference to current application's NSFileManager
> set {theURLs, theError} to NSFileManager's defaultManager()'s
>contentsOfDirectoryAtURL:targetDir includingPropertiesForKeys:{}
>options:(NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)
> if theURLs is equal to missing value then error (theError's
>localizedDescription() as text)
> if returnType = "furl" then
> return theURLs as list
> else if returnType = "path" then
> return (theURLs's valueForKey:"path") as list
> else if returnType = "name" then
> return (theURLs's valueForKey:"lastPathComponent") as list
> else if returnType = "displayed name" then
> set theNames to {}
> repeat with aURL in theURLs
> set {theResult, theName} to (aURL's
> getResourceValue:(reference)
>forKey:(current application's NSURLLocalizedNameKey) |error|:(missing
>value))
> set end of theNames to theName as text
> end repeat
> return theNames
> end if
>end listFolder:returningAs:
>
>As you can see, it's quite a bit slower.
Hi.
It can be speeded up considerably for large folders by fetching the keys
with the URLs, and a bit more by building theNames as an NSMutableArray:
on listFolder:targetDir returningAs:returnType -- "furl", "path", "name",
"displayed name"
set NSDirectoryEnumerationSkipsHiddenFiles to a reference to 4
set NSFileManager to a reference to current application's NSFileManager
set {theURLs, theError} to NSFileManager's defaultManager()'s
contentsOfDirectoryAtURL:targetDir includingPropertiesForKeys:{current
application's NSURLLocalizedNameKey}
options:(NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)
if theURLs is equal to missing value then error (theError's
localizedDescription() as text)
if returnType = "furl" then
return theURLs as list
else if returnType = "path" then
return (theURLs's valueForKey:"path") as list
else if returnType = "name" then
return (theURLs's valueForKey:"lastPathComponent") as list
else if returnType = "displayed name" then
set theNames to current application's NSMutableArray's new()
repeat with aURL in theURLs
set {theResult, theName} to (aURL's getResourceValue:(reference)
forKey:(current application's NSURLLocalizedNameKey) |error|:(missing value))
(theNames's addObject:(theName))
end repeat
return theNames as list
end if
end listFolder:returningAs:
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden