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

Use AppleScript to count words and characters in text Apps
Here's a quick way to count the number of characters, words or paragraphs in some text, without having to launch a full-blown third party application. First, copy the text you wish to count to the clipboard, then open Script Editor (in Applications -> AppleScript), and then enter any one of these AppleScript commands (the lines that start with -- are comments; you don't need to enter those):
-- count all characters
count (the clipboard)

-- count all the words
count words of (the clipboard)

--count the paragraphs
count paragraphs of (the clipboard)
Press the Run button to execute the code you have written. The result will be displayed in the lower half of the window. If you want to check some other text, copy it to the clipboard and simply press the Run button again.

[robg adds: I took this one step further, and wrote a simple application that will display a dialog with the results. Here's the code:
set myCount to count (the clipboard)
set myWords to count words of (the clipboard)
set myParas to count paragraphs of (the clipboard)

display dialog "Characters: " & myCount & "
Words: " & myWords & "
Paragraphs: " & myParas
Note that there are line breaks entered after the & " at the end of two of the lines; those are there to make the dialog box easier to read. Save the code as a program, and you're set. To use the program, copy some text to the clipboard then launch the program. A dialog box will appear showing the counts for characters, words, and paragraphs. I'm sure there are more elegant ways to do this; I'm a total AppleScript novice.]
    •    
  • Currently 2.89 / 5
  You rated: 3 / 5 (9 votes cast)
 
[27,910 views]  

Use AppleScript to count words and characters in text | 15 comments | Create New Account
Click here to return to the 'Use AppleScript to count words and characters in text' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use AppleScript to count words and characters in text
Authored by: spicyj on Jul 31, '07 07:57:08AM
Why not use wc? Type pbpaste | wc in terminal, and get the lines, words, characters count.

[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: gshenaut on Jul 31, '07 08:22:55AM
Just what I was going to say, but you could still do it in Applescript with do shell script "pbpaste | wc"

[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: osxboy on Jul 31, '07 09:54:16AM

You could add

alias wcp='pbpaste | wc' to you're bash profile (.bash_profile)

Then copy some text and type wcp in the terminal



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: Fairly on Jul 31, '07 02:31:19PM

Yeah exactly. Why make it difficult when it's so easy. But good on you, robg! That's a nice script too.



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: MrLucky on Jul 31, '07 10:48:58AM

Nice!

I've always wanted a word counter specifically for TextEdit, so I added the following and saved it as a script (the first bit tells the system to copy the selected text so I don't have to strain my poor, overused Cmd+C fingers). Then, I used FastScripts to make a keyboard shortcut for it.

tell application "System Events"
tell process "TextEdit"
set frontmost to true --> bring app to the foreground
click menu item 5 of menu 1 of menu bar item 4 of menu bar 1 --> copy
end tell
end tell

set myCount to count (the clipboard)
set myWords to count words of (the clipboard)
set myParas to count paragraphs of (the clipboard)

display dialog "Characters: " & myCount & "
Words: " & myWords & "
Paragraphs: " & myParas



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: wallybear on Aug 01, '07 04:48:50AM

Lots simpler:

tell application "TextEdit"
set ch to count characters of document 1
set wd to count words of text of document 1
set par to count paragraphs of text of document 1
display dialog "Characters: " & ch & return & "Words: " & wd & return & "Paragraphs: " & par buttons "OK"
end tell

This will do statistics for the first document window opened in TextEdit, with no need of using clipboard.
Or you can use the "Statistics..." service in Services menu; curiously enough, count by applescript and count by Statistics don't match.



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: wallybear on Aug 01, '07 04:54:40AM

And replacing "document 1" with "front document", you'll get the statistics for the document you're currently working on:

tell application "TextEdit"
set ch to count characters of front document
set wd to count words of text of front document
set par to count paragraphs of text of front document
display dialog "Characters: " & ch & return & "Words: " & wd & return & "Paragraphs: " & par buttons "OK"
end tell



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: wallybear on Aug 01, '07 04:58:43AM

Sorry to keep posting replies to myself, but I just noted you can slightly simplify the script removing the words "of text" from the script, as they are superfluous.



[ Reply to This | # ]
Another Version
Authored by: micmoo on Jul 31, '07 12:01:36PM

I am a TOTAL applescript n00b. I just started a few days ago. I made this one:

set Entry to display dialog "Enter Text" default answer ""
set textEntry to text returned of Entry

set myCount to count (textEntry)
set myWords to count words of (textEntry)
set myParas to count paragraphs of (textEntry)
display dialog
"Characters: " & myCount & "
Words: " & myWords & "
Paragraphs: " & myParas

It sorta seems pointless cause you would copy and paste it anyway, but what the heck. Don't flame saying this isn't useful cause its not. I just figured I would make it. Enter any text and it counts it words, characters, paragraphs.



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: NovaScotian on Jul 31, '07 12:36:22PM

A version that counts what in a file:

set f to (choose file with prompt "Choose a text file" without invisibles)
tell application "Finder" to open f -- brings it to front if open
delay 0.5 -- may have to be lengthened if doc is not open.
tell application "System Events" to tell window 1 to keystroke "ac" using command down -- command a is "all", command c is "copy".
tell (the clipboard) to set {char, wds, para} to {count characters, count words, count paragraphs}



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: soundsgoodtome on Jul 31, '07 04:45:55PM

Or you could choose Services>Statistics…, and call it a day....



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: n1mie on Aug 01, '07 06:56:45AM

Sounds great, except that the mentioned service does not appear in my menu. There is a "summarize" one, but it is dimmed in TextEdit, BBEdit, and Word. Don't know, but the AppleScript and the command line methods seem pretty useful, each in it's own right, to me.

---
--Chip



[ Reply to This | # ]
About Statistics Service Mentioned Above
Authored by: tarmer on Aug 01, '07 08:24:33AM
The statistics service mentioned in several posts is part of the free WordService package from Devon Technologies. It's available from the free software section at the bottom of page on their download page: http://www.devon-technologies.com/download/index.html. Once you've installed it, logged out and back in, you can highlight sections of text then after selecting Statistics from your service menu you are provided with the total number of characters, spaces, words and lines. BTW the WordService package includes tools for formatting and converting text. Hope this helps!

Toby Wilson

[ Reply to This | # ]
About Statistics Service Mentioned Above
Authored by: osxpounder on Aug 01, '07 01:12:09PM

That service looks like it'll be useful for word and character counts. Thanks! I haven't logged out/in to try it yet.

I'm checking out the ReadMe.rft file that comes with it, and I notice that it has one feature that could trip you up, if you're writing in English using the official MLA style [used by literature and language teachers and writers. It's probably not important unless you need to strictly follow MLA rules, but if the rules are important, don't use that "Initial Caps Of Sentences" feature. APA and Chicago Style rules are different.

In MLA style, we are not supposed to capitalize after colons [:] unless the text that follows could be a sentence on its own [not a sentence fragment].

We don't capitalize after semi-colons [;] because it's understood already that the semi-colon connects 2 independent sentences. It's weird, I know, but this is the kind of stuff that gets penalized in scholarly writing, and in publications, especially if you're student working for course credit.

We also do not capitalize after a 3-dot ellipsis [... ], when we're quoting part of someone else's text, because that's used to show something was taken out of the same sentence, on both sides of the little dots... the text that follows the 3-dot ellipsis is still part of the same sentence. Thus, you don't capitalize; that would make it seem like there was more than one sentence being quoted.

When you're quoting more than one sentence, and you're cutting out stuff by using the ellipsis "between sentences", you use a 4-dot ellipsis [.... ]. That shows everyone that the quoted bit came from a sentence, but we aren't seeing the end of that quoted sentence. The difference between 3-dot and 4-dot ellipses are important in journalism, because when you quote someone and do the ellipses wrong, you're misquoting them.

Always use a space after an ellipsis.



[ Reply to This | # ]
Use AppleScript to count words and characters in text
Authored by: robleach on Nov 07, '14 08:26:34AM
Just did this for any selected text (without having to copy)...

set debug to false

--Get the highlighted text
set selecTxt to getHighlight(debug)

set myCount to count (selecTxt)
set myWords to count words of (selecTxt)
set myParas to count paragraphs of (selecTxt)

display dialog "Characters: " & myCount & "
Words: " & myWords & "
Paragraphs: " & myParas


on getHighlight(debug)
	
	--Save the current contents of the clipboard
	set theSpare to the clipboard as record
	--Save a text version of the contents of the clipboard if possible
	set spareTest to ""
	try
		set spareTest to the clipboard as text
	end try
	--Declare the variable we're going to return
	set selecTxt to ""
	
	tell application "System Events"
		--Initiate the copy
		keystroke "c" using {command down}
		
		--Wait up to 2 seconds for the copy to finish
		set done to "no"
		set waitnum to 0
		set waitInterval to 0.02
		set maxwaits to 100
		
		--Repeat while the clipboard contents have not changed
		repeat while done = "no"
			--Get the contents of the clipboard
			set selecTxt to the clipboard as text
			
			--See if we're done or need to wait
			if waitnum is equal to maxwaits then
				set done to "yes"
			else if spareTest is equal to selecTxt then
				delay waitInterval
				set waitnum to waitnum + 1
			else
				set done to "yes"
			end if
			
		end repeat
		
		if debug is true then
			display dialog "Copied text: " & (the clipboard as text)
		end if
	end tell
	
	--Restore the original clipboard contents
	set the clipboard to theSpare as record
	
	--Return the highlighted text
	return selecTxt
	
end getHighlight


[ Reply to This | # ]