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

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

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.

Count Words, Characters and Paragraphs With A Roll Your Own Automator Service [OS X Tips]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.

Got an OS X tip? Need help troubleshooting OS X? (sorry, you need Javascript to see this e-mail address)or leave a comment below.

About the author

Rob LeFebvreAnchorage, Alaska-based freelance writer and editor Rob LeFebvre has contributed to various tech, gaming and iOS sites, including 148Apps, Creative Screenwriting, Shelf-Awareness, VentureBeat, and Paste Magazine. Feel free to find Rob on Twitter @roblef, and send him a cookie once in a while; he'll really appreciate it.

(sorry, you need Javascript to see this e-mail address)| Read more posts by .

Posted in OS X, Tips & Tricks | Tagged: , , , , , , |