Re: AppleScriptObjC -- List Folder Handler
Re: AppleScriptObjC -- List Folder Handler
- Subject: Re: AppleScriptObjC -- List Folder Handler
- From: Christopher Stone <email@hidden>
- Date: Sun, 02 Dec 2018 19:35:43 -0600
On 11/30/2018, at 22:42, Shane Stanley <email@hidden
<mailto:email@hidden>> wrote:
> As you can see, it's quite a bit slower. And of course a name can vary
> depending on the Finder settings:
Hey Shane,
Well isn't that just annoying as hell. (The above and the bit about “/”
showing as “:”).
You say also that the OS can produce a localized name instead of the verbatim
FS Name.
What might be an example of same?
In any case – the fact that it's hard to get the absolute file system name of a
file/folder without pitfalls is pretty ridiculous.
I've added your display name function to my handler, as Nigel's is only a
trifle faster on my 1K item test folder. (I'll digest the NSMutableArray
method later though. Thanks Nige!)
I've also produced my own fsName method by massaging the lastPathComponent to
transform any errant colon characters into the proper forward slash.
This mess makes me want to go back to the Finder or list folder. Neither of
them does anything weird to file/folder names.
Or the Satimage.osax of course (which also converts “/” to “:” but makes it
super easy to fix).
tell application "Finder" to set targetDir to insertion location as alias
set nameList to list files targetDir with names only without recursively
set nameList to change ":" into "/" in nameList -- works within an AppleScript
list object
It's going to kill me to give that thing up after 15+ years.
Ah, well. Once more into the breach dear friends...
--
Take Care,
Chris
----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/11/30 16:19
# dMod: 2018/12/02 17:48
# Appl: AppleScriptObjC
# Task: List a Given Folder by Display Name, File System Name, Furl, or Path.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @List, @Folder, @Display_Name, @Name,
@FSName, @Furl, @Path
----------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
tell application "Finder" to set targetDir to insertion location as alias
set itemFurlList to my listFolder:targetDir returningAs:"fsName"
----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on listFolder:targetDir returningAs:returnType -- "displayName", "fsName",
"furl", "path",
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)
# Ensure ascending Finder-like sort even on APFS systems.
set sortDesc to current application's NSSortDescriptor's
sortDescriptorWithKey:"path" ascending:true
selector:"localizedStandardCompare:" -- Finder-style sorting
set theURLs to theURLs's sortedArrayUsingDescriptors:{sortDesc}
# NOTE - dependent upon the Finder's "Show all filename extensions" setting.
if returnType = "displayName" 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
else if returnType = "fsName" then
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's
text item delimiters, linefeed}
set itemList to ((theURLs's valueForKey:"lastPathComponent") as list)
as text
set theString to current application's NSString's
stringWithString:itemList
set stringLength to theString's |length|()
# Work around possible conversion of "/" to ":" in file name.
set theString to theString's stringByReplacingOccurrencesOfString:":"
withString:"/" options:(current application's NSRegularExpressionSearch)
range:{location:0, |length|:stringLength}
set theString to theString as text
set theString to text items of theString
set AppleScript's text item delimiters to oldTIDS
return theString
else if returnType = "furl" then
return theURLs as list
else if returnType = "path" then
return (theURLs's valueForKey:"path") as list
end if
end listFolder:returningAs:
----------------------------------------------------------------
(*
Revision Notes:
2018/12/02
+ Fixed type "name" to return the abolute file system name.
* Works around problems with "/" characters.
+ Added "displayName" option.
+ Changed "name" option to fsName for File System Name.
*)
----------------------------------------------------------------
_______________________________________________
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