Monday, December 5, 2011

Setting Up a Folder Action

The Automator application on OS X gives a good drag-and-drop way to have the computer take over some repetitive tasks for us. Things that Automator can be good for are:

  •  combine a bunch of PDFs into a single PDF
  • rename a whole bunch of files
  • run a script (bash/ruby/php)
  • manipulate some images (resize or watermark)

If you think you will actively be working with files (dragging them around) then set up an Application automation. If some other task might be creating the files you are starting with then you might want to set up a folder action. In other systems the folder action is called a "hot folder". This quick tutorial is going to set up a folder action to take any file or set of files I dump into the folder and then add a timestamp to the filename and move the item to a folder on my hard drive called "Finished".

First we will open Automator and select a New workflow from the File menu. We are going to make a workflow of type "Folder Action" so we select the proper icon and click the "Choose" button.

By choosing a folder action, we are asking Automator to watch the folder at all times, and whenever we add any file, group of files or folders to the folder, we want the workflow to begin.

The first thing Automator will want is an answer to "Folder Action Receives files and folders added to". We need to choose what folder is going to have this action attached to it. For the purposes of this demo, we are going to create a new folder called "StampTasks" and place it on the desktop.

The next step is to add the timestamp to each file that gets dropped into the folder. In the Automator, there is a library. The library has two types of actions: those supplied by the system and those supplied by third party applications. Pixelmator gets a shout out here for some of the really useful automator actions they have supplied.

For this tutorial, we are just going to use the action in "Files and Folders" called "Rename Finder Items". We do this by just dragging the Action from the list of actions to the main workflow window. When we first drag the action, we will get a dialogue box asking us if we want to work on the original files or if we want to make duplicates of the files and work on those.
For my purposes, I don't mind working on the originals, so I click Don't Add for this tutorial. Now we are presented with the second step in our workflow. For renaming purposes, Automator provides us with a few different choices for how we want to rename our files. 

Today we are just going to "Add Date or Time" and using the drop down menus I am going to insert the current date after the name of my files.

Now we need another action to move these files out of the folder and place them into their final resting place. So I use the "Move Finder Items" and then navigate to the folder I want to place the items into. "Move Finder Items" can be a little confusing, but basically it is saying "take the file that you did an operation on in the previous step and move it".

Finally, just to show off we can add a "Show Growl Notification" (this action is under Utilities if you have Growl installed). Here is what the final workflow looks like.

At this point you may be excited to run your new folder action but you will see that the "Run" button gives you an error. This is because the workflow starts when you drop a file into the folder, so we need to save the workflow first (NB: with Lion, I have gotten lazy about saving things, but if you don't explicitly save, Lion will happily not save a workflow and it doesn't give you the "Save As..." or other indication that it didn't save). It just won't run your workflow and you will say bad words.

If you have created the workflow and saved it, you should now be able to drop a file into the "StampTasks" folder and wait a few seconds and see it appear in the "Finished" folder with a Growl message. Here is a link to a zip copy of the workflow I created for this tutorial. In general, Automator will place files in the ~/Library/Workflows/Applications directory. (NB: In Lion I notice that the Library folder for the user is not invisible, so you may need to use "Go to Folder" from the "Go" menu in Finder to get at it.

The biggest issue with using the Automator is that debug info and errors are hard to work with. For instance, using the working tutorial above, delete the "Finished" folder. Then when you run the workflow you will get no error, but it won't run either.

Working with Scripts:
Any bash or ruby script can be run in automator using the Run Shell Script command. By default you can run shell scripts in your favorite flavor of sh as well as perl, python or ruby. The shell scripts should be expecting to get the files or text they are to work with just as if they were set up to get things from the command line. The shell script will be run once for each file in the folder.

No comments:

Post a Comment