Wednesday, October 20, 2010

Automate Just About Anything on Your Mac, No Coding Required

By Whitson Gordon

Automate Just About Anything on Your Mac, No Coding Required

Automate Just About Anything on Your Mac, No Coding RequiredEven if you're not a programmer, you can easily create tiny, time-saving applications that breeze through repetitive tasks—renaming large groups of files, executing terminal commands, and much more—with the simple, code-free, drag-and-drop interface of OS X's built-in tool, Automator.

For those of you who don't know, Automator is a program that allows you to create your own workflows, through the use of drag-and-drop actions, that automate repetitive tasks. We've featured numerous Automator workflows and applications on Lifehacker before, but you may not know how easy it is to make your own. You don't need to be a programmer or hacker to make use of Automator, either—anyone can make use of this simple (yet powerful) tool, no matter what menial task you need to automate. Here's a quick guide to using it, as well as a few examples to get the gears turning.

Creating a Simple Workflow in Automator

One of the easiest ways to use Automator involves creating workflows that perform batch operations. It only takes a minute or two to put together a workflow, thus saving you tons of time renaming files, cropping photos, or converting PDFs to an editable format.

Anatomy of a Workflow

Automate Just About Anything on Your Mac, No Coding Required

An Automator workflow is a simple series of actions that perform in sequence. You can select any number of actions from Automator's large library, and drag them into whatever sequence you want. When you run the workflow, Automator will go through each step, pushing the output from the last action into the next action. Thus, you can have an action that grabs some files, pass those onto an action that edits them, pass those edited files on to another action, and so on. It's essentially programming for the non-programmer—you tell it what to do each step of the way.

Batch Rename a Group of Files

Automate Just About Anything on Your Mac, No Coding Required

Let's say we have a series of files that we want to be able to look through, but their filenames are a mess. A common example of this problem involves digital photos. Every time you copy photos from your camera to your computer, they have names like PICT_001, PICT_002 and so on. Say you want the filenames to actually convey something about where the photos come from, like "Italy Vacation 001", "002", and so on. Or, if you prefer, you can add the photo's date and time after each filename, so you know when each photo was taken. With just the action "Rename Finder Items," you can manipulate a group of filenames any way you want. You can even change a filename's case or find and replace text. For this example, we'll assume each group of photos is already sequential, and that we just want to give them a more descriptive name.

Start up Automator and select Workflow. In the left sidebar, you can view your library of actions, organized by category, like "Music" or "Photos". You can also search for an action using the search bar at the top if you know what you're looking for. In this case, head up to the search bar and search for the Ask for Finder Items action, since we want the first part of this workflow to be our decision on what files to rename. Click on it in the left sidebar and drag it into the main window. Hit the Allow Multiple Selection checkbox, since we'll be renaming files in batches.

Then, head back over to the search bar and type in "Rename Finder Items". Drag that action over to the main window, under your Ask for Finder Items action. It will give you a popup, asking you if you want to add a Copy Finder Items action, to which we'll answer no—we want to change the original files, so leaving it as is is okay. Hit "Don't Add" and configure the action as you want. In this example, we want to rename all the files sequentially, so choose "Make Sequential" in the top drop-down, add the number to a new name ("Italy Vacation") and choose to start the numbers at 1. You may also want to force the numbers to have 2 or 3 digits (i.e. adding "01" to the end of the first file instead of just "1"). Try running the workflow by hitting the Start button in the upper right hand corner. It will ask you which files you want to tweak, and then rename them all in one fell swoop. Check out the video above to see an example of this in action.

Batch Crop Images

If you have a handful of photos that are the wrong size, but don't feel like cropping them one by one, you can send them through an Automator workflow that will crop them all down for you. Of course, make sure you're generous enough with your shots that nothing gets cropped out by accident.

Automate Just About Anything on Your Mac, No Coding Required

To do so, just drag in the "Ask for Finder Items" or "Get Selected Finder Items" actions—depending on how you want to use the app—into the workflow. Alternatively, you could add the "Get Selected iPhoto items" if you're an iPhoto user—this will just grab the selected photos in iPhoto instead of going through the Finder.

Next, drag the "Crop Images" action into the workflow. You can set it to fit the height, width, or a relative edge of the image to your specified dimensions as well, thus resizing the image at the same time. Automator shows a few example photos in the action window so you can see what each option will give you. Note that in this case, you may actually want to add a "Copy Finder Items" action in between the ask and crop steps—that way, you don't accidentally ruin any photos in the automation process.

Extract Text from PDF Files

Automate Just About Anything on Your Mac, No Coding Required

Let's say you've been given a bunch of PDF files that you want to put into a text editing program, like TextEdit or Microsoft Word. Our workflow will start the same as the other two—with the "Ask for Finder Items" action—and then continue with the Extract PDF Text action. Choose Rich Text (so you don't loose your formatting), save the output to your Desktop (or whever else you want), and let 'er rip. After selecting the PDFs you want to work with, Automator will provide you with a new set of editable .rtf files on your Desktop that you can now work with.

Automate Just About Anything on Your Mac, No Coding Required

With workflows like this, you can make it even more useful by turning it into a droplet with a small tweak. Just delete the "Ask for Finder Items" action (since the droplet will get input from the Finder directly), and go to File > Save, choosing "Application" from the dropdown menu in Automator. Now, you can just drag a selection of files onto the application's Finder icon, and it will run using those files as its input.


These are just a few simple examples of how you can use Automator to work with multiple files, but there are a ton of actions available to you for any given situation. Just look at the PDF section of Automator's library—you can turn a bunch of PDF files into images, set their metadata, watermark them, encrypt them, or do any other number of things. Spend some time poking around in Automator to see all the things it can do—it will help you save a ton of time on some of those more tedious tasks.

Get Away from the Command Line

While Automator is great for dealing with a large number of files, you actually have a lot more available to you in Automator than just running through menial jobs. Whats really neat about Automator is that it can add new features to certain programs that weren't originally available through the GUI. For example, if you want to create a symbolic link (which is incredibly useful for Dropbox enthusiasts), you have to open up Terminal or download a separate app to do so. Instead of doing that, you might as well turn that task into a small app or context menu item, using the "Run AppleScript" or "Run Shell Script" action in Automator.

Sync a File or Folder with Dropbox

Automate Just About Anything on Your Mac, No Coding Required

One of the things I always find myself wanting is a way to sync files outside my Dropbox with the click of a button. Sadly, neither Dropbox nor OS X have this feature built-in, but I can make my own context menu item for the Finder with Automator. Just open up Automator and hit Service in the opening window. Services (as they are arranged in Snow Leopard) are custom context menu items or menu bar items that you can add to pretty much any Mac program. For this example, we're going to create a service that allows us to right-click on a file or folder in the Finder, hit a new "Add to Dropbox" menu item, and sync that item to Dropbox through the magic of symbolic links.

When the workflow window comes up, change the dropdown menus at the top to read "Service receives selected files or folders in Finder". Then add the "Run AppleScript" action to the workflow. Copy the code below, replacing the text that was in the Run AppleScript window:

 on run {input, parameters}          repeat with i from 1 to (count input)                 set posix_path to POSIX path of (item i of input)                 if posix_path ends with "/" then set posix_path to ¬                         text 1 thru -2 of posix_path                 set AppleScript's text item delimiters to "/"                 set filesname to quoted form of text item -1 of posix_path                 do shell script "ln -s " & quoted form of posix_path ¬                         & " " & "~/Dropbox/" & filesname         end repeat          return input end run 

This will create a symbolic link of the selected files or folders in your Dropbox, ready for Dropbox to sync them to your other computers. Make sure you change the "~/Dropbox/" path on the 10th line if your Dropbox folder is located somewhere else. Then, just save it with whatever title you want (something like "Add to Dropbox"), and you'll find it in the Finder's context menu whenever you right-click on a file. Alternatively, Services are also always available in the menu bar by clicking on the application's name and going to Services.

Automate Just About Anything on Your Mac, No Coding Required

Again, this is just an example. You can turn pretty much any AppleScript or shell script into an Automator action, giving you nearly limitless possibilities if you're willing to roll up your sleeves a bit. Sure, Automator is still a "no coding required" application, but if you're willing to do a little bit of code (AppleScript and terminal commands are pretty easy to learn), you cam make much more complicated workflows. In fact, Services like our previously mentioned Middleman, which syncs iTunes with non-iPod devices, and the Cut and Paste for the Finder actions were created in Automator using AppleScript and shell scripts.

Also note that, if you don't feel like messing around with scripts, you can download third-party actions to your computer and use those (as long as you don't want to distribute your workflow—it will only work if the user has installed the necessary third-party actions). If you ever have a need for an action that isn't in Automator, check around Apple's Automator action database or sites like Automator World to see if they have something that will work. Also, for what it's worth, the above script is a modified version of this script I found over at Macworld. Even if you don't have a ton of scripting experience, searching around the net for existing scripts can be extremely helpful.


This is just a smattering of ways you can use Automator. Automator has a ton of other functionality in it, like Services (mentioned above) or the ever-useful Folder Actions, which allow you to do things like print files from your mobile phone or remote computer. We've barely scratched the surface of all it can do, and really, you're only limited by your imagination (and perhaps scripting abilities). Be sure to check out the other workflows we've featured, and share your own favorite uses for Automator in the comments.

Number of comments
  • Share this:

No comments:

Post a Comment

CrunchyTech

Blog Archive