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

Simple word and character count for TextEdit Apps
TextEdit is far leaner than other, bulkier text editors or word processors, but it is spartan enough not to have a word count feature. So I wrote one and I thought it might be useful to somebody else.

The following AppleScript adds a word (and character) count pop-up dialog to TextEdit:
tell application "TextEdit"
  set word_count to count words of document 1
  set char_count to count characters of document 1
  set show_words to (word_count as string) & " words. (" & (char_count as string) & " characters.)"
  set dialog_title to "TextEdit Word Count"
  display dialog show_words with icon 1 with title dialog_title buttons {"Ok"} default button "Ok"
end tell
Copy and paste the above into AppleScript Editor (in Applications » Utilities), and save it here: ~/Library » Scripts » Applications » TextEdit. That's the Library folder in your user's directory; I called my script Word Count.

Open AppleScript Editor's Preferences, and on the General tab, turn the 'Show Script menu in menu bar' option on. You'll then have a script icon up there, near your clock.

Now, with any TextEdit document in front of you, you can access your Word Count script from the Script menu in the menu bar. When you do, you should get a small dialog saying something like:
TextEdit Word Count
2832 words. (15060 characters.)
The 'OK' button dismisses the dialog box.

[crarko adds: I tested this, and it works as described.]
    •    
  • Currently 3.68 / 5
  You rated: 4 / 5 (25 votes cast)
 
[76,765 views]  

Simple word and character count for TextEdit | 15 comments | Create New Account
Click here to return to the 'Simple word and character count for TextEdit' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Simple word and character count for TextEdit
Authored by: kirsch on May 06, '10 07:57:44AM

Here's a tip to create a word count service that will work in most Mac OS X applications including TextEdit. I like this solution better because it works everywhere:
http://daringfireball.net/2007/01/word_count_script_for_thisservice



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: PancakeMan on May 06, '10 07:59:35AM

Would it be possible to have the script report counts for both selected and total?



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: ObbieZ on May 06, '10 09:19:06AM

Sorry, this didn't work in Leopard on an Al PBG4. The preference described for Script Editor was not there, restarting TextEdit didn't do it. I seem to remember reading how to do this in the past (pre-Leopard), but I've only succeeded in adding scripts to the menu in iTunes.

Perhaps this is a Snow Leopard-only trick? If so, maybe it should be labelled as such.



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: crarko on May 06, '10 09:46:37AM

I will change it to Snow Leopard only. Thanks!

~CRA



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: robg on May 06, '10 10:00:29AM

Actually, the hint works fine in 10.5. What changed from 10.5 to 10.6 is the spot where you enable the Scripts menu item: you'll find it in Applications » AppleScript » AppleScript Utility in 10.5

In 10.6, they merged the functionality of AppleScript Utility directly into the AppleScript Editor.

-rob.



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: fauxparse on May 06, '10 01:18:22PM

Or you could use the built-in "wc" command-line utility. Set this to "display as tooltip":


<code>wc | awk '{ print $1,"lines,",$2,"words,",$3,"characters"; }'</code>



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: Stormchild on May 06, '10 04:56:30PM

A better solution is to grab WordService, a free OS X service that works in any app. The included 'Statistics' service gives you word/character counts, and it comes with a bunch of other useful text manipulation services as well.

http://www.devon-technologies.com/products/freeware/services.html



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: osxpounder on May 18, '10 01:35:16PM

I use that WordService (on Leopard) and love it. It will convert case of selected text in many apps, too (but not Firefox, sorry to say).

I assigned shortcut keys for lowercase, all caps, initial caps of words, and initial caps of sentences. Works great in TextEdit.



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: belleboom on May 08, '10 05:58:26AM
Thanks for the hint, I've been wanting this function for a while but hadn't gotten around to doing anything about it. Using this and another thread from a couple years back I put together a version that counts selected text (so I can track what I've added recently, rather than necessarily the whole document), and added a title, icon, and customized buttons.

Code is a bit hacky, but works fine for me on a Mac Pro with 10.6.3:
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

tell application "TextEdit"
	set ch to count characters of (the clipboard)
	set wd to count words of text of (the clipboard)
	set par to count paragraphs of text of (the clipboard)
	display dialog "Characters: " & ch & return & "Words: " & wd & return & "Paragraphs: " & par buttons {"sob!", "yay!"} with icon 1 with title "Word Count..."
	
end tell
Edited for width by crarko
Edited on May 13, '10 05:12:48AM by crarko


[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: belleboom on May 09, '10 07:33:33PM
Updated code, once I got through fiddling with it. Includes word, character and paragraph counts by document and selection, and a (commented out) option for a voice report instead of a dialog box... because that's awesome:)
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 selection to clipboard
	end tell
end tell
tell application "TextEdit"
	-- stats for all of document 1
	set word_count to count words of document 1
	set char_count to count characters of document 1
	set para_count to count paragraphs of document 1
	-- stats for selection
	set ch to count characters of (the clipboard)
	set wd to count words of text of (the clipboard)
	set par to count paragraphs of text of (the clipboard)
	-- next two lines announce word totals for selection and document; cool but slows down the dialog display so commented out; use instead of dialog box if you prefer
	--	set alert to "You have " & wd & "words in your selection, and " & word_count & "words in your document"
	--	say alert using "Alex"
	-- open dialog with data, title, icon, close buttonsx2 (same effect for each, can also use Esc for 1 and Enter for 2 to trigger), closes after 20 secs in case you are feeling lazy
	display dialog "Document..." & return & "   Words: " & word_count & return & "   Characters: " & char_count & return & "   Paragraphs: " & para_count & return & return & "Selection..." & return & "   Words: " & wd & return & "   Characters: " & ch & return & "   Paragraphs: " & par buttons {"sob!", "yay!"} default button 2 cancel button 1 with icon 1 with title "Word Count" giving up after 20
end tell
edited for width by crarko
Edited on May 13, '10 05:15:10AM by crarko


[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: Anonymous on May 10, '10 06:41:08PM

I don't know if you looked at this post after you wrote it, but it could use some line wrapping.



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: belleboom on May 11, '10 06:50:20AM

Hmm, right you are and apologies to all, but sadly it seems I can no longer edit the post. How does one insert those useful code boxes in a post?



[ Reply to This | # ]
Simple word and character count for TextEdit
Authored by: toc-rox on May 09, '10 05:28:56AM
Paragraphs added:
tell application "TextEdit"
	set paragraph_count to count paragraphs of document 1
	set word_count to count words of document 1
	set char_count to count characters of document 1
	set show_words to (paragraph_count as string) & " paragraphs" & return & (word_count as string) & " words" & return & (char_count as string) & " characters"
	set dialog_title to "TextEdit Word Count"
	display dialog show_words with icon 1 with title dialog_title buttons {"Ok"} default button "Ok"
end tell
edited for width by crarko
Edited on May 13, '10 05:16:05AM by crarko


[ Reply to This | # ]
Or simply use the free Wordservice:
Authored by: tice on May 09, '10 09:42:40PM
Simple word and character count for TextEdit
Authored by: mmkstarr on Mar 28, '13 11:08:55AM

popup:

Script Menu

An error occurred. OSErr -10828

The script doesn't give me a word count.



[ Reply to This | # ]