AppleScript | AppleScrunix – Listing files in a folder
Tuesday 26 July 2011 - Filed under automation + gaps + Technology
One of the most common uses for AppleScript, and thus AppleScrunix, is to process files in a folder/directory. In order to process them you must first list the files. Though I find the listing of a folder of files, just as easy using straight AppleScript as I do with AppleScrunix, I much prefer the variations of returned lists in AppleScrunix.
A simple file listing in AppleScript looks like:
tell application "System Events" to set fileList to name items in folder "/Users/avail/Desktop/PDFs"
Result: {“001_FILENAME.PDF”, “001_FILENAME.txt”, “002_FILENAME.PDF”, “003_FILENAME.PDF”, “004_FILENAME.PDF”, “010_FILENAME.PDF”, “011_FILENAME.PDF”, “016_FILENAME.PDF”}
The same example in AppleScrunix looks like:
set fileList to paragraphs of (do shell script "ls /Users/avail/Desktop/PDFs/")
Same Result: {“001_FILENAME.PDF”, “001_FILENAME.txt”, “002_FILENAME.PDF”, “003_FILENAME.PDF”, “004_FILENAME.PDF”, “010_FILENAME.PDF”, “011_FILENAME.PDF”, “016_FILENAME.PDF”}
If all I had gained was a slightly shorter line of code it wouldn’t be worth it. However, now that we have entered the world of AppleScrunix, we can leverage the power of unix commands with just a few additional characters.
Simply adding an asterisk to my command, I get a list of file paths and not just file names:
set fileList to paragraphs of (do shell script "ls /Users/avail/Desktop/PDFs/*")
Result: {“/Users/avail/Desktop/PDFs/001_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/001_FILENAME.txt”, “/Users/avail/Desktop/PDFs/002_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/003_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/004_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/010_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/011_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/016_FILENAME.PDF”}
What if I just want to list the pdf files and not the text (txt) files? By just adding ‘*PDF’ I get the desired result:
set fileList to paragraphs of (do shell script "ls /Users/avail/Desktop/PDFs/*PDF")
Result: {“/Users/avail/Desktop/PDFs/001_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/002_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/003_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/004_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/010_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/011_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/016_FILENAME.PDF”}
Or, what if I just want to list the files numbered between 010 and 019? By just adding ’01*’ I get the desired list of files:
set fileList to paragraphs of (do shell script "ls /Users/avail/Desktop/PDFs/01*")
Result: {“/Users/avail/Desktop/PDFs/010_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/011_FILENAME.PDF”, “/Users/avail/Desktop/PDFs/016_FILENAME.PDF”}
Study the man page for ‘ls’ and you will find a treasure trove of small command modifications that will make a big difference in your solutions.
AppleScript / AppleScrunix Examples – using the do shell script command in AppleScript.
2011-07-26 » Russ Leseberg