Make a Word & Character Counting Service for All Apps in Mac OS X
While some writing and text apps have native word and character counters as part of their built-in feature set, not every app does. That can be changed easily though by creating a Mac OS X Service that will provide word counts and character counts from just about anywhere on a Mac that text is selectable. This means apps like TextEdit, the Quick Look preview panels, and even web browsers like Safari, Chrome, Firefox, all will gain the word/character counting feature that is easily accessible from a right-click. This is probably the best method of gaining a word counter into Mac apps that don’t support the feature natively.
A Mac OS X Service is created using the scripting app Automator, bundled in all versions of Mac OS X. If you’re new to Automator and the idea of scripting sounds overwhelming, don’t worry, this is a extremely simple to set up, uses prewritten code, and requires no specific ability to write scripts.
How to Make the Word & Character Counting Service for Mac in Automator
We’re going to create an Automator Service using an AppleScript created by a user named ‘nslater‘ on Github. It’s already short and concise and thus there’s no need to reinvent the wheel or rewrite it ourselves.
- Launch the Automator app, found in the /Applications/ folder of Mac OS X (or open it with Spotlight / Launchpad
- Select “Service” at the splash screen of Automator
- Select into the search box on the left side of the Automator window, and type “run applescript”, then drag and drop that action into the right side window
- Copy and paste the following AppleScript code from GitHub into the “Run AppleScript” form:
- Save the service and name it something fairly short, like “Count Characters & Words”
on run {input, parameters}
tell application "System Events"
set _appname to name of first process whose frontmost is true
end tell
set word_count to count words of (input as string)
set character_count to count characters of (input as string)
tell application _appname
display alert "" & word_count & " words, " & character_count & " characters"
end tell
return input
end run
Now that you’ve created the service and saved it, let’s test it out to make sure it works. There shouldn’t be any issues, but if you encounter any error it’s likely because the copying and pasting of the above code went wrong with character translation, thus you may want to try copying the code directly from the GitHub page of nslater if it isn’t running properly.
How to Use the Word & Character Counting Service in any Mac OS X App
Now that the service has been created, you can use it just about anywhere in Mac OS X that text or words are selectable. This doesn’t have to be a text editor or entry field, it can be raw text in non editable documents, web pages, or Quick Look windows, the only necessity is that the text is selectable. Here’s how it works:
- Launch a text file in an app like TextEdit
- Either select all the text, or just a sample of the text, and right-click (control+click) the mouse button
- Go to the “Services” menu and choose the ‘Count Characters & Words’ option
- Find the word and character count of the selected text in a popup window like so
Now that you’ve confirmed the word counter service to be functioning as intended, you can go ahead and quit out of Automator. The Service will remain intact.
The Service should carry over immediately to all apps through Mac OS X, but you may want to relaunch some apps if you don’t see the character and word counter option available in the Services menu. (side note: you can also use Terminal to killall SystemServices but that can have unintended side effects, thus we won’t specifically recommend it).
If you decide you don’t want this service, you can easily remove it and others from the Services menu of Mac OS X by heading over to an unlikely place in System Preferences.
Neat. Is there a way to exclude specific characters like [line feed] in the character count?
This worked great! Thanks!!
To Rich Gordon above, I don’t get a services menu either, but I do see the name I gave to the service as a choice. I saved the service as “Character & word count” and when I select the text in Safari and right click on it, “Character & word count” is one of the choices from the right click menu that comes up. I hope this helps!
Seems like a great idea, and I successfully saved the script. But when I right-click, I don’t see a “Services” menu. Any idea why?
Exactly what I needed .. many thanks!
This is great, thank you so much, it will help me with Twitter.
fantastic. exactly what I needed. amazing apple did not think of this… thank you very much!
Excellent – much appreciated.
Best wishes,
Peter deP
10:41h on Tuesday,
14 October 2014
I’m having the same problem as other Snow Leopard users – I get the response “The action “Run AppleScript” encountered an error.”.
I note that in ~/Library/Services, the resulting Automator file is called ‘Word & character count.workflow’. The other two services I have there end in ‘.service’.
Is this of any note?
Very nice!
For the people who are having problems, after you paste the script into the Automator window is the text shown in uniform red?
If it is, click on the little hammer icon just below “Run AppleScript”: this should force AppleScript to parse the script. You’ll know when this has happened, because the words in the script change from uniform red to a mixture of black, blue and green. Only save the script when this is done.
This suggestion might be totally absurd, but it’s the only way that I could see anything going wrong.
Nice hint. I modified it to give me a paragraph count as well.
on run {input, parameters}
tell application “System Events”
set _appname to name of first process whose frontmost is true
end tell
set word_count to count words of (input as string)
set character_count to count characters of (input as string)
set myParas to count paragraphs of (input as string)
tell application _appname
display alert “” & word_count & ” words, ” & character_count & ” characters,” & myParas & ” paragraphs”
end tell
return input
end run
Great idea!
I should have mentioned the freeware service “Word Services” from devontechnologies.com, (http://www.devontechnologies.com/download/products.html).
Word Services gives mutant superpowers to what’s outlined in this hint.
I’m not a computer person by any stretch but I made sure (after I pasted in the info) that my screen looked exactly like the sample. There is a line at the top that wasn’t included in the paste, so I typed it in. Also a couple of words at the beginning and end of the paste were duplicated, so I deleted those duplicates. AND everything worked as stated. I have Lion.
done as pointed out, but nothing happens.
Maybe it’s just because I’m on 10.6.8 in italian?
I’m (see above) using 10.6.8 in English, Russian and Estonian and I’ve altered my keyboard shortcut, cmd+space, to switch keyboard layouts. I wonder if that has anything to do with it?
The script works with OS X Lion, Mountain Lion, and OS X Mavericks, but I don’t have a Snow Leopard machine to try it out on. Try copying and pasting the raw script from here:
https://gist.github.com/nslater/1616556/raw/
Be sure the only action in Automator is “Run Applescript” and save it as a Service. Don’t try to run it in Automator without saving it as a Service and accessing it from the Services menu, it won’t work in Automator because text hasn’t been selected.
Nope, that doesn’t do it, either. (I had actually tried copying the code directly from the raw source before posting initial report, but I’ve tried it again now anyway.) Saved in Automator and tried accessing through Services in TextEdit, but the error message is the same: “The action “Run AppleScript” encountered an error.” When ‘Show Workflow’ is selected Finder opens on the file saved by Automator to the folder [user]>Library>Services. I’d really like to get this sorted out, because otherwise I’ll need to continue opening Pages and pasting from web forms to compose, e.g. within limits of e-bay replies, which is a nuisance. Any other ideas? Please help.
done as adviced. Still no way.
Probably because we’re running Snow Leopard, Andrea. Looks like that’s all the support we’re going to get. (Make of it what you will.)
Great little script It’s particularly good for Twitter
Hasn’t worked for me, either, I’m afraid; did everything to the letter, and then went to https://gist.github.com/nslater/1616556/ when it wouldn’t work, but just kept getting the same message:
“The action “Run AppleScript” encountered an error.”
Facing the same problem, I wrote a utility app to automatically count words in any text copied to clipboard. Displays word count in menu bar.
WordCounter, a free app for Mac OS X 10.6+, available here:
https://www.onekerato.com/1/post/2014/01/introducing-wordcounter-free-mac-os-x-app-for-counting-words-in-copied-text.html
Brilliant, simple, and very useful. Thanks for this!
Uhh…I did this exactly as told, and I selected this article and ran the script…only to have Google Chrome open in Parallels Desktop. Am I…missing something?
LOL, it sounds as if something went very wrong with your implementation. Take a screen shot of your code and share it in the comments.