property script_name : "List Folder Contents"
property script_description : "This script will list a folder's contents returning full paths as strings and limit the list to specific file types. The script can also process subfolders (recursion)."
set the_folder to (choose folder with prompt "Choose a Folder to List:") as Unicode text
set file_types to {"APPL"} --file types, set to {} and inc_folders to true to just return folders; file types are 4 character codes such as "osas" or "TEXT"
set with_subfolders to my get_boolean("Search subfolders?", {"No", "Yes"})
if with_subfolders = "user cancelled" then return
set inc_folders to my get_boolean("Include folders in the list? (If you only want file names, and not folder names, select \"No\".)", {"No", "Yes"})
if inc_folders = "user cancelled" then return
set use_posix_path to my get_boolean("Return the paths as HFS (Mac-style) or POSIX (UNIX-style)?", {"HFS", "POSIX"})
if use_posix_path = "user cancelled" then return
set return_as_string to my get_boolean("Return the results as an AppleScript list or a string with each file on its own line?", {"List", "String"})
if return_as_string = "user cancelled" then return
set add_to_clipboard to my get_boolean("Copy the results to the clipboard?", {"No", "Yes"})
if add_to_clipboard = "user cancelled" then return
set the_files to get_folder_list(the_folder, file_types, with_subfolders, inc_folders, use_posix_path)
if return_as_string then
tell (a reference to my text item delimiters)
set {old_tid, contents} to {contents, return}
set {the_files_string, contents} to {the_files as Unicode text, old_tid}
end tell
copy the_files_string to the_files
end if
if add_to_clipboard then
if not return_as_string then
copy the_files to the_files_string
repeat with i from 1 to (count the_files_string)
set item i of the_files_string to ("\"" & item i of the_files_string & "\", ")
end repeat
set the_files_string to ("{" & (text 1 thru -3 of (the_files_string as Unicode text)) & "}")
end if
set the clipboard to the_files_string
end if
beep
return the_files
on get_folder_list(the_folder, file_types, with_subfolders, inc_folders, use_posix_path)
set the_files to {}
tell application "Finder" to set folder_list to items of folder the_folder
repeat with new_file in folder_list
try
set temp_file_type to file type of new_file
on error
set temp_file_type to "fold"
end try
if file_types contains temp_file_type or file_types = {} then
if use_posix_path then
set the_files to the_files & {POSIX path of (new_file as Unicode text)}
else
set the_files to the_files & {new_file as Unicode text}
end if
end if
if temp_file_type = "fold" then
if inc_folders and file_types ≠ {} then
if use_posix_path then
set the_files to the_files & {POSIX path of (new_file as Unicode text)}
else
set the_files to the_files & {new_file as Unicode text}
end if
end if
if with_subfolders then set the_files to the_files & my get_folder_list((new_file as Unicode text), file_types, with_subfolders, inc_folders, use_posix_path)
end if
end repeat
return the_files
end get_folder_list
on get_boolean(m, b)
try
return (button returned of (display dialog m buttons ({"Cancel"} & b) default button 3 with icon 1) = (b's item 2))
on error
return "user cancelled"
end try
end get_boolean