Search This Blog

Saturday 12 June 2010

Mac: Folder actions

Programmable automation is built into the OS X operating system in the form of the scripting language AppleScript. As its name suggests, AppleScript is a user interface for writing scripts—small programs that you can run on their own or in conjunction with other activities (including other scripts or macros). One of the more useful things you can do with AppleScript is to create folder actions, a few examples of which we'll examine here.

A folder action has a simple purpose: It applies the relevant instruction to any item added to or removed from the folder. Let's say, for example, that you have a number of graphics files in TIFF format, and you need to resize them, convert them to grayscale, convert them to PNG format, and resave them for a Web publication you're working on. A graphics program such as Photoshop could do this, but if the files are coming from various locations on your Mac, it would be more convenient to drag them into a separate folder from those locations and have the transformations applied automatically. A folder action would make this happen—and without your having to open the programs required to effect the changes. Of course, folder action scripts don't have to be that complex. Nor should your first attempt be, if you're considering writing it from scratch, In fact, OS X ships with several built-in folder action scripts, all of which are relatively simple. Here's a list of a half-dozen of them:
add – new item alert.scpt A pop-up window appears to let you know that an item has been added to the folder. This script is obviously useful for shared folders whose contents you need to monitor.
close – close sub-folders.scpt Designed for screen clean-up; whenever an item is added to the folder, the folder and all its subfolders close automatically.
convert – PostScript to PDF.scpt This script converts PostScript (EFS) files to PDF files the instant you add them to the folder.
image – Duplicate as JPEG.scpt When you add an image file to the folder, OS X will automatically create a copy of that file in JPEG format. If it's already in JPEG format, it will do nothing. There are actually three scripts of this type built in to OS X: one to convert to JPEG, another to PNG, and a third to TIFF.
image – Rotate Left.scpt Any image file added to the folder is rotated 90 degrees to the left. A similar script does the same but rotates to the right, while two others flip the image vertically or horizontally.
image – Info to Comment.scpt When you add an image file to the folder, information about the file is added to the Spotlight comments field. A related script lets you automatically see these comments whenever a new file is added to that folder.
 
The built-in folder action scripts are located in Macintosh HD -> Libraries -> Scripts -> Folder Action Scripts.
Clearly, these aren't earth-shaking additions to anyone's automation arsenal. But they point to the power of folder actions, especially in cases where you have multiple files to be acted upon. Once you have the script in place, the simple act of dragging a file from one folder to another causes the actions specified by the script to occur. You can't get much more basic than that.
Folder actions work by attaching specific scripts to specific folders: I'll get to this in a minute. For folder actions to work at all, though, the folder action system must be activated. You can do so in two primary ways:
  1. Right-click on a folder and choose More -> Enable Folder Actions
  2. Right-click on a folder, choose More -> Configure Folder Actions, and check Enable Folder Actions on the resulting Folder Actions Setup dialog box.
Enabling folder actions does so for your entire OS X system, not just for the specific folder; in fact, you can't simply enable folder actions for one folder but keep them disabled otherwise.
 
This cascading menu points to the Enable Folder Actions option. After you've enabled Folder Actions, you can disable them by returning to this menu.
With folder actions enabled, you can tell OS X what you want that folder to do, by assigning it a script. To select the script you want applied to a specific folder, right-click that folder and choose More -> Attach a Folder Action. Typically, you'll be presented with the Folder Actions Scripts folder, but if not, navigate to Macintosh HD -> Libraries -> Scripts -> Folder Action Scripts.
 
The scripts are found in the far right panel; consider placing your custom and downloaded scripts in this folder as well for easy reference.
Each folder may contain multiple folder actions, so you're not restricted to doing only one thing with a drag and drop. If you have your Mac's sound volume turned up, you can hear the scripts being enacted, each with a separate ping. In some cases, the script creates sub-folders; open the parent folder to see what the script has done. (For example, the file-conversion scripts included with OS X do this.)
 
The Original Images sub-folder contains the graphics files dropped into the parent folder; the JPEG, PNG, and TIFF sub-folders contain the corresponding copied and converted files. All these folders were created automatically by the script.