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


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: 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 | # ]