All posts

Automator

Originally published on macresearch.org, around 2006. Reproduced from the author's archive; some links may no longer resolve.

Using Automator as a Script Repository

Automator was introduced in Mac OS X 10.4 to allow users to create workflows, which are quite similar to shell pipelines on the UNIX command line, but have the added advantage that they can work with applications, not just commands. Automator has been widely-adopted by creative types for streamlining image processing and the like, but scientists have been slower to catch on, favoring the Terminal for their automation needs.

In this short piece, I want to show you how you can use Automator as a sort of script repository for all those little commands that you execute often. Effectively, Automator gives you a very simple GUI for your command line scripts, and gathers them all into one place.

To demonstrate this, I am going to create a simple Automator workflow that tar-gzips a file or folder, something which will not be foreign to most Computational Scientists. First, open up Automator, which can be found in the Applications folder. Click on Finder in the Library column on the far left, and then drag the item ‘Get Selected Finder Items’ from the Action column into the workflow area on the right. As the name suggests, and as explained in the small info box in the bottom left of the window when the action is selected, this action simply outputs the paths to the file and/or folders selected in the Finder.

Our objective is to tar and gzip each of these items. For that we will use a shell script. Select the Automator item in the Library column, and drag the ‘Run Shell Script’ action into the workflow under the first action. In the ‘Pass input:’ popup button, select ‘as arguments’, so that the paths output from the Finder action will be made available to the shell script via input arguments, rather than the standard input stream. Finally, enter the following script into the space provided:

`


for f in "$@"
do
    cd `dirname "$f"`
    name=`basename "$f"`
    tar czf "$name.tgz" "$name"
done

`

If you have any shell scripting experience, you will realize that this just loops over the input arguments, and for each one, changes to the enclosing directory and tar-gzips the file or folder there. When all is said and done, the Automator window should look something like the screenshot below.

To test it, select something unimportant on your Desktop in Finder, and click the Run button in Automator. You should end up with a tar-gzipped archive of the selected item on your Desktop, along with the original item.

If you had to open Automator every time you needed this script, it wouldn’t be any more useful than hitting the command line, but you can save Automator actions, making them available via a Finder menu. To do this, simply choose Save from the File menu. Give the workflow a name (eg ‘Tar Gzip’), navigate your way to the folder ~/Library/Workflows/Applications/Finder — if you are not already there — select ‘Workflow’ for the file format, and click the Save button. Now go to Finder, Control-click on a file or folder, and take a look in the Automator menu item. You should see your workflow listed there, and be able to select it to run the script.

This only scratches the surface of what is possible, of course. Basically, any little script or set of commands that you run regularly is a candidate for your Automator repository. Here are a few ideas:

Modify the tar-gzip script above to move the compressed archives to a folder called Checkpoints. Then, whenever you are about to make widespread changes to a project or document, checkpoint it so that you can go back should something go wrong. If you use a Source Control Management (SCM) system like CVS or Subversion, add a few workflows that run commands like update and commit. Another use for SCM systems is when you want to give out some code or files with the SCM files removed. You can develop a workflow that searches for source control files in a selected folder using the UNIX find command, and deletes them using rm. For example, `


for f in "$@"
do
    find -depth "$f" -name '.svn*' -exec rm -rf "{}" \;
    find -depth "$f" -name 'CVS' -exec rm -rf "{}" \;
done

`

If you develop in Cocoa, one very useful command is class-dump, to see the methods and classes defined in an executable or library. Using Automator, you can easily class-dump any file from the Finder, and display the results. (To do the latter, pipe the results from the class-dump command to the TextEdit action ‘New TextEdit Document’.)

With a little thought, you should be able to come up with many, many more applications of Automator with shell scripts. Before you know it, your command line will be completely obsolete. Well, I think for many of us pigs will have to fly first, but moving some of those repetitive tasks away from Terminal and into Finder can be a nice time saver, not to mention RSI prevention.