Script to count and say count for TextEdit

I prefer to work with plain text, rather than open a huge word processing program, like Word, for almost all drafting and notes. Since I’m using a mac, and I have to eventually transfer most work into Word, I currently love TextEdit (well, I prefer LaTeX, but we can’t have everything).

Because of its focus on plain text and no features, TextEdit lacks the word count feature of Word. Sometimes, like when you are submitting an abstract; okay, so usually this happens when you are submitting an abstract, you really, really need to be aware of word counts. After much searching around on the web, I encountered a forum where some other folks were lamenting the same thing -> at MacWorld. And, some other super helpful people (specifically mprussell and belleboom) had already written scripts that will overcome this “problem.”

I’ve posted their two great scripts below. Just copy and paste them (one at a time if you want both) into your AppleScript Editor (you can find it under [Utilities]. Save it in Library-> Scripts -> Applications->TextEdit ; you may have to create this path with folders.

The first is a no-frills “count the text and then show it in a pop-up” script. The second will count the text of the highlighted selection of text as well as the text in the whole document and say it out loud, using Alex. I prefer the second one, and you can comment out the speech part if you don’t like it. It will also go faster if you comment that part out of the script.

Script #1.

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

Script #2.

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

One thought on “Script to count and say count for TextEdit

Leave a Reply

Your email address will not be published. Required fields are marked *