One of the great things about Mac OS X is how easy it is to automate tasks, either using AppleScript or Automator. I’ve been laid up for a few days, so I had some time to play around with Automator.
If you’ve never used Automator before you’re missing out on a whole aspect of your Mac that makes life so much easier. Automator is simply a method for creating your own automated tasks and saving these tasks as applications and plug-ins. The purpose of this post is to show you how to create a plug-in using Automator that can be attached to a folder.
Before we start, you need to be aware of a couple of the terms that will crop up when you use Automator:
- Actions: Actions are the sequence of steps used to achieve the task you want to complete. Actions are categorised according to the Mac OS Application that they work with, for example, Finder Actions work with Files and Folders, iCal Actions with calendar events etc.
- Workflow: An Automator Workflow is a collection of Automator Actions grouped to achieve the task in mind.
Now that we have the jargon sorted out, we can start working on our Automator workflow. First of all, you can find Automator in the Applications folder. When you open Automator, you’ll be prompted for your starting point: Custom, Files & Folders, Music & Audio, Photos & Images and Text. For the purposes of this mini-tutorial, we’ll be starting with Custom.
The next thing to note is the layout of the Automator window. On the left hand side of the screen you’ll see the list of available actions, while on the right you’ll see just blank space. This will be the canvas for creating your Workflow.
As an example task, we’re going to create a Folder on our Desktop. When files are added to this Folder, they’ll automatically zipped and the .zip file will be placed on our Desktop, and the folder will be emptied.
Now is the time to create the folder on the Desktop: Cmd-Click or Right Click and create a New Folder. Call it “Make Archive”. Back to Automator.
As I said, the left hand side contains the library of available actions. The first action we’re going to use is “Get Specified Finder Items”. You’ll find it in the “Files & Folders” library on the left. Drag the action to the Workflow space on the right.
You’ll notice that there is an “Add” button. Using this button we’ll add the “Make Archive” folder that we previously created on the Desktop.
Next, we tell Automator that we want to get the contents of that folder. Happily there is an action for that, and it’s called “Get Folder Contents”. Drag this action to the Workflow. Within this action, there is a tick box to “Repeat for each subfolder found”. Tick it.
With this Action, Automator knows the contents, of the folder, now it’s just a matter of doing something with that information. As we want to create a ZIP archive, we need an action to do this, and once again Automator obliges with “Create Archive”. Drag this to the Workflow:
With this action, we need to give the created archive a name and a location to save to. Enter your own personal preferences here.
That’s the basic functionality in place. Now we need to test it. Copy a file to your “Make Archive” folder. When you have that completed, click the “Run” button in the top right corner of Automator. If everything is working, you should see a green tick and “Workflow Completed” in the Automator status bar. If you don’t, re-read the above instructions.
You’ll notice that we had to manually run the Automator Workflow for anything to happen. Now I’ll show you how to attach the Workflow to the “Make Archive” folder so that it runs automatically anytime you add files to the folder.
The automation is handled by saving the Workflow as a Folder Action Plug-in. A Folder Action is triggered when a specified folder changes. In Automator, go to “File, Save as Plug-in”. You’ll be prompted for a name and the type of plug-in to save as. Call your plug-in “make_archive”, and the plug-in type should be changed to “Folder Actions”.
When you do this, a third option will become available, “Attached to Folder”. Browse to your “Make Archive” folder on the Desktop. You may be prompted to “Enable Folder Actions”. If you are, tick the box and continue with the save.
To test your new automatic archiver, copy a file to the folder and watch Automator do it’s magic!
There is one small problem though: when the archive has been created, the original file stays in the “Make Archive” Folder. If you leave this file in place, it will be added to the next archive you create, which is not what we want. The answer lies in creating a new Automator Workflow. In this new Workflow, we use the same first two Actions, but we replace the “Create Archive” Action with “Move Finder Items to Trash”.
Once again save this Workflow as a Plug-in and attach it to your “Make Archive” folder. Perform another test, and this time the folder should be emptied once the archive is completed.