Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Build a service to count characters, words and paragraphs System 10.6
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.]
    •    
  • Currently 3.48 / 5
  You rated: 2 / 5 (33 votes cast)
 
[10,284 views]  

Build a service to count characters, words and paragraphs | 18 comments | Create New Account
Click here to return to the 'Build a service to count characters, words and paragraphs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Build a service to count characters, words and paragraphs
Authored by: OldUnixGuy on May 01, '12 08:49:13AM

"Apple does not provide any way to count the number of characters in a selected text." As any UNIX geek knows, this is simply not true. Launch Terminal and type "man wc".

Kevin



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: kirkmc on May 02, '12 01:03:54AM

wc only works with text files. This service works on selected text in any application.

---
Mac OS X Hints editor - Macworld senior contributor
http://www.mcelhearn.com



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: OldUnixGuy on May 02, '12 06:37:04AM

This is true, but the statement that Apple does not provide "any way" to count text in a file is still false.

Kevin



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: kirkmc on May 02, '12 07:08:22AM

They don't provide "any way" to count text in all kinds of files. Sorry, no need to pick nits. If you work with .txt files, that's fine, but most people do use other formats too.

---
Mac OS X Hints editor - Macworld senior contributor
http://www.mcelhearn.com



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: tofergregg on May 02, '12 04:00:04PM
Here's what I do when I want to count something that isn't in a text file:

  1. select the text and copy it.
  2. open Terminal and type wc.
  3. paste the text.
  4. type ctrl-D.

This prints out lines / words / characters of the selected text.

[ Reply to This | # ]

Build a service to count characters, words and paragraphs
Authored by: newscientist on May 01, '12 09:49:34AM
Here's the script without the French with added checks for single words, characters, etc.

on run {input, parameters}
	try
		set MyText to input as string
		set numChars to the number of characters of MyText
		set numWords to the number of words of MyText
		set numPara to the number of paragraphs of MyText
		set theResult to "The selected text contains:" & return & "- "
		
		if numChars is 1 then
			set theResult to theResult & numChars & " character;" & return & "- "
		else
			set theResult to theResult & numChars & " characters;" & return & "- "
		end if
		
		if numWords is 1 then
			set theResult to theResult & numWords & " word;" & return & "- "
		else
			set theResult to theResult & numWords & " words;" & return & "- "
		end if
		
		if numPara is 1 then
			set theResult to theResult & numPara & " paragraph."
		else
			set theResult to theResult & numPara & " paragraphs."
		end if
		
		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


[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: deef on May 01, '12 11:34:35AM

The free WordService from Devon does this, plus a lot more: http://www.devontechnologies.com/products/freeware.html



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: digs0 on May 01, '12 01:37:16PM

Nifty hint. It shows up as "Count Characters in Selection" for me.

---
DJR



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: jdpanoptic on May 01, '12 02:42:54PM

You can also download Wordservice.zip from Devon Technologies which does the same thing.

http://www.devontechnologies.com/download/products.html



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: jgateau on May 01, '12 09:34:01PM

Is there a reason why the (included) bsd util "wc" is insufficient for this task? I guess it doesn't capture paragraphs, but gets words, characters and lines...



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: mmnw on May 02, '12 01:52:29AM

Yes, but it only works on saved text files. This hint works on selected text in any Application supporting OS X Services. This is definitely an advantage over wc.



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: Wuffel on May 02, '12 06:04:48AM

Might the language issue be the result of the commands being in French? I'm guessing it could trigger the results to come up in French too...



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: mike666 on May 02, '12 12:44:07PM

That would be bizarre behavior if true, since the French terms in the original script are all variables. That would imply if you used gibberish names for variables (i.e. "set sjdUnfjkss to input as string" - which is valid) the title would come out as "Doksnj Elanvbdj Croslkd" :D



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: mike666 on May 03, '12 06:20:33PM
I think it would be safe to assume that someone named "Guillaume Gete" probably has his OS X install localized to Français; even though he programs his Applescript in English, he probably named the service to blend in with his environment when he compiled it. Not even sure if Applescript itself has been localized to other languages. If not, then shame, shame on Apple!

[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: murgatroyd on May 08, '12 04:49:29AM
Years past I wrote a tome with over 150,000 words. I wanted to know how many UNIQUE WORDS I hand employed in my masterpiece. For instance, of those 150,000 words there are 13,546 'the' words, 17,004 'and' words, 11,098 'for' words, etc., employed. I wrote an AppleScript:
(*
	the script functions till the final counts are displayed in Results panel, 
		then errors attempting to display properly.
	Applescript by Hanaan Rosenthal is being checked out today from SIBL, NYPL.
*)

set xrk to true as boolean

set {ten, kIndex, pk} to {10 as integer, 0 as integer, 0 as integer}

set {k24, k, k1, k11} to {"", {}, {}, {}}

set {wordIndex, h, kCount} to {0 as integer, 1 as number, 0 as integer}


set pathDM to (path to desktop folder as string) -- & "UniqueWords:")
set pathToDM to (pathDM & "treatise1.txt") as string
set kFile to (pathDM & "folder1:file1") as string

tell application "TextEdit"
	tell document 1
		set ParagraphCount to count (get paragraphs of it)
		
		--if file pathToDM exists then
		--activate it
		(*	else
		open pathToDM as document type text ¬
			with password "4DearMergatroid!"
	end if
*)
		
		log "Paragraph Count = " & ParagraphCount
		if ParagraphCount is less than 10 then set h2 to 1 as integer
		if ParagraphCount is greater than or equal to 10 and ParagraphCount is less than 50 then set h2 to 5 as integer
		if ParagraphCount is greater than or equal to 50 and ParagraphCount is less than 500 then set h2 to 10 as integer
		if ParagraphCount is greater than or equal to 500 then set h2 to 15 as integer
		
		set textReturn to false
		repeat while not textReturn
			try
				display dialog "Start counting " & return ¬
					& "from what numbered paragraph ...?" default answer "1" buttons {"cancel", "set"} ¬
					default button "set" with icon note
				set h to text returned of result as integer
				set textReturn to true
			on error
				display dialog ¬
					"Bye-bye" giving up after 1
				--close document pathToDM
				return
				
			end try
			
		end repeat
		if textReturn is false then display dialog "Bye." giving up after 2
		
		-- preliminaries taken care of, start the ball rolling ...	
		
		repeat while xrk is true
			if (h + h2) is greater than ParagraphCount then
				set h3 to ParagraphCount as integer
				set xrk to false
			else
				set h3 to (h + h2) as integer
			end if
			
			set k11 to paragraphs h thru h3
			
			repeat with kword in k11
				set k1 to k1 & (characters of kword) as string -- all characters in k1
			end repeat
			set k11 to {""}
			set wordIndex to (count words of k1) -- count the words in string k1
			
			set kIndex to (kIndex + wordIndex)
			try
				repeat with j from 1 to wordIndex
					if k is {""} then set k to word j of k1
					
					if word j of k1 is not in k then
						set end of k to word j of k1
						-- log " This is k -> " & k & " "
					end if
					
				end repeat
				
				set pk to pk + 1
				log "list " & ": " & pk & tab ¬
					& "total words: " & kIndex & tab ¬
					& "unique: " & (count k)
				set k1 to {""}
				set h to (h3 + 1)
				-- set h2 to (h2 + h)
			end try
		end repeat
		
		activate "Appleworks 6"
		
		set kpk2 to ((current date) & return & ¬
			return & "There were " & kIndex & " words counted ..." & ¬
			return & "with " & (count k) & " unique words.") as string
		
		set kpk1 to kpk2 & return & return & ¬
			"A dated record was created " & ¬
			"in UniqueWords:folder1:file1" as string
		
		select menu item 1 of menu item "New" of menu "File"
		make new text at beginning of document 1 with data kpk2
		save document 1 in kFile as file of type("TEXT")
		close document 1
		
		display dialog kpk1 buttons "Ok" default button 1
		
	end tell
end tell


log "Logging off ..." & tab & "byeeeeee."
Now what I was ultimately searching for with the script was a unique word count. Of those 150,000+ words inside the tome, a specific number, say 17,453 words were unique. I never got the script to function. Tomes with unique words under a count of 4096 and the script functions. Above that number and the script will crash. Each unique word has to have its own array … If I recollect correctly the problem was the default number of arrays in AppleScript. Once the number of unique words counted by the script began to tally past …? 4096 was it? the default size limit of an AppleScript array was compromised and the script crashed. I also gave up working on perfecting the script at this time. If anyone is up for a challenge, a UniqueWords script, here's one. Think about it: a marker of one's ability to express themselves. A marker of ones command of the english or any language. The number (and information gleaned) is similar to an IQ score at certain K-12 to the adult stages in ones life. (I'd like half of a Nobel Prize winnings should anyone take this idea to the limit. It was MY idea that got you this fame and fortune, mind you … :)

[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: TvE on May 19, '12 10:28:37AM

What's wrong with "sort -u" to get the unique words and then "wc" to count the actual number - should that not work (once you have each word on a seperate line).

Or have I misunderstood the goal?



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: murgatroyd on May 19, '12 04:07:38PM

Thanks for the quick-thought response but you misunderstood the goal.

---
Someday, Murgatroyd will live …!



[ Reply to This | # ]
Build a service to count characters, words and paragraphs
Authored by: murgatroyd on May 19, '12 04:13:32PM

I am of the belief an AppleScript array was unable hold more than (512 X 8) 4,096 unique numbers. Any larger number of unique words in any tome crashes any AppleScript. It's a software bug with arrays.

---
Someday, Murgatroyd will live …!



[ Reply to This | # ]