Cedeq Community Forum
Keyboard Shortcut - Get a Word Count of Selected Text - Printable Version

+- Cedeq Community Forum (https://cedeq.com/forums)
+-- Forum: General (https://cedeq.com/forums/forumdisplay.php?fid=1)
+--- Forum: Keyboard shortcuts with AutoHotkey code (https://cedeq.com/forums/forumdisplay.php?fid=4)
+--- Thread: Keyboard Shortcut - Get a Word Count of Selected Text (/showthread.php?tid=17)



Keyboard Shortcut - Get a Word Count of Selected Text - denilama - 03-07-2018

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."
}
else
{
  NewClipboard := Trim(Clipboard)
  Count := 0
  Position := 1
  while, Position := RegExMatch(NewClipboard, "\S+", Out, Position+Strlen(Out))
    Count++
  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.