Automator for Mac OS X: Tutorial and Examples

Learn how to use Automator for Mac OS X to automate tedious workflows in this tutorial with five complete examples. By Neil North.

Leave a rating/review
Save for later
Share

As a software developer, there are a bunch of repetitive and tedious tasks you have to do almost every day. Wouldn’t it be great if there was a way to automate them?

Well, there is – by using the app Automator that is built right into Mac OX X.

In this tutorial, you will learn how to use Automator through five examples, which show you how to automate five common developer tasks:

  1. Xcode -> Text Editor. Create a service that allows you to highlight text from Xcode, then instantly copy it into a new file in your favorite text editor.
  2. Auto-resize images. Create a folder action that automatically resizes, copies and renames images placed in that folder, which prepares them for iOS development.
  3. SQLite queries. Create a SQL query tool for a selected SQLite file.
  4. CSV -> database. Create an application which allows you to drag and drop one or more CSV files into a database. Like magic, the contents of the CSV will automatically populate the selected table. Doesn’t that sound like a nice feature?
  5. Running shell commands. Create a workflow to run shell commands using variables.

By the end of this tutorial, you’ll have learned how you can improve your workflow to save a lot of time. That way you’ll have time for more important things… like catching a nap! :]

What is Automator?

AutomatorIcon

Automator first became a part of OS X at Tiger (10.4). From the beginning, it has been a way for users to automate repetitive tasks with Apple software, such as Safari, iTunes and Calendar. A number of third-party products, like Photoshop, Microsoft Office and many text editors are also compatible with Automator.

Its design is more user-friendly than writing code or running shell scripts, as it presents the user with an array of built-in options to use, and all within a visual interface. The concept behind the design is to make automation accessible without the need to write code.

What You Need

Here are the things you’ll need for this tutorial:

  • An Apple Computer running OS X 10.4 or later: This tutorial was produced in OS X 10.9 Mavericks, and some of the actions listed in here will not be available in older versions. The UI has had many changes since the original release.
  • Automator: This should be installed by default in OS, and as long as you’ve not moved it you’ll find it in your Applications folder.
  • Sample Files: For the fourth Sample project, you’ll have access to two files you can use, so go ahead and download them now. The second sample application also requires some image files on your Mac in order to test. Any image files will do – yes, even LOLcats! :]

Attribution: michellelevine

LOLcat

Attribution: michellelevine

Getting Started

Open Automator from your Applications folder on your computer.

You’ll be greeted by a screen that asks what type of document you wish to create:

Screen Shot 2013-12-11 at 2.05.42 pm

You can get a description of how each document type works by clicking on it and reading the description that shows below the box. Later on in this lesson, you’ll learn more some of these types, but for now, select Workflow and click Choose.

You should now see the Automator UI:
Automator UI

The Media button gives you access to media files on your hard drive that you may wish to work with, including audio, image and video data. The buttons on the right side of the display allow you to Record a series of actions, such as button clicks, within an application. You can also Run your action workflow from here.

  1. The hide/show library button expands and retracts panel 2 and 3. This feature gives you more room to work.

    The Media button gives you access to media files on your hard drive that you may wish to work with, including audio, image and video data. The buttons on the right side of the display allow you to Record a series of actions, such as button clicks, within an application. You can also Run your action workflow from here.

  2. In this section, you can toggle between actions and variables and use the search bar to narrow down the options. You can add an action to the workflow by dragging from 2 to 4 or double clicking the option in 2.
  3. This section tells you additional information about the action or variable selected in section 2. It’s useful because you can see exactly what inputs, options and outputs are available.
  4. This section is your workflow, it allows you to order how you want actions to occur in a procedural, top-to-bottom fashion. You can click and drag an action to re-order the workflow.
  5. In this section you can see a log output and the current variables in use.

For your first example, you’re going to create an Automator workflow to help you export code snippets to your favorite text editor from right within Xcode.

Opening Selected Text in Text Editor

Open up Xcode, select some text, and right click. A pop-up menu will appear, and one of the options in this menu is Services, which has a number of interesting things you can do with that text.

For example, in the screenshot below, if you chose New TextWrangler Document with Selection from the list, it would instantly create a new TextWrangler document that contains the selected text:

Services

Note: If there is no text selected, then the services menu will likely be empty. If you don’t see any services, then you probably don’t have any text selected. Try again!

Now, let’s say you have a favorite text editor that doesn’t have a service created for you already, like the TextWrangler example here. Even if you don’t have access to the internal API, you can still automate the copy/open/paste process by creating your own service using Automator.

Let’s give this a shot! In Automator, close your current document and create a new document with File\New. Then select Service and click Choose.

Then look for this box, which should appear at the top of your workflow pane:

automator workflow input

Since you selected a service, it will look for a usable variable to pass into the workflow. If you have a look in the first dropdown box, you’ll see a list of all the types of media that a service can handle:

variable types

For this project, leave it on text. In the second box, choose the applications you want the service to be available. Leave Any Application checked to allow text from anywhere to display this service.

Note: For future reference, you can restrict which applications can use a service in this same dropdown menu.

To do this, Other… from the drop down list and then locate the app you want from the Applications Folder.

Screen Shot 2013-12-15 at 10.10.02 pm

Note: For future reference, you can restrict which applications can use a service in this same dropdown menu.

To do this, Other… from the drop down list and then locate the app you want from the Applications Folder.

Screen Shot 2013-12-15 at 10.10.02 pm

Make sure the Library is visible on the far left and Actions is selected:

Screen Shot 2013-12-15 at 10.09.32 pm

Click Library from the left column to un-filter the list of actions.

Type Copy into the search box to narrow down the list:

actions library

Find Copy to Clipboard in the list, click down on it and drag it from the actions library to the workflow area on the right.
workflow with action

Notice the triangle just below the entire selection dropdown field?
Screen Shot 2013-12-14 at 7.10.44 pm

This represents an output and data passing from one action to the next. The text collected by the service is passed onto the Copy to Clipboard action. When this service runs, it will copy the selected text to the clipboard.

Go back to the search box for our library and type launch.

Select Launch Application from the list and drag it to the right under your Copy to Clipboard action.
Screen Shot 2013-12-15 at 10.13.21 pm

This action has configurable settings; you can select which application to launch by using the dropdown list. This example uses TextWrangler (download it for free), but feel free to choose your favorite text editor instead.

workflow shot 2

So now your text is on the clipboard, and you’ve launched the application. What’s next? Now you just need to paste the text into the application.

But not too fast! You have a problem. There is no action called Paste from Clipboard. What are you going to do?

One option is to record the events of pasting text into TextWrangler, but why would you do that when you can use AppleScript?

Search and drag Run AppleScript from the actions list to the current workflow below Launch Application.

Note: While it would be fun to take a deep dive into AppleScript, it’s outside the scope of the topic. So, this course will cover some basic code examples, which will hopefully whet your appetite and inspire you to learn more about this useful feature.

In the Run AppleScript box replace the code with the following:

tell application "System Events" to keystroke "v" using {command down}

Your workflow should now look like this:

Untitled

The above is not a paste function, you’re telling the system to press command-v (which has the result of pasting the copied text into your text editor).

Before you test this out, it’s a good idea to make it more reliable, and there are a few ways to do this. Modify the code to the following:

tell application "TextWrangler"
	activate
	delay 1
	tell application "System Events" to keystroke "v" using {command down}
end tell

Be sure to replace the text “TextWrangler” with the name of the text editor you chose.

This makes sure sure your chosen text editor is running and is the active window before pasting your text. Specifically:

  • If you want to give an application multiple commands, you can combine them in a tell statement, as shown in the above code.
  • First, you tell your text editor to activate (become the active window). If the application is closed this command will open it, so you may remove the Launch Application action if you wish.
  • Delay tells the action to wait before proceeding to the next line. The number after the word delay represents the number of seconds to wait.

Now go to File, Save and select the name you wish to appear in your services list. In this example you can use “Open in TextWrangler”.

As services run automated procedures over the selected item(s), so it’s important to name them in a way that describes the action they perform. You’ll see a lot of actions start with “Add to” or “Open In” — some even clarify further by adding “with selection” to the end.

Neil North

Contributors

Neil North

Author

Over 300 content creators. Join our team.