Build a service to count characters, words and paragraphs

May 01, '12 07:30:00AM

Contributed by: guillaumegete

Apple does not provide any way to count the number of characters in a selected text. Fortunately, you can create your own very easily using Automator.

Launch Automator and create a new Service. Add the Run AppleScript script action, then paste the following code:

on run {input, parameters}
	try
		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
Now save the service and use a name such as "Count characters in selection."

To use the service, select any text in a text application (this does not work in Word, however), then choose your service in the contextual menu.

You can download a precompiled service here. This has been tested successfully on Mac OS X 10.6 and 10.7.

[kirkmc adds: This works as advertised. For some reason, however, when I install the precompiled service, while its name is "Count characters in selection," it displays in the Services menu as "Compteur de signes," which is French for character counter. If you're unfamiliar with using services in OS X, you can see this Macworld article I wrote earlier this year.

We ran a hint using AppleScript to count words and characters back in 2007, which uses a different approach, requiring that you copy text to the clipboard.]

Comments (18)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20120428125918199