I am making a simple app that will loop through every file and subfolder in that folder, making a tree. I have made no code. The script should get a folder, then loop through every file in that folder,then with any folders in that folder, use that as the parameter for the next loop. I have no idea of how to start on this.
test123testa. It’s not clear to me precisely how you want the script to work. The following returns each folder followed by the files in that folder. It uses the Finder, which can be slow or even fail if the number of folders and files is very large, although it was reasonably quick with a test folder containing 518 folders and files (0.9 second timed with Script Geek).
set folderTree to getFolderTree()
on getFolderTree()
set theFolder to (choose folder)
set folderTree to {theFolder as text}
tell application "Finder"
set theFiles to (name of every file in theFolder)
if theFiles ≠ {} then set the end of folderTree to theFiles
try
set theFolders to (every folder of the entire contents of theFolder) as alias list
on error
display alert "No folders were found in the selected folder"
error number -128
end try
repeat with aFolder in theFolders
set contents of aFolder to aFolder as text
end repeat
set theFolders to sortFolders(theFolders) of me -- disable to change folder sort
repeat with aFolder in theFolders
set end of folderTree to linefeed & aFolder
set theFiles to name of every file in folder aFolder
if theFiles ≠ {} then set the end of folderTree to theFiles
end repeat
end tell
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, linefeed}
set folderTree to folderTree as text
set AppleScript's text item delimiters to ATID
return folderTree
end getFolderTree
on sortFolders(a)
repeat with i from (count a) to 2 by -1
repeat with j from 1 to i - 1
if item j of a > item (j + 1) of a then
set {item j of a, item (j + 1) of a} to {item (j + 1) of a, item j of a}
end if
end repeat
end repeat
return a
end sortFolders
This could be done with a single command using Shane’s FileManagerLib script library.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "FileManagerLib" version "2.3.4"
set setFolderTree to objects of (ChooseFolder) ¬
searching subfolders true ¬
include invisible items false ¬
include folders true ¬
include files false ¬
result type files list
I rewrote my script in post 2 using ASObjC. This script is both faster and more reliable, and it writes the data to a text file on the desktop.
-- revised 2022.10.13
use framework "Foundation"
use scripting additions
on main()
set theFolder to POSIX path of (choose folder)
set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
try
set theFolders to getFolders(theFolder)
set folderTree to getFolderTree(theFolders)
on error
display alert "An error has occurred" message "An item in the selected folder could not be processed" as critical
error number -128
end try
saveFolderTree(folderTree, theFolder)
end main
on getFolders(theFolder)
set fileManager to current application's NSFileManager's defaultManager()
set theKeys to {current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey}
set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:theKeys options:6 errorHandler:(missing value))'s allObjects()
set theFolders to current application's NSMutableArray's new()
(theFolders's addObject:theFolder)
repeat with anItem in folderContents
set {theResult, isDirectory} to (anItem's getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(missing value))
if isDirectory as boolean is true then
set {theResult, isPackage} to (anItem's getResourceValue:(reference) forKey:(current application's NSURLIsPackageKey) |error|:(missing value))
if isPackage as boolean is false then (theFolders's addObject:anItem)
end if
end repeat
set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:"path" ascending:true selector:"localizedStandardCompare:"
return (theFolders's sortedArrayUsingDescriptors:{theDescriptor})
end getFolders
on getFolderTree(theFolders)
set fileManager to current application's NSFileManager's defaultManager()
set folderTree to current application's NSMutableArray's new()
repeat with aFolder in theFolders
(folderTree's addObject:(aFolder as text))
(folderTree's addObjectsFromArray:getFiles(aFolder, theFolders, fileManager))
(folderTree's addObject:"")
end repeat
return ((folderTree's componentsJoinedByString:linefeed) as text)
end getFolderTree
on getFiles(theFolder, theFolders, fileManager)
set theFiles to (fileManager's contentsOfDirectoryAtURL:theFolder includingPropertiesForKeys:{} options:4 |error|:(missing value))'s mutableCopy()
theFiles's removeObjectsInArray:theFolders
return ((theFiles's valueForKey:"lastPathComponent")'s sortedArrayUsingSelector:"localizedStandardCompare:")
end getFiles
on saveFolderTree(theText, theFolder)
set theFolder to theFolder's lastPathComponent
set fileName to theFolder's stringByAppendingString:" Folder Contents.txt"
set theFile to (current application's NSHomeDirectory()'s stringByAppendingPathComponent:"Desktop")'s stringByAppendingPathComponent:fileName
(current application's NSString's stringWithString:theText)'s writeToFile:theFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end saveFolderTree
main()
There are a number of reasons why the Finder script might fail, and it’s impossible to identify the exact reason based on the information you provide. I only encountered an issue with this script when the number of folders and files being processed was large, which is the reason I provided the ASObjC script.
Just as an aside, it would appear that either the macOS or AppleScript versions shown in your post may not be correct. Perhaps you have mistaken Script Editor version for AppleScript version.
The OS version number is correct as i am running this script on a uni-book white mid-2010, but how do i get the applescript version? I do not want to use ASObjC.
To get the AppleScript version, load Script Editor and select Script Editor > About, which will show both the Script Editor and AppleScript versions. The AppleScript version will probably be 2.5 or 2.6.
Based on the information you have provided, it is not possible for me to know why the Finder script doesn’t work for you. I’ve tested and retested the script and it works fine, except when the number of folders and files is too great for Finder to handle. Perhaps another forum member will be able to help.
main()
on main()
set folderPath to (choose folder) as text
set outputText to getFolderTree(folderPath)
set fRef to (open for access file ((path to desktop as text) & "Folder Contents.txt") with write permission)
try
set eof fRef to 0
write outputText to fRef as «class utf8»
close access fRef
display dialog "Listing saved to file \"Folder Contents.txt\" on desktop." buttons {"OK"} default button 1 with icon note
on error msg number num
close access fRef
display dialog msg buttons {"OK"} default button 1
error number n
end try
end main
on getFolderTree(folderPath)
script o
property output : {}
on recurse(folderHFSPath)
-- Store the current folder path (hopefully in HFS format).
set end of my output to folderHFSPath
-- Get the POSIX paths of any subfolders and the names of any files.
tell application "System Events"
set thisFolder to folder folderHFSPath
set subfolderPOSIXPaths to POSIX path of thisFolder's folders
set fileNames to name of thisFolder's files
end tell
-- Sort the file names and concatenate them to the output.
ShellSort(fileNames, 1, -1)
set output to output & fileNames
set end of my output to "" -- Empty line after last file name.
-- Sort the subfolder paths and deal with them recursively.
ShellSort(subfolderPOSIXPaths, 1, -1)
repeat with POSIXPath in subfolderPOSIXPaths
set POSIXPath to POSIXPath's contents
-- Get the HFS path from a System Events folder object specified using this POSIX path!
-- (Workaround for a filing system bug.)
tell application "System Events" to set HFSPath to path of folder POSIXPath
recurse(HFSPath)
end repeat
end recurse
end script
-- Call the recursive handler above.
o's recurse(folderPath)
-- Zap any names or paths beginning with ".".
repeat with i from 1 to (count o's output)
set thispath to item i of o's output
if (thispath begins with ".") then set item i of o's output to missing value
end repeat
-- Return what's left as a single text with linefeeds.
return join(o's output's text, linefeed)
end getFolderTree
(* Shell sort
Algorithm: Donald Shell, 1959.
AppleScript implementation: Nigel Garvey, 2010. *)
on ShellSort(theList, rangeIndex1, rangeIndex2)
script o
property lst : theList
end script
set listLen to (count theList)
if (listLen > 1) then
if (rangeIndex1 < 0) then set rangeIndex1 to listLen + rangeIndex1 + 1
if (rangeIndex2 < 0) then set rangeIndex2 to listLen + rangeIndex2 + 1
if (rangeIndex1 > rangeIndex2) then
set {rangeLeft, rangeRight} to {rangeIndex2, rangeIndex1}
else
set {rangeLeft, rangeRight} to {rangeIndex1, rangeIndex2}
end if
set stepSize to (rangeRight - rangeLeft + 1) div 2
repeat while (stepSize > 0)
repeat with traversalIndex from (rangeLeft + stepSize) to rangeRight
set currentValue to o's lst's item traversalIndex
repeat with insertionIndex from (traversalIndex - stepSize) to rangeLeft by -stepSize
tell o's lst's item insertionIndex
if (it > currentValue) then
set o's lst's item (insertionIndex + stepSize) to it
else
set insertionIndex to insertionIndex + stepSize
exit repeat
end if
end tell
end repeat
if (insertionIndex < traversalIndex) then set o's lst's item insertionIndex to currentValue
end repeat
set stepSize to (stepSize / 2.2) as integer
end repeat
end if
return -- nothing.
end ShellSort
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
Nigel. I tested your script, and it works great. I ran some timing tests with my test folder, and my ASObjC script was marginally faster, but the difference is not significant. Your script does throw an error when the target folder is my home folder.
I suspect the relative speeds depend on the system and/or distribution of files and folders in the hierarchy. On my own machine so far, my script’s faster than the ASObjC one (rather a surprise!) and they’re both faster in Script Editor than in Script Debugger.
Neither of them can handle my own home folder! :lol: Mine goes on forever and eventually beachballs. Yours throws a “Can’t make some data into the expected type.” error after about half a minute (or a minute-and-a-half in SD), the whole of the repeat loop in getFiles() being highlighted. I’ve trapped the error and ascertained that ‘anItem’ is a ‘missing value’ when the error occurs, but haven’t backtracked further to find out why. I’m guessing it’s something to do with the convoluted system of aliases that goes on in some of the Library subfolders.
Thanks Nigel for all the information. This has been an interesting thread.
I had timed our scripts in Script Geek, and I’m surprised by the differing results reported by Script Editor and Script Debugger. With this type of script, I suspect timing results within a reasonable range are not very important.
It’s unfortunate that my ASObjC script wouldn’t run on your computer’s home folder, as a user might want to do this on occasion. However, I’ve spent enough time on this thread, and the OP has no interest in the ASObjC script, so I’ll leave things as they are.
I understand. I’m not inclined myself to spend time on the “entire home folder” problem as it almost certainly involves restricted stuff in the user’s Library folder, which I don’t think is of interest to test123testa anyway.
No further feedback from the OP as yet, but I’ve updated my script this morning to save a text file to the desktop like yours does.
Nigel. I was studying your script to better understand its operation, and I changed code lines 1 and 2 below to code lines 3 and 4 below. I then removed the repeat loop that zapped any paths or files that begin with a “.”. Afterwards, the script ran successfully on my home folder, which wasn’t the case before, although its execution time with my standard test folder increased from 320 to 434 milliseconds (with the write file code disabled). Anyways, a user who has a compelling need to get the visible contents of their home folder might consider this option.
-- existing code
set subfolderPaths to path of thisFolder's folders
set fileNames to name of thisFolder's files
-- new code
set subfolderPaths to path of every folder of thisFolder whose visible is true
set fileNames to name of every file of thisFolder whose visible is true
Thanks, peavine. I’m afraid the ‘whose’ filters are giving me timeout errors on large folders — not necessarily in my Library folder. I’ll look into it further at more leisure. Perhaps not this evening.
It turns out that my System Events script can (often!) negotiate my own home folder without needing to check the disk items’ ‘visibility’ properties. But a couple of my subfolder names run foul of a filing system bug — in Mojave, anyway. These folders were attached to an e-mail I received in 2005 and have names which told me where their contents had to go for the purpose being discussed at the time: eg. “put into ~/Library/Contextual Menu Items/”. The HFS paths to these folders should end with either the trailing slash or with both the trailing slash and a colon. But the ‘path’ returned by System Events only has a colon at the end, so it’s incorrect and can’t be used to identify the folders. This appears to be a file system fault rather than a System Events one, as a similar thing occurs with AppleScript’s own files and aliases:
"iMac HD:blah:blah:put into ~/Library/Contextual Menu Items/:" as «class furl»
result as text
--> "iMac HD:blah:blah:put into ~/Library/Contextual Menu Items:" -- (Trailing slash omitted.)
System Events does have its own quirk though. If it’s used to get just the folders’ names instead of their full paths, all the slashes in the names are replaced with colons! It apparently gets what might be called the “POSIX names”.
An unlikely workaround in the case of the paths is to use System Events to get the folders’ ‘POSIX paths’ (which it gets right), sort on those, and then get the HFS ‘paths’ from folder objects specified using the POSIX paths!
tell application "System Events"
set thisFolder to folder folderHFSPath
set subfolderPOSIXPaths to POSIX path of thisFolder's folders
-- [snip]
end tell
-- [snip]
repeat with thisPOSIXPath in subfolderPOSIXPaths
set thisPOSIXPath to thisPOSIXPath's contents
tell application "System Events" to set subfolderHFSPath to path of folder thisPOSIXPath
recurse(subfolderHFSPath)
end repeat
I’ve modified the script in post #9 accordingly, but it’s probably overkill for the OP’s purposes.
Nigel. I tested your revised script on a documents folder and my home folder, and it worked great in both instances. The workaround is an interesting one. I agree these scripts will probably be of no use to the OP, but I suspect other forum members and googlers will find these scripts to be helpful in the future.
I always use “System Events” over Finder.
I find it to be more reliable.
Try this routine on your large folder and see if it craps out.
set folderTree to getFolderTree()
on getFolderTree()
local theFolders
set theFolder to (choose folder)
set folderTree to {theFolder as text}
tell application "System Events"
set theFiles to (name of every file in theFolder)
if theFiles ≠ {} then set the end of folderTree to theFiles
try
set theFolders to (every folder of the theFolder) as list
on error
display alert "No folders were found in the selected folder"
error number -128
end try
repeat with aFolder in theFolders
set contents of aFolder to path of aFolder
end repeat
--set theFolders to sortFolders(theFolders) of me -- disable to change folder sort
sortFolders(theFolders) of me
--=theFolders
repeat with aFolder in theFolders
set end of folderTree to linefeed & aFolder
set theFiles to name of every file in folder aFolder
if theFiles ≠ {} then set the end of folderTree to theFiles
end repeat
end tell
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, linefeed}
set folderTree to folderTree as text
set AppleScript's text item delimiters to ATID
return folderTree
end getFolderTree
on sortFolders(a)
repeat with i from (count a) to 2 by -1
repeat with j from 1 to i - 1
if item j of a > item (j + 1) of a then
set {item j of a, item (j + 1) of a} to {item (j + 1) of a, item j of a}
end if
end repeat
end repeat
--return a (Not needed since routine sorts the list in-place which was passed by reference by default)
end sortFolders