Inspired by a recent episode of The Mac Cast I decided to see if I could come up with a simple way of getting a word count of a PDF on OS X using only tools that come standard with the OS.

Because of OS X’s Unix underpinnings, all Macs have access to the Unix wc command which calculates word counts on given input. OS X also has a handy built in Terminal command to access the contents of the clipboard (pbpaste). This leads to an obvious simple manual solution:

  1. Open the PDF in Preview
  2. Select All Text
  3. Copy to clipboard
  4. Run the Terminal command: pbpaste | wc -w

This is a bit cumbersome though, so I went on to create a simple OS X Service to calculate the word count of any selectable text in any app (the fact that this is even possible, let alone easy, is why I love OS X).

For those of you just looking for a copy of the Service, you can download it here:


To install the service simply extract the automator file from the ZIP archive and copy it into either the Library/Services folder in your home directory, or the system-wide service folder /Library/Services.

Once the Service is installed you can use it in almost any OS X app (specifically in any app written using the standard Cocoa libraries) by selecting some text, right-clicking on it, and selecting the Word Count service:

Right Click Selected Text to Invoke the Service

When done the results will look something like this:

Sample Output

Those of you who want to see how easy this Service was to write, read on and I’ll walk you through it.

The Automator app that comes free with OS X makes it easy to create system-wide services. Lets start by launching that app, and selecting Service from the list of possible action types to create.

Using the drop-downs at the top of the workflow, set the input to Service receives selected Text in any application.

Next add a Copy to Clipboard action into the workflow, and then add a Run Shell Script action below that, within the Run Shell Script action set the shell to /bin/bash.

Then paste the following code into the body of the Run Shell Script action:

word_count=`/usr/bin/pbpaste | /usr/bin/wc -w`
/usr/bin/osascript -e "tell app \"System Events\" to display alert \"Word Count: $word_count\""

The first line calculates the word count of the contents of the clipboard and saves it to a variable, and the second line uses Apple Script to display the calculated word count in a dialog box.

All you have to do now is save the action and give it a name, then it’s ready to use!

We can do a little better though – why only show the word count, why not list the character count and line count as well? OK, lets do that, alter the contents of the Run Shell Script to the following and save:

char_count=`/usr/bin/pbpaste | /usr/bin/wc -m`
word_count=`/usr/bin/pbpaste | /usr/bin/wc -w`
line_count=`/usr/bin/pbpaste | /usr/bin/wc -l`
/usr/bin/osascript -e "tell app \"System Events\" to display alert \"Character Count: $char_count\nWord Count: $word_count\nLine Count: $line_count\""

When you’re done your Automator workflow should look like this:

Workflow Screenshot (click to enlarge)
Click to Enlarge