Count Words, Characters and Paragraphs With A Roll Your Own Automator Service [OS X Tips]

By

WordCountService

As a writer, I need to know the number of words in my textual musings on a fairly regular basis. I’m sure many of you might have the same need, if even to count the characters in those funny Tweets you’ve been thinking about for weeks. Today’s tip should help you out, in a super cool DIY style.

If you’ve used Mac OS X for a while, you’re familiar with Services. If not, it’s both a menu item in the Application menu and a contextual menu item. To see the Services menu, simply click on the Application menu (in Safari, it’s the Safari menu, in Chrome, it’s the Chrome menu, etc.) just to the right of the Apple menu.

To create your own Service, open Automator from the Applications folder; it’s a Mac app that comes with every copy of OS X. Once launched, choose Service (the icon looks like a big gear), and then click the Choose button. Then, click on Utilities in the left hand pane, then Run Applescript in the pane to the right. You can also type Applescript into the search field at the top left of the window to find the “Run Applescript” service creator. Drag the Run Applescript icon to the pane on the right, and it will create a blank template for you. Then, type or paste the following script in, completely replacing the template text:

on run {input, parameters}
try
set MyText to input as string
set NumberChars to the number of characters of MyText
set NumberWords to the number of words of MyText
set NumberParag to the number of paragraphs of MyText
set TheResult to "The selected text contains :" & return & "- " & NumberChars & " character(s) ;" & return & "- " & NumberWords & " word(s) ;" & return & "- " & NumberParag & " paragraph(s)."
display dialog TheResult buttons {"OK"} default button 1 with icon note
on error errmsg number errnum
display dialog errmsg & " [" & errnum & "]" buttons {"OK"} default button 1 with icon stop

end try
return input
end run

In essence, this script creates five variables, MyText to bring in highlighted text, NumberChars, NumberWords and NumberParag to count the text items, and TheResult to create a user-readable dialogue box.

Save this Service file as WordCount (or any other name you like) and quit out of Automator. Head over to any block of text (you can even use this post if you like) and highlight it. Then right click on it, choose the Services menu from the resulting contextual pop-up, and then choose WordCount. A dialogue will show up with the number of characters, words, and paragraphs.

It’s fairly easy to customize this script to your needs, as well. If you delete any one of the variables above, it won’t show up in your dialogue box. For example, if you only want a word counter, delete the lines:

set NumberChars to the number of characters of MyText
set NumberParag to the number of paragraphs of MyText

and change the TheResult line to this:

set TheResult to "The selected text contains :" & return & "- " & NumberWords & " word(s)."

Let us know how it works for you in the comments below.

Source: Mac OS X Hints

Got an OS X tip? Need help troubleshooting OS X? Drop me a line or leave a comment below.

Newsletters

Daily round-ups or a weekly refresher, straight from Cult of Mac to your inbox.

  • The Weekender

    The week's best Apple news, reviews and how-tos from Cult of Mac, every Saturday morning. Our readers say: "Thank you guys for always posting cool stuff" -- Vaughn Nevins. "Very informative" -- Kenly Xavier.