Edit:
To add context – I’m building a script to recreate an empty folder structure on a new hard drive from a populated one on an old hard drive.
Hey Folks,
I’m wanting to Recursively list the paths of all Folders OR Files in a Given Directory.
The appended script works for Folders and is very fast, but unfortunately it includes packages.
Is there a simple way to filter them out?
--------------------------------------------------------
# Auth: Christopher Stone { Building on work by Nigel Garvey }
# dCre: 2021/07/21 01:01
# dMod: 2021/07/21 01:01
# Appl: AppleScriptObjC, Finder, BBEdit
# Task: Recursively Return File Paths of All Folders in a Directory.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @BBEdit, @Recursive, @Folders
# Vers: 1.00
--------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------
tell application "Finder"
set targetFolderPath to POSIX path of (insertion location as alias)
end tell
set currentApp to current application
set nsPath to currentApp's NSString's stringWithString:targetFolderPath
set nsPath to nsPath's stringByResolvingSymlinksInPath()
# GETS THE NSURL, WHETHER OR NOT THE FILE/FOLDER EXISTS
set folderNSURL to currentApp's |NSURL|'s fileURLWithPath:nsPath
# Get URL of all items in folder, recursively
set IsDirectoryKey to currentApp's NSURLIsDirectoryKey
set theFileManager to currentApp's NSFileManager's defaultManager()
set allURLs to (theFileManager's enumeratorAtURL:folderNSURL includingPropertiesForKeys:{IsDirectoryKey} options:((currentApp's NSDirectoryEnumerationSkipsPackageDescendants) + ((currentApp's NSDirectoryEnumerationSkipsHiddenFiles) as integer)) errorHandler:(missing value))'s allObjects()
# Build an array with URLs to directories
set filteredArray to currentApp's NSMutableArray's new()
repeat with oneURL in allURLs
set keyValue to end of (oneURL's getResourceValue:(reference) forKey:IsDirectoryKey |error|:(missing value))
if (keyValue as boolean) then (filteredArray's addObject:oneURL)
end repeat
# Convert URL array to file list
set AppleScript's text item delimiters to linefeed
set filteredArray to (filteredArray as list) as text
bbeditNewDoc(filteredArray, "activate") of me
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on bbeditNewDoc(_text, _activate)
tell application "BBEdit"
set newDoc to make new document with properties {text:_text, bounds:{0, 44, 1920, 1200}}
tell newDoc
select insertion point before its text
end tell
if _activate = true or _activate = 1 or _activate = "activate" then activate
end tell
end bbeditNewDoc
--------------------------------------------------------
I’m also looking at this:
Filtering folder contents with UTI - #10 by NigelGarvey
It’s far slower, but it seems to work when fed an UTI of public.folder
.
Slower is relative – 10+ seconds isn’t bad for the folder I’m testing with.
TIA.
-Chris