How to set up a folder action to automate document processing

AppleScript gives users the power to perform sophisticated functions, by calling on scriptable features in applications and the rich collection supplied in OS X.

There are several ways in which you can apply a script to your documents: you can turn your script into a mini-application that has file selection dialogs, pack it up into a droplet onto which you drag files that you want it to process, or turn it into a folder action.

Folder actions are scripts that are attached to designated folders. Whenever a document of the kind that can be processed by the script is placed in that folder, the attached script is run automatically. This is true whether the documents are moved locally using the Finder, saved from an application, copied over a network into a shared folder, or arrive as an Internet download. Folder actions can thus be flexible, powerful, and very useful.

There are many applications for folder actions to make your life simpler. Ideas include compression and decompression of .zip archives, converting file names so that they will work on other file systems (removing slash characters, for instance), scaling images and preparing them for web use, and more. So long as your script does not need to interact with the user, it should prove ideal as a folder action, and scripts that provide easy access to command shell tools are particularly enabling.

folderacts11. Enable Folder Actions. By default, OS X may be running with folder actions disabled. To turn them on, and configure folder actions, start the Configure Folder Actions application in /Library/Scripts/Folder Actions. You could instead run the script named Enable Folder Actions in the same folder. Note the other useful scripting tools provided in that folder.

folderacts22. Check application dictionary. Although the outer script required for a folder action is specific to such actions, the business part may be common to other scripts. If you are going to call on an application, the Finder, or another scriptable part of OS X, you will need to open the relevant script dictionary using Script Editor so that you can call the right functions in your code. Here you can see the Folder Actions Suite in System Events, which controls folder actions.

folderacts33. Test shell command. If your folder action is going to run a shell command or script, you should develop and test the shell code using Terminal. After careful study of man unzip, the chosen business code for this example folder action is tested here:
unzip -o -d destinationFolder
This can then be embedded in the AppleScript code to do the work for the folder action.

folderacts44. Develop standalone script. It is usually simplest to develop a standalone or droplet script before turning it into a folder action. Open an example that does something similar to your script, in this case the Convert to PDF script in the example Printing Scripts. Edit that script, saving it with a new script name, so that it incorporates your business code, calling unzip in the shell.

5. Test script. When you think you are ready to test your script, compile it, and iteratively correct any errors picked up by the compiler. Then test it out, correcting bugs as necessary, on some sample files. Remember to test not only files that it should work on, but also those that could result in errors, or should not work – here, .gz and other archive formats.

folderacts66. Wrap and install. Wrap the business code from your test script into a folder action harness. In this case, the most suitable is ‘convert – PostScript to PDF’ in the Folder Action Scripts folder. Save it, and copy that script into the Folder Action Scripts folder using a similar name pattern. You can download my version of the script here: folderactsscript.

folderacts77. Create the folder. Then create the folder for the action, and add it in Folder Actions Setup by clicking the + tool at the foot of the left side of its window.

folderacts8afolderacts8b8. Attach to folder. With your script in the right location and your folder ready to attach that script to, make the connection in Folder Actions Setup. Select your folder in the left pane, and click the + tool below the right pane to select your script for attachment. Note that you should store the folder action script here (or in the equivalent location in your ~/Library folder) if you want to use this tool to attach it.

9. Test folder action. Put a series of test files into your folder, ensuring that you include files that should work, those that could cause errors (such as a Stuffit .sit archive incorrectly given a .zip suffix), and those that your folder action should not try to decompress, such as .gz and other archive formats. Check what happens with duplicate names, and tweak your script to suit.

folderacts1010. Consider Script Debugger. If you are going to use AppleScript much, it is well worth investing in the best script development environment: Script Debugger. Although it might seem costly at first, its cost will be quickly repaid in the time and effort that it saves.

Based on a Masterclass which was originally published in MacUser volume 25 issue 4, 2009.