Automate tasks with Folder Actions

One of the great things about OS X is that it’s like the real world. You store your files in folders; when you don’t want something, you put it in the Trash. But on your Mac, a folder isn’t really just a folder, and that’s not a bad thing. Take for example, Folder Actions. With this handy feature, you can attach AppleScripts to specific folders and have these scripts run automatically whenever you add files to the folders. That means that you can automatically get an alert when files are added to a specific folder, change the Finder labels when you put them in a folder, or unzip archives. Here’s how Folder Actions work, and how you can use them to save time.

Turn on Folder Actions globally

To use Folder Actions, you must first turn this feature on globally. To do this, right-click or Control-click on any folder and choose Services -> Folder Actions Setup. (If you only have a few Services, you may not have a Services menu and will instead see the Folder Actions Setup menu item at the bottom of the contextual menu.) When you choose Folder Actions Setup, a small window opens showing a list of AppleScripts. Select Enable Folder Actions in the window and then quit (Command-Q).

Get alerted to incoming files

Now, let’s apply a simple, useful folder action to one of your folders. If you share files with others over a network, your colleagues may deposit files in your Mac's built-in Drop Box folder. But how do you know when a new document has arrived? Add a Folder Action that displays a dialog box whenever a new file is added to this folder.

To do this, first go to your Public folder. You’ll find it inside your home folder—the one with the house icon. (Or, in the Finder select Go -> Go To Folder and type ~/Public/). Right-click or Control-click on the Drop Box folder inside, choose Folder Actions Setup, click on “add - new item alert.scpt,” and then click on Attach.

Alert
Once you attach the Folder Action to your Drop Box folder, you'll see an alert like this every time someone puts something in the folder.
Let’s try this out. Take any file and place it inside this folder. Within a few seconds you should see a dialog box telling you how many new items were added to the folder. This dialog asks if you would like to view these items. Click on Yes to open a window showing the files, or click on No to dismiss the dialog box. You may not need to see the files right away, but now you know that they have been added.

Another way to use this same script is with your Dropbox folder. If you share files using Dropbox’s shared folders, you may want to know when you receive new files. Dropbox offers “desktop notifications”—floating alerts—but they fade away after a few seconds. Attach the same script we used above to a shared folder and you’ll see an alert whenever Dropbox downloads new files to that folder. Note that this only works when an item is added to the folder itself, not when someone adds an item to one of its sub-folders. If you have a lot of sub-folders in your shared folder, you can apply the same script to each of them to get alerts for all of the folders.

Explore OS X’s other built-in actions

OS X includes a handful of folder action scripts in the /Library/Scripts/Folder Action Scripts folder. Let’s look at a couple of them and see how they can save you time.

In this folder, you’ll see a number of scripts that can act on graphics. For example, there are three scripts that can automatically convert image files to JPEG, PNG or TIFF format. Let’s say you have a blog, and you want all your graphics to be in JPEG format. Create a folder somewhere and attach the “image - Duplicate as JPEG.scpt” script to it. Every time you add an image file to that folder, it will be immediately converted.

There’s a nifty script if you often open a lot of folders within a specific folder. As you know, closing all those windows can be annoying. If you attach the “close - close sub-folders.scpt” script to a folder, any sub-folders that are open will be closed when you close the top-level folder. This is useful if you have projects that require you to access files in a lot of sub-folders.

Turn off Folder Actions

Folder Actions setup
In this window, you can see all folders that have Folder Actions attached to them, and which scripts they are set to run.
If you ever want to turn off Folder Actions for specific folders, you can access the Folder Actions Setup applet as described above by clicking on any folder. Dismiss the list of scripts, and you’ll see a list of those folders for which you’ve set Folder Actions, and which scripts you’ve attached to them. Turn off any of these by unchecking them, or delete any folder or script by selecting it and clicking on the minus button.

I’ll follow up this article with another about more Folder Action scripts for you to try. If you have any favorites to share, please talk about them in the comments.

Senior contributor Kirk McElhearn writes about more than just Macs on his blog Kirkville. Twitter: @mcelhearn Kirk is the author of Take Control of Scrivener 2.

Subscribe to the Help Desk Newsletter

Comments