Macs in Chemistry

Insanely great science


Print Folder Contents Applescript

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:

ls > target_file

Can be used to list the directory and send the output to the temporary file.

Alternatively the command:

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 final part of the script prints the temporary file.

The script can be downloaded from here.

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
end if

do shell script the_script

set to_print to "lp " & target_file

do shell script to_print

--For testing
--tell application "Terminal"
--do script to_print
--end tell