How to create a Service in Snow Leopard
As I discussed in my look at Services in Snow Leopard, I think this revamped feature is one of the hidden gems in Mac OS X 10.6. While being able to control what appears in the Services menu is a nice change, the real winner here is the ability to create your own Service, or to download Services created by others.
I’ll walk you through the creation of a simple Service using Automator. Even if you’re not a programmer, and you find the thought of AppleScript scarier than a root canal at the dentist, I promise you’ll have nothing to fear by following this tutorial.
At the end, you’ll have a functional Service, along with (hopefully) enough knowledge to venture into the creation of your own Services. If nothing else, though, you’ll get a sense for just how easy it can be to create simple Services.
For the tutorial, I decided to create a Service that would extract the text from one or more PDFs selected in the Finder. The extracted text will be placed into a new text file, saved, renamed, and then moved. All of this functionality will take only a few minutes to create, and can then be used from the Finder’s contextual menu, or via a keyboard shortcut.
Obviously, this tutorial is only for those running OS X 10.6. You’ll need to have some PDFs available to test with the Service, and enough extra drive space to save the text files you’ll create—and trust me, if you don’t have that much free space, you’ve got much larger issues than not being able to follow this how-to!
As an aside, I also created a video version of this tutorial [3:15, 11.6MB] as a learning exercise for Camtasia for Mac, which I’m in the process of reviewing. If you’d prefer to watch the video version of this tutorial, just click the above link. It differs slightly from this version, but creates the same text extraction Service.
Build the Service
To get started, launch Automator (in Applications). A new window will appear with a drop-down sheet displaying a number of workflow templates; select Service, then click Choose.
This will open a (mostly) blank template, which will look familiar if you’ve used Automator in OS X 10.5. However, up at the top, you’ll notice a thin section with two pop-up menus and a checkbox; this area is used to define what type of information your Service works with, which applications it works in, and whether the action it takes replaces the selected text or not.
Click the first pop-up menu, and set it to PDF Files, as that’s the type of file we’ll be working with. Click the second pop-up menu, and set it to Finder, as that’s where the Service will be activated. The checkbox won’t be clickable, as we’re not working with a selection of text.
Now it’s time to create the actual Service. Because the Service is only launched after selecting one or more files, we don’t need to do any work to find the files we’ll be working with; they’re passed in when you launch the Service.
Given that the files are already known to your Service, the first step is to select the PDFs entry in the Library column, then drag the Extract PDF Text action into the right-hand work area. This is the action that actually extracts the text from the selected PDF(s).
Set the options on this action as you prefer—I used Plain Text output, and added a page header. I left the destination set to Desktop (as the file will be moved later), and the output file name the same as the input name (but the extension will differ, so there’s no conflict).
After extracting the text, the next step is to rename the file. In the Library column, select Files & Folders; then drag Rename Finder Items into the work area on the right, below the existing Extract PDF action.
When you drop the workflow, a dialog will appear asking if you want to first copy the items before renaming. Since you’ll be renaming a just-created file (which can be easily recreated), there’s no point to make a copy, so click Don’t Add.
Modify the action as you wish; I set mine up to add the creation date at the front of the filename, included leading zeros, and changed the separators a bit. The end result will be a series of files that are automatically sorted by creation date, even when viewed in alphabetical order.
After renaming, the final step is to move the file to its final destination. In my case, that’s to a folder named _Extracted PDFs on my Desktop.
In the Files & Folders column in the Library, drag the Move Finder Items action to the bottom of your workflow. Click the To pop-up menu, and navigate to the folder where you’d like to save the files; select Other if it’s not one of the predefined destinations. Click once on the target folder, then click the Choose button to set that folder as the destination.
And that’s it for the Service—three simple steps to extract text from PDFs, rename the resulting files, and move them to their final destination. Select File -> Save, give your new Service a memorable name (Extract text from PDF, for instance), and click Save.
Using the Service
Once you’ve created your service, using it is as simple as could be. In Finder, select one or more PDFs, then Control-click on the selection to display the contextual menu. Scroll to the bottom of the contextual menu and select Services -> Extract text from PDF (or whatever you named the Service).
Wait a second or two (or longer, if you’ve chosen a huge PDF), then navigate to your chosen folder. There you’ll find one text file for each PDF you had selected, each containing the text from that PDF.
This is a very simple example, but I hope it gives you a sense for what’s possible with Services in Snow Leopard.
The ability to create your own Services—or for others to create distributable Services—should lead to an abundance of available tools to help you with those tasks you do on a repetitive basis. Personally, I’m keeping my eyes peeled for the surely-in-development “Write weekly Macworld blog entries” Service. Until it arrives, though, I’ll continue writing them myself.