Exclude words when converting to "Title Case" - AutoHotkey Script

Published on
4,625 Points
3 Endorsements
Last Modified:
Joe Winograd
50+ years in computer industry
CIO•Document Imaging
EE — FELLOW 2017
MVE 2015,2016,2018
RENOWNED 2018,2019
GOLD 2020
In a recent question here at Experts Exchange, a member wants to enhance an AutoHotkey script that performs "Title Case" conversion. The enhancement is to allow specification of words that are excluded from the capitalization (conjunctions and prepositions). This article presents such a solution.

In an interesting question here at Experts Exchange, a member asked how an AutoHotkey script that performs "Title Case" capitalization can be enhanced to exclude words from the capitalization, such as the, to, and, from, for, etc. — i.e., specified conjunctions and prepositions. The AutoHotkey code in this article provides such a solution. It works as follows:

• Defines a keyboard shortcut (aka "hotkey") that performs the steps below.

• Operates on the selected (highlighted) text by first copying it to the clipboard via Ctrl+C.

• Capitalizes all words using the AutoHotkey built-in StringUpper command with the T (Title Case) option.

• Reads a simple, plain text file that contains the words to be excluded from capitalization. Each line in the file has one exclusion word on it. A sample exclude file is attached to this article.

• Un-capitalizes the words in the Title that are in the exclusion list, unless it is the first word in the Title, in which case it stays capitalized. For example, this:

the quick brown fox jumps over the lazy dog and a cat simply for fun

becomes this (using the attached exclude file):

The Quick Brown Fox Jumps Over the Lazy Dog and a Cat Simply for Fun

• Puts the converted Title on the clipboard and types it via the SendInput command.

The hotkey in the posted code is Alt+Ctrl+T (with the T standing for Title case), but you may change it to whatever you want. The general format is Modifiers+Key, where the Key may be (nearly) any key on the keyboard (including letters, numbers, function keys, and numeric keypad keys) and the Modifiers may be Alt, Ctrl, Shift, and Win (the Windows logo key), specified as follows:

! (exclamation mark) ==> Alt
^ (caret, circumflex, hat) ==> Ctrl
+ (plus sign) ==> Shift
# (hash mark, pound sign, octothorpe) ==> Win

You may use any number of those modifiers, and they may be in any order, that is, "!^" (Alt+Ctrl) behaves the same as "^!" (Ctrl+Alt). Also, you may use no modifiers. For example, you could have a function key or a numeric keypad key by itself be the hotkey.

The Key names are defined here:
List of Keys

As part of designing this script, I did some web research on capitalization. I thought it would be straightforward — being a bit of a grammarian myself, I should have known better. :)

First, I went after an easy-to-use list of prepositions and found these:


More than I was expecting! The list of conjunctions was much better:


Second, I wanted to be certain of the rules for Title Case capitalization and found this:


Some of the advice there is perplexing, such as do not capitalize with, but do capitalize without. Apparently, the reason for this is the rule that says, "Always capitalize words of five or more letters..." Can't say I buy that one and, indeed, my attached sample exclude file contains several words with five or more letters. The choice, of course, is yours — create whatever exclude file you want.

The EE member specifically asked for an AutoHotkey solution. If other readers of this article are not familiar with AutoHotkey, my EE article will get you going on it:

AutoHotkey - Getting Started

Here is the script in a code block (it is also attached as a file at the end of the article for easy downloading):

; Joe Winograd 5-Jan-2020 Version 1
#Warn,UseUnsetLocal ; warning on uninitialized variables
#NoEnv ; avoid checking empty variables to see if they are environment variables
#SingleInstance Force ; replace old instance immediately
SetBatchLines,-1 ; run at maximum speed

!^t:: ; pick whatever hotkey you want - this is Alt+Ctrl+T (stands for Title case)
; I put exclude file in same folder as script - change line below to put it wherever you want
ExcludeFile:=A_ScriptDir . "\TitleCaseExclude.txt" ; file of words to exclude (conjunctions and prepositions)
If (ErrorLevel!=0)
  MsgBox,4112,Error,Error Level %ErrorLevel% trying to read exclude file:`n`n%ExcludeFile%
Clipboard:="" ; clear clipboard (with Ctrl+c below, this should not be needed, but just in case)
SendInput,^c ; copy selected text to clipboard
ClipWait,1 ; wait one second for text to appear on clipboard
If (ErrorLevel=1) ; copy timed out
  MsgBox,4144,Error,Text did not appear on clipboard after one second
ClipboardFirstChar:=SubStr(Clipboard,1,1) ; get first character
StringUpper,ClipboardFirstChar,ClipboardFirstChar ; capitalize first letter to use later
StringUpper,ClipboardTitle,Clipboard,T ; convert to Title case
Loop,Parse,Exclude,`n,`r ; loop through all exclusions
  ClipboardTitle:=RegExReplace(ClipboardTitle,"i)\b" . A_LoopField . "\b",A_LoopField) ; change exclusions back to lower case
Clipboard:=ClipboardFirstChar . SubStr(ClipboardTitle,2) ; capitalize first word even if an exclusion
SendInput %Clipboard%

My hope is that the descriptive variable names along with the comments in the script provide enough documentation for readers to modify it, but if you have any questions, post them here and I'll try to assist.

The script should work in all versions of Windows from XP through W10, both 32-bit and 64-bit. I tested it on W7/64-bit, W8.1/64-bit, W10/32-bit, and W10/64-bit — all worked perfectly!

If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe



Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free