some workflows involve creating standard sets of job folders at the beginning of a project and then going through and cleaning out any unused folders at the end. this is a good way to standardise archiving procedures but it can also be tedious to clean up once the project is done. this post shows how to easily delete empty folders using applescript.
thanks to xander at cadcoder for the basic structure of the shell command.
to follow along, copy and paste the script examples into applescript editor (found in applications > utilities) or, for older OS versions, script editor (applications > applescript). we’re going to set this one up as a droplet (so you’ll save it from script editor as an application). you can save a droplet to your desktop or drag it to your sidebar for easy access. to run the script just drag and drop a folder onto the droplet’s icon.
the basic form of the script goes like this :
on open mgItem do shell script "cd " & quoted form of POSIX path of mgItem & " && find . -type d -empty -delete;" end open
in the shell, a folder is known as a directory (d). the first part of the command changes the current directory (cd) to our dropped folder. then it performs a search of that folder (find) for empty subfolders (-type d -empty) and deletes them. simple.
this works a treat — most of the time. but macs have these dastardly hidden files, the most common of which are the good old .DS_Store files which help the finder do its thing. a folder containing nothing but a .DS_Store file looks empty, but as far as the shell is concerned, it’s not.
so, here’s a variation to deal with that issue — first delete any .DS_Store files, then delete empty folders :
on open mgItem do shell script "cd " & quoted form of POSIX path of mgItem & " && find . -name .DS_Store -delete; find . -type d -empty -delete;" end open
ok, that’s better. but to make the droplet a little more user-friendly, we should ensure it can handle multiple dropped folders — rather than having to drop them one at a time :
on open mgItems repeat with mgItem in mgItems do shell script "cd " & quoted form of POSIX path of mgItem & " && find . -name .DS_Store -delete; find . -type d -empty -delete;" end repeat end open
and to make it more robust, it should not have a conniption if we inadvertently drop files instead of, or as well as, folders :
on open mgItems repeat with mgItem in mgItems set mgItem to mgItem as alias tell application "System Events" to set theClass to get class of item (mgItem as string) if (theClass as string) contains "cfol" then do shell script "cd " & quoted form of POSIX path of mgItem & " && find . -name .DS_Store -delete; find . -type d -empty -delete;" end if end repeat end open
that’s about it — super fast, super easy way to clean out any unused folders.
for those interested in this sort of thing…
when an applescript is saved as a droplet, its icon includes a downwards pointing arrow. the on open mgItems line at the start of this script is what allows the saved application to run as a droplet. without that on open command you’d need to find some other way to reference the folders to be processed. renaming finder items shows a couple of other ways you could do this.