Friday, August 22, 2014

How To Get Your Mac To Count Highlighted Words

Earlier today, I was looking for a way to add the ability to count how many words were highlighted on my Mac. So, after a quick Google search, I came across one implementation on the Macworld forums by guillaumegete. I'll admit, presentation wise, it's not the greatest but it does what I want, it counts the number of characters, signs and paragraphs that you have highlighted.

For anyone wondering, I'm currently running OS X Mavericks 10.9.4 and according to the forum post, this implementation has been tested OS X 10.6 and 10.7.

To add the feature, launch Automator on your Mac, select Service, search for "Run AppleScript" and then add the following code:
on run {input, parameters}
          set MyText to input as string
          set NombreSignes to the number of characters of MyText
          set NombreMots to the number of words of MyText
          set NombrePara to the number of paragraphs of MyText
          set LeResultat to "The selected text contains :" & return & "- " & NombreSignes & " sign(s) ;" & return & "- " & NombreMots & " word(s) ;" & return & "- " & NombrePara & " paragraph(s)."
          display dialog LeResultat 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
Save the Service and name it whatever you'd like, I called it "Character Count". To use the Service, highlight some text, right click, hover over "Services" and select (in this case) "Character Count".