AutoHotkey Tip of the Week: Instant Upper Case, Lower Case, and Initial Cap Text—September 2, 2019

Tips: Quick Hotkeys for Changing Text To/From Capital Letters and How to Initial Cap Everything, Plus, How to Write Robust Clipboard Routines

Light Bulb!This week I offer two useful tips: one for editing text and the other for improving your AutoHotkey scripts.

When reviewing my books, I look for those tips which I use all the time. I’ve found that I developed some scripts primarily for demonstration purposes and rarely ever use them again. Yet, I have a few which I use so much that I feel like they have become a part of my Windows system.

AHKNewCover200In this case, while perusing my Beginner’s Guide to AutoHotkey, I noticed in “Chapter Four: Hotkeys and Text Editing with Windows Clipboard” the Hotkeys for changing selected portions of text into all capital letters, all lowercase letters, or initial cap every word in the section. I originally wrote these Hotkeys when I edited articles submitted by freelance writers.

Some writers have a penchant for placing their article headlines and topic subheadings in all uppercase letters. By creating a Hotkey for converting the entire line to Title Mode (initial capital letter for each word), I quickly solved the retyping problem:

+^k:: ; SHIFT+CTRL+K converts text to capitalized
  Clipboard := ""
  SendInput, ^c ;copies selected text
  ClipWait
  StringUpper Clipboard, Clipboard, T ; Title mode conversion
  SendInput %Clipboard%
Return

This Hotkey mostly fixes the all-caps text by converting every word to initial caps. However, I did need to revert some prepositions and connectors to lowercase as appropriate (e.g. Andand, Forfor, Toto, etc):

^l:: ; CTRL+L converts text to lower
  Clipboard := ""
  SendInput, ^c ;copies selected text
  ClipWait
  StringLower Clipboard, Clipboard
  SendInput %Clipboard%
Return

Less often, when I wanted to yell, I included a routine for changing the selected text to all caps:

^u:: ; CTRL+U converts text to upper
  Clipboard := ""
  SendInput, ^c ;copies selected text
  ClipWait
  StringUpper Clipboard, Clipboard
  SendInput %Clipboard%
Return

Although I rarely edit other people’s writing these days, I continue to use the three Hotkeys regularly.

Tip #2: Standard AutoHotkey Windows Clipboard Routine

hotkeycover200As shown in “Chapter Five: Sharing AutoHotkey Scripts and Restoring the Clipboard’s Original Contents” of my Beginner’s Guide and discussed in greater detail in Chapter Nine of my AutoHotkey Hotkey Techniques book, you can add a number of features to the AutoHotkey Clipboard routines shown above which make them more robust. I recommend that you add these extras to every Clipboard routine you write (shown in red):

!R::
OldClipboard := ClipboardAll
Clipboard := "" ; Clears the Clipboard
SendInput, ^c
ClipWait 0 ; Pause for Clipboard data
If ErrorLevel
{
MsgBox, No text selected!
}
StringUpper Clipboard, Clipboard ; Clipboard manipulation code
SendInput, %Clipboard%
Sleep 200
Clipboard := OldClipboard
Return

This routine saves and restores the original contents of the Windows Clipboard. It also uses ErrorLevel in conjunction with the ClipWait command to warn you if the Clipboard copy fails. When writing other Clipboard dependent routines, replace the StringUpper Clipboard, Clipboard statement with the relevant Clipboard manipulation code. You can use this example as a template for your Clipboard routines.

Alternative Approach with Format() Function

Starting with AutoHotkey Version [v1.1.20+]: you can replace the StringLower command with the Format() function for case conversions, as shown below:

MsgBox % Format("{:U}, {:L}, {:T}", "upper", "LOWER", "title")

For the examples in this tip, send all caps:

SendInput % Format("{:U}", Clipboard)

Convert to lowercase:

SendInput % Format("{:L}", Clipboard)

Capitalize the first letter of each word:

SendInput % Format("{:T}", Clipboard)

You can use each single line to replace two lines in each Hotkey:

StringUpper Clipboard, Clipboard, T
SendInput %Clipboard%

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s