Cedeq Community Forum

Full Version: Keyboard Shortcut - Get a Word Count of Selected Text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Have you ever had to copy and paste text to Microsoft Word just to get a word count? If so, this AutoHotkey script may interest you. It counts how many words are in a selected portion of text (from a website, document, or other), pretty much like the Word Count function in MS Word.

The script counts the number of non-whitespace character groups. The count result is supposed to be the same as MS Word (2010) in English and French languages. So this script will count "It’s AutoHotkey-based" and "Bonjour !" as two words.

Need help creating a keyboard shortcut with ShortKeeper using the following AutoHotkey code? This 5-minute tutorial will help!
ClipSaved := ClipboardAll
Clipboard =
SendInput, ^c
ClipWait, 2
if ErrorLevel
  MsgBox % "Failed attempt to read selected text."
  NewClipboard := Trim(Clipboard)
  Count := 0
  Position := 1
  while, Position := RegExMatch(NewClipboard, "\S+", Out, Position+Strlen(Out))
  MsgBox % "Words :" . Count
;Clipboard := ClipSaved

The script replaces the clipboard content with the selected text. The last line of code (#18) can be uncommented (delete ';' at the beginning of the line) to restore the clipboard with its previous content.

This script is optimized to be used with ShortKeeper. As such, there is no "Return" at the end and variable(s) don’t need to be cleared. ShortKeeper will do this automatically. Just copy/paste the code in the PARAM field in ShortKeeper, set your hotkey, save, and you’re ready to use your new keyboard shortcut.