This works well on my Documents folder, but I don’t know how it’ll cope with a full DVD or how long it’ll take. The text file it produces contains the full path to each item, but if you uncomment the commented code, it’ll give a “folder contents”-style layout, with just the item names indented below the names of the folders they’re in.
I’ve used a Shell sort to keep the script short (!), but there’s a faster sort here if you need it. Alternatively, you could leave the sort out altogether if you didn’t want the indented layout.
The text files are saves as UTF-8 Unicode text with a BOM.
There may be a faster way to do this using shell scripting, but I don’t have time to research it.
-- Shell sort algorithm: Donald Shell 1959. AppleScript implementation: Nigel Garvey 2010.
on ShellSort(theList, l, r)
script o
property lst : theList
end script
-- Process the input parmeters.
set listLen to (count theList)
if (listLen > 1) then
-- Negative and/or transposed range indices.
if (l < 0) then set l to listLen + l + 1
if (r < 0) then set r to listLen + r + 1
if (l > r) then set {l, r} to {r, l}
-- Do the sort.
set inc to (r - l + 1) div 2
repeat while (inc > 0)
repeat with j from (l + inc) to r
set v to item j of o's lst
repeat with i from (j - inc) to l by -inc
tell item i of o's lst
if (it > v) then
set item (i + inc) of o's lst to it
else
set i to i + inc
exit repeat
end if
end tell
end repeat
set item i of o's lst to v
end repeat
set inc to (inc / 2.2) as integer
end repeat
end if
return -- nothing.
end ShellSort
-- Create the text to be written to the file.
-- (Uncomment the commented code to get names indented by container rather than full paths.)
on createText(rootPath, contentPaths)
script o
property paths : contentPaths
end script
set astid to AppleScript's text item delimiters
(*
set AppleScript's text item delimiters to ""
set tabStr to {tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab} as text -- Hopefully more than needed!
set AppleScript's text item delimiters to ":"
set nonIndent to (count rootPath's text items) - 1
repeat with i from 1 to (count o's paths)
set thisPath to item i of o's paths
set tiCount to (count thisPath's text items)
if (thisPath ends with ":") then
set item i of o's paths to text 1 thru (tiCount - nonIndent - 1) of tabStr & text from text item -2 to -1 of thisPath
else
set item i of o's paths to text 1 thru (tiCount - nonIndent) of tabStr & text item -1 of thisPath
end if
end repeat
*)
set AppleScript's text item delimiters to linefeed
set outText to "Entire contents of " & rootPath & linefeed & linefeed & o's paths
(*
set AppleScript's text item delimiters to linefeed & tab
set outText to outText's text items
set AppleScript's text item delimiters to linefeed
set outText to outText as text
*)
set AppleScript's text item delimiters to astid
return outText
end createText
-- Write the text to file as UTF-8.
on writeTextFile(txt, defaultLoc)
set f to (choose file name with prompt "Save the UTF-8 text listing as." default name "Entire contents listing.txt" default location defaultLoc)
set fRef to (open for access f with write permission)
try
set eof fRef to 0
write «data rdatEFBBBF» to fRef -- UTF-8 BOM.
write txt as «class utf8» to fRef
end try
close access fRef
display dialog "The listing has been saved in file \"" & f & "\"" buttons {"OK"} default button 1
end writeTextFile
-- Get HFS paths to all the items in the hierarchy except the root.
on getPaths(rootFolder)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
tell application "Finder" to set thePaths to text items of ((entire contents of rootFolder) as text)
set AppleScript's text item delimiters to astid
return thePaths
end getPaths
on main()
set rootFolder to (choose folder with prompt "Choose a folder or disk to catalogue.")
set rootPath to rootFolder as text
set thePaths to getPaths(rootFolder)
ShellSort(thePaths, 1, -1)
set outText to createText(rootPath, thePaths)
writeTextFile(outText, (path to documents folder))
end main
main()