What are Folder Actions?
Everyone’s workflow includes repetitive tasks that are performed in response to files being added to- or removed from folders: perhaps you start a Time Machine backup when you save an important file, always open Transmission when a .torrent file is downloaded, regularly check shared folders for new files, repeatedly upload resources to a remote server, or simply add a line to a log file whenever files you add or delete files. Wouldn’t it be convenient if these tasks were automatically taken care of for you?
Folder Actions, one of OS X’s powerful automation and scripting features, can tell the system to monitor a folder and automatically execute an Automator workflow, AppleScript, or shell script when items are added to- or removed from the folder. The ability to automatically do virtually anything as files are added to- or removed from folders can be a huge time saver in any workflow.
As an introduction to Folder Actions (and, if you’re not already familiar with it, Automator), we’ll create a simple action that automatically prompts us to give our screenshots descriptive names as we take them.
Before we begin, we have a little setup to do. Since we only want to be prompted to rename screenshots and not other files added to the Desktop, we have to configure OS X so that screenshots are saved in their own folder. I highly recommend doing this even if you don’t end up using this Folder Action; it really helps keep both your screenshots and Desktop organized.
Make a new folder wherever you want and call it something like ‘Screenshots’. Then run the following command in Terminal, replacing
path/to/folder/ with the path to the folder you just created.
defaults write com.apple.screencapture location path/to/folder/
You will have to run
killall SystemUIServer for the changes to take effect.
Creating a new Folder Action
The easiest place to begin a new Folder Action is Automator. Open that up (from
/Applications) and make a new document. Select the
Folder Action type and click
You’ll be given a blank workflow with an option in the top bar to select the folder to which this Action should be bound. Click that popup menu and choose
Other... to select your Screenshots folder.
Use cmd+s to save your empty Folder Action and give it a name like ‘Rename New Screenshots.’ You can see it attached to your screenshots folder in the Folder Actions Setup application (found in
Building the Automator workflow
If you’ve never used Automator before, you’ll soon see that it’s very simple. Automator workflows are composed of actions, which can be dragged into the workflow from the Library on the left. These are the elements that make up what a workflow does. Each action optionally receives an input and creates an output. In a Folder Action workflow, the first action is automatically given a reference to the file that has been added to the folder as an input. Variables are the other main component of an Automator workflow. Like in AppleScript and other programming languages, variables are containers that store data to be accessed later. Automator comes with actions to save and retrieve variables as well as a panel that lists active variables so they can be dragged into actions (you may have to click the button with two rounded rectangles at the bottom of the panel first).
To learn more about Automator, give Apple’s Automator Programming Guide a read.
Before we build our workflow, let’s outline what we want to do:
- Save a reference to the new file so we can access it later
- Ask the user to type a name for the new screenshot
- Save that name in a variable so we can access it later
- Rename the added file (we’ll retrieve the saved reference to it) to the name the user entered (which we’ll also retrieve)
Translating this into Automator actions is fairly easy.
Use the search bar in the Actions Library to find the Set Value of Variable action and drag it into the workflow area. Click on
New Variable... in the
Variable: menu and name the new variable ‘file’.
Next we have to allow the user to type a name for the screenshot. This is done with the Ask For Text action. After dragging it into the workflow, we can customize how the prompt is displayed. Change the
Question: (the prompt displayed to the user) to something descriptive, like ‘Name your new screenshot:’. You can also provide a default answer, such as ‘new screenshot’. We don’t want to create nameless files, so check the
Require an answer box. The buttons can also be renamed; I left
Cancel alone but changed
OK to ‘Save’. Finally, Ask For Text isn’t interested in the output of the previous action (in this case assigning a variable), so click its Options tab and check the
Ignore this action's input box.
The next step is to save that name into a variable so we can access it later. Add another Set Value of Variable action and call its variable ‘name’.
Now we need to rename the file. Since the Rename action requires its input to be the path to the file, we need to retrieve the
file variable we saved. Add a Get Value of Variable action and select ‘file’ from the
Variable: menu. Since this action doesn’t care about the result of the previous action, check the
Ignore this action's input box in its Options tab.
Finally, drag out a Rename Finder Items action. Choose
Don't Add when Automator asks if you want to add a Copy action so that the original files are preserved; we really do want to rename the original files. Change the new action’s operation menu to
Name Single Item, but take note of the other options: they include some powerful renaming capabilities that you may want to make use of in the future. Set the
Name: menu to ‘Basename only’ so that the files’ extensions are preserved. Finish the action off by dragging the
name variable token from the bottom pane into the
The completed workflow should look like this:
Testing our Folder Action
Try taking a new screenshot. It should automatically trigger a dialog box like the one we created:
Type in a name and hit
Save. The new file in your Screenshots folder will have the name you entered. Say goodbye to digging through dozens of screenshots on your desktop to find the right one!
The main drawback of Folder Actions is a delay that is sometimes present between a file being added/removed and the Action being triggered. This is a restriction imposed by OS X (search for messages containing
Throttling respawn from
com.apple.FolderActions.folders in Console.app), presumably for performance purposes. This lag can be very noticeable when an Action requires user interaction, as ours does, but is less of an issue when the Action does work in the background.
As you’ve seen, Folder Actions can do a lot. Automator has a pretty good set of built-in actions (hello Run Web Service!), but you can go even farther with the Run AppleScript and Run Shell Script actions. AppleScripts can also be added directly: start your script with
on adding folder items to someFolder after receiving someFiles and save it in
/Library/Scripts/Folder Actions Scripts/. Almost any repetitive task you do manually can be automated with Automator, AppleScript, or a shell script and triggered by a Folder Action.
Another route for filesystem-based automation is the app Hazel from Noodlesoft ($25). Hazel includes lots of tools to help keep files and folders organized, and also allows Automator workflows, AppleScripts, and shell scripts to be bound to changes in files and folders.
Make an awesome Folder Action? Post in the comments!
As always, if you have any questions about Folder Actions or Automator, feel free to ask on Ask Different.