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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Automator for Mac OS X: Tutorial and Examples
40 mins
- What is Automator?
- What You Need
- Getting Started
- Opening Selected Text in Text Editor
- Trying it Out
- Batch Renaming and Resizing Images
- Creating a Folder Action
- Using Automator Variables
- Perform SQL queries on an SQLite file using Automator
- The “View Results” Action
- Making a standalone application to add data from CSV to Database
- Using Variables from your Workflow in AppleScript
- Where To Go From Here?
Trying it Out
Open up Xcode, create a new project (or open an existing project) and select some text.
Next, either right-click on the text or go up to the menu bar, select Xcode. In the Services menu you should now see your new service. Go ahead and run it.
If it all goes right, you should see your selected text in your text editor:
This makes a new document with Command-N, waits half a second, then pastes the text into the new document. Experiment with what best suits your needs!
tell application "TextEdit"
activate
delay 0.5
tell application "System Events" to keystroke "n" using {command down}
delay 0.5
tell application "System Events" to keystroke "v" using {command down}
end tell
This makes a new document with Command-N, waits half a second, then pastes the text into the new document. Experiment with what best suits your needs!
tell application "TextEdit"
activate
delay 0.5
tell application "System Events" to keystroke "n" using {command down}
delay 0.5
tell application "System Events" to keystroke "v" using {command down}
end tell
So that’s how you can automate copying and pasting into application. Next up, you’ll learn how to use Automator to rename and auto-resize a batch of images for iOS projects.
Batch Renaming and Resizing Images
Apple woke up to the fact that preparing images for iOS development can result in poor quality images, so they have included the asset catalog in iOS 7 to make it easier to track image assets.
Note: To learn more about the asset catalog in iOS 7, check out Chapter 9 in iOS 7 by Tutorials, “What’s New in Xcode 5.”
Note: To learn more about the asset catalog in iOS 7, check out Chapter 9 in iOS 7 by Tutorials, “What’s New in Xcode 5.”
You still need to create all of the required images, and it’s still a best practice to follow the previous naming conventions. What’s cool about Automator is that you can delegate the tedious task of processing and renaming to an automated action. Imagine how much easier image processing will be!
If you’ve been thinking about getting an app to help you resize and rename files, don’t do it! You can easily make one yourself with Automator.
There are a few ways to do this, but try using Folder Actions first. They allow you to set up a folder that runs a specific workflow on every file placed within.
Folder actions can revolutionize your workflow. Here are some examples of how you can use them:
- Drag and drop a single file into a folder set up with an action of your choosing, and the workflow will automatically apply to that file.
- Select multiple images, drag and drop them onto the folder and the workflow will work through each file in a loop sequence.
- Use this folder as your default save location for files. This way you have a “set and forget” solution, so your files will always be prepped and ready to use when you go looking for them.
Creating a Folder Action
Open Automator and select New to create a new project. Select Folder Action and click Choose. and you’ll see the input at the top of the workflow is different again:
Here you can choose the folder to apply this folder action to.
From the dropdown menu select Other… and go to the location you want your folder. If you don’t have a location in mind, just create it on the desktop for now. To do this, select New Folder and call it Prepare Images for iOS.
Open this folder via finder and add two more folders in it, one called Processed the other called Processed @2x, this is where photos will go after processing.
Your folder structure should now look like:
Go back to the Automator project and add the Copy Finder Items action from the library to the workflow. Remember to use the search box if you’re having trouble finding it.
In the To: dropdown list select Other… and locate the folder named Processed @2x.
Your workflow should now look like this:
This moves the files from the Prepare Images for iOS folder to the Processed @2x subfolder.
Next, you’ll rename the file so that it has @2x at the end of the file name but before the file extension.
From your library add the action called Rename Finder Items. Make sure the dropdown box at the top of the action is set to Add Text. In the Add: area type @2x and set it to After Name. Note that there is a small text label below this section which gives you an example of how the filename will look after this modification:
Go to File then Save, call it Prepare for iOS.
This is all you need do for full resolution images, but you’ll also need half resolution versions with the original filename.
To try this out, go to your desktop and right-click the Prepare Images for iOS folder, go down to Services and select Folder Actions Setup…
Below is an example of a folder set up to run 2 workflows:
Use the checkboxes to enable or disable folder actions, as well as the individual scripts. How simple is that?
But don’t get ahead of yourself! Work through this next exercise to make it work with a single workflow, and focus on learning about variables.
To try this out, go to your desktop and right-click the Prepare Images for iOS folder, go down to Services and select Folder Actions Setup…
Below is an example of a folder set up to run 2 workflows:
Use the checkboxes to enable or disable folder actions, as well as the individual scripts. How simple is that?
But don’t get ahead of yourself! Work through this next exercise to make it work with a single workflow, and focus on learning about variables.