Re: AppleScriptObjC -- List Folder Handler
Re: AppleScriptObjC -- List Folder Handler
- Subject: Re: AppleScriptObjC -- List Folder Handler
- From: Shane Stanley <email@hidden>
- Date: Sat, 01 Dec 2018 15:42:23 +1100
On 1 Dec 2018, at 2:44 pm, Christopher Stone <email@hidden>
wrote:
>
> Would you mind showing me how to get the actual name or point me to a
> relevant script?
Sure:
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. And of course a name can vary
depending on the Finder settings:
set targetDir to path to downloads folder
set firstList to my listFolder:targetDir returningAs:"displayed name"
tell application "Finder"
tell Finder preferences
set all name extensions showing to not all name extensions
showing
end tell
end tell
set secondList to my listFolder:targetDir returningAs:"displayed name"
firstList = secondList --> false
Also note that APFS formatted volumes won't return the values (in any of the
above cases) in any particular sorted order. You probably want to add these two
lines:
set sortDesc to current application's NSSortDescriptor's
sortDescriptorWithKey:"path" ascending:true
selector:"localizedStandardCompare:" -- Finder-style sorting
set theURLs to theURLs's sortedArrayUsingDescriptors:{sortDesc}
--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>
_______________________________________________
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