I recently needed a list of all files in a particular folder, actually the folder contained sub-folders also containing files. This tedious task becomes trivial with a couple of UNIX commands and Applescript provides a convienient user interface and the glue between the different UNIX commands.
The script is shown colour coded below. The first part of the script simply asks the user to choose the folder they want to print, and then generates a UNIX compatible POSIX path. The next part generates a temporary file to save the output into.
The next dialog asks the user if they only want to print this folder or to include sub-directories. If the response is to only print this folder then the UNIX command:
1 2 |
ls > target_file |
Can be used to list the directory and send the output to the temporary file.
Alternatively the command:
1 2 |
ls -p -C -R > target_file |
Works recursively through the file structure listing all the files and formats the output into a multi-column format.
The next dialog checks if you want to print of save the output to a pdf file.
The final part of the script prints the temporary file or saves the pdf.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
set the_folder to (choose folder with prompt "Choose a folder or volume:") set the_folder to POSIX path of the_folder tell application "Finder" to set the_temp_folder to (path to temporary items folder) as text --If you want to keep the file use --tell application "Finder" to set the_desktop to (path to desktop folder) as text set target_file to the_temp_folder & "JUNK.txt" set target_file to POSIX path of target_file display dialog "Do you want to print this folder only or include sub-directories" buttons {"Cancel", "This only", "Sub-Directories"} default button 3 if the button returned of the result is "This only" then --Print as list set the_script to "cd " & the_folder & "ls > " & target_file as text else if the button returned of the result is "Sub-Directories" then --Print recursively as formatted list set the_script to "cd " & the_folder & "ls -p -C -R > " & target_file as text else quit end if set theResponse to display dialog "Do you want to print or save pdf to desktop? Type filename in box below" default answer "Myfile" buttons {"Print", "Save PDF"} default button 1 --copy the result as list to {text_returned, button_pressed} if the button returned of the theResponse is "Print" then do shell script the_script else set posix_path to POSIX path of target_file set the_path to path to desktop as string set pdf_path to POSIX path of the_path set the_pdf to pdf_path & (text returned of theResponse) & ".pdf" set to_print to "cupsfilter " & posix_path & " > " & the_pdf do shell script to_print end if --For testing --tell application "Terminal" --activate --do script to_print --end tell |
The script can be downloaded from here.
Last updated 10 April 2023