AppleScriptObjC -- List Folder Handler
AppleScriptObjC -- List Folder Handler
- Subject: AppleScriptObjC -- List Folder Handler
- From: Christopher Stone <email@hidden>
- Date: Fri, 30 Nov 2018 17:46:33 -0600
Hey Folks,
I was needing to return file/folder names non-recursively from a target folder,
and I wanted to do it with AppleScriptObjC instead of the Finder, System
Events, or the List Folder command.
After a little looking and not finding I decided I'd just write a handler, and
I found a discussion
<https://forum.latenightsw.com/t/how-do-i-get-list-of-only-normal-files-in-folder/1378>
on the LNS Forum that was helpful toward that end.
Script Debugger 7 <https://latenightsw.com/> has a couple of useful clippings
too.
It seemed pointless to return only item names, so I added an option to return
Furls or Paths as well.
It's faster than System Events – waay faster if you use whose visible is true
in the SEV code.
I'm pretty happy with the result, but as usual if anyone knows a better way I'm
all ears. :)
Enjoy.
--
Best Regards,
Chris
----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/11/30 16:19
# dMod: 2018/11/30 16:52
# Appl: AppleScriptObjC
# Task: List a Given Folder as type Furl, type Path, or by Name.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @List, @Folder, @Furl, @Path, @Name
----------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
set targetDir to path to downloads folder
set itemFurlList to my listFolder:targetDir returningAs:"furl"
set itemPathList to my listFolder:targetDir returningAs:"path"
set itemNameList to my listFolder:targetDir returningAs:"name"
----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on listFolder:targetDir returningAs:returnType -- "furl", "path", "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
end if
end listFolder:returningAs:
----------------------------------------------------------------
_______________________________________________
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