This site has an identical script written by Nigel Garvey.
It works great, but I would like the script to return only the contents of folders without affecting packages. Therefore, I wrote for myself (and for other users) my own version that does not use the find utility:
property file_id : null
set rootFolder to choose folder with prompt "Choose the root folder"
set logFileHfsPath to (path to desktop as string) & "Folder Hierarchy.txt"
try
set file_id to open for access file logFileHfsPath with write permission
on error
close access file logFileHfsPath
set file_id to open for access file logFileHfsPath with write permission
end try
set eof of file_id to 0
write ("Contents of " & rootFolder & return & return) to file_id
write ("Start " & (current date) & return) to file_id -- starting at eof
my do_subfolders(rootFolder, "")
write (return & return) to file_id starting at eof
close access file_id
display notification "Folder hierarchy TXT" & linefeed & "created on the desktop." with title "Folder hierarchy.script"
on do_subfolders(this_folder, name_prefix)
tell application "Finder"
write (return & name_prefix & (name of this_folder)) as «class utf8» to file_id -- starting at eof
set file_list to files of this_folder
repeat with this_file in file_list
write (return & name_prefix & tab & (name of this_file)) as «class utf8» to file_id -- starting at eof
end repeat
set folder_list to folders of this_folder
repeat with this_subfolder in folder_list
my do_subfolders(this_subfolder, tab & name_prefix)
end repeat
end tell
end do_subfolders