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
You are currently viewing page 2 of 5 of this article. Click here to view the first page.

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.

Screen Shot 2013-12-12 at 11.47.43 am

If it all goes right, you should see your selected text in your text editor:

Screen Shot 2013-12-12 at 11.48.08 am

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!

Note: If you chose a different text editor, you might have to instruct the text editor to create a new document before pasting the code. Here’s what I had to do to get this script to work with TextEdit:
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:

Screen Shot 2013-12-12 at 4.59.40 pm

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:
Screen Shot 2013-12-12 at 12.25.37 pm

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:

Untitled3

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:

Screen Shot 2013-12-12 at 5.10.09 pm

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:

Screen Shot 2013-12-12 at 6.35.42 pm

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.

Note: You can create multiple workflows and assign them to the same folder, and even select which actions to do for a given batch.

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:

Screen Shot 2013-12-12 at 6.35.42 pm

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.

Neil North

Contributors

Neil North

Author

Over 300 content creators. Join our team.