Make a Word & Character Counting Service for All Apps in Mac OS X

Jan 27, 2014 - 25 Comments

Word and 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.

  1. Launch the Automator app, found in the /Applications/ folder of Mac OS X (or open it with Spotlight / Launchpad
  2. Select “Service” at the splash screen of Automator
  3. Create an Automator Service

  4. 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
  5. Use the "Run AppleScript" library action

  6. Copy and paste the following AppleScript code from GitHub into the “Run AppleScript” form:
  7. 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

    The word counting script in Automator

  8. Save the service and name it something fairly short, like “Count Characters & Words”
  9. Save Service as Word and Character counter

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:

  1. Launch a text file in an app like TextEdit
  2. Either select all the text, or just a sample of the text, and right-click (control+click) the mouse button
  3. Go to the “Services” menu and choose the ‘Count Characters & Words’ option
  4. count-words-characters-service-menu-mac-os-x

  5. Find the word and character count of the selected text in a popup window like so

The word and character count provided in a pop-up box

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.

.

Related articles:

Posted by: Paul Horowitz in Mac OS, Tips & Tricks

25 Comments

» Comments RSS Feed

  1. Axel says:

    Neat. Is there a way to exclude specific characters like [line feed] in the character count?

  2. Greenie says:

    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!

  3. Rich Gordon says:

    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?

  4. Exactly what I needed .. many thanks!

  5. Jeanette Jones says:

    This is great, thank you so much, it will help me with Twitter.

  6. Matthias says:

    fantastic. exactly what I needed. amazing apple did not think of this… thank you very much!

  7. PdeP says:

    Excellent – much appreciated.

    Best wishes,
    Peter deP
    10:41h on Tuesday,
    14 October 2014

  8. Rod says:

    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?

    • Sam says:

      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.

  9. Diorite says:

    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

  10. Ellen says:

    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.

  11. Andrea says:

    done as pointed out, but nothing happens.
    Maybe it’s just because I’m on 10.6.8 in italian?

    • McD says:

      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?

    • Paul says:

      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.

      • McD says:

        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.

      • Andrea says:

        done as adviced. Still no way.

        • McD says:

          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.)

  12. LensKaraoke says:

    Great little script It’s particularly good for Twitter

  13. McD says:

    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.”

  14. onekerato says:

    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

  15. Scott says:

    Brilliant, simple, and very useful. Thanks for this!

  16. Joe Rizzo says:

    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?

    • p says:

      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.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site