More Pop-up Synonym Menu Tips for AutoHotkey Hotstrings, (Part 8, Beginning Hotstrings)

The C Option (Case Sensitive)—An Easy Way to Limit Hotstring Action. Plus, How to Emulate Hotstring Capitalization in an Action Menu Subroutine.

*          *          *

Beginning AutoHotkey Hotstrings 200pxAs a convenience for people who want to get this entire series (plus more) in one place, I have now published the e-book Beginning AutoHotkey Hotstrings which is available on the ComputorEdge E-Books site (in three formats—EPUB for mobile devices, MOBI for Kindle, and PDF for printing and computer) and through Amazon. Regardless of whether you’re interested in the book or not, it’s worth your time to peruse some of the blogs linked at “Beginning AutoHotkey Hotstring Techniques” found under the “AutoHotkey Topics and Series” tab in the top menu bar. They just might inspire your next AutoHotkey script.

*          *          *

While working with the OverusedWords.ahk script from last time, I notice that particular issues arose when activating a replacement menu with the word have. For example, I found that have when used in the form of a question doesn’t need the pop-up menu. “Have you eaten yet?”
Regardless, the selection list of unnecessary and unwanteds replacement words appears. I don’t contemplate a time when in normal communications I would want to replace the word have at the beginning of a question. I prefer that the synonym menu not pop-up. There is a fairly easy fix for this.

By adding the C option to the Hotstring routine, it becomes case sensitive:

:B0C:have::
  If A_EndChar = %LastChar%
    WordMenu(5,"must,need to,am compelled to
     ,am forced to,are compelled to,are forced to
     ,hold,possess,own,contain,acquire,gain,maintain
     ,believe,bear,beget,occupy,absorb,fill,enjoy")
Return

(The WordMenu() function line of code is wrapped for display purposes with line continuation, as discussed in the last blog.)

Adding the C option between the first two colons eliminates any Hotstring response to the capitalized form of the word. Questions starting with Have will no longer activate a pop-up menu.

Have Menu
This modified menu for the word “have” includes options for replacing the expression “have to.”

Note: I’ve also added a few more options to the have replacement menu (or more appropriately have to). While not appearing on the original list of overused words, have to is another expression which probably should be supplanted with one of the six items at the top of the menu at the right. (You may find more options to add to the have to list.)

No list of words is likely to include all possible replacements or usages of an overused word. You should tailor your lists in ways which suits you best.

Warning: Reinforcing what I wrote last time, the OverusedWords.ahk script should only be loaded when you’re doing your most significant development work. The script will slow you down as menus pop up at seeming random times. While leaving the script loaded all of the time may be educational, it could drive you insane.

(The script OverusedWords.ahk can be downloaded from the ComputorEdge AutoHotkey script site. All the free scripts available at the site are discussed at the Web page “Free AutoHotkey Scripts and Apps for Learning Script Writing and Generating Ideas.”)

Passing through Capitalization in Action Hotstrings

A regular Hotstring (without the C option for case sensitivity) responds to any capitalization you may include when typing an activating string. For example, the following snippet yields different results depending upon whether you type btw, Btw, or BTW:

::btw::by the way

Type btw and you get by the way. Add an initial capital to Btw and By the way is returned. Utilize all capital letters by keying in BTW and the result BY THE WAY appears—also in all caps. This is a powerful automatic feature for regular hotstrings. No need to worry about whether you insert a word at the beginning of a sentence or in the middle. The initial cap is automatically carried through. This feature is lost in action Hotstrings running a snippet of code.

This problem exists for the Hotstring menus discussed here. Even if you capitalize the activating text, its replacement will display the all lowercase words from the menu.

One solution I considered was to add two case sensitive (C option) Hotstrings for each word: one with the menu items capitalized and the other all lowercase. This would work, however, since it requires adding two Hotstring setups for each overused word, it doubles the amount of code in the script.

I opted for the more complicated approach which is implemented in the OverusedWord.ahk script. It works by checking the first letter of the text for case, then appropriately capitalizing (or not) the replacement word. While this subroutine may seem a bit complicated for this application, it’s worth taking a glance at the techniques. You may want to accomplish something similar in other scripts. The WordMenuAction: subroutine is modified as follows:

WordMenuAction:
 Check := BackSpaces -1   ;number of spaces less first letter
 SendInput {BackSpace %Check%}  ;backspace all but first char
 SendInput {Shift down}{Left}{Shift up} ;select first char
 OldClipboard:= Clipboard ;save former clipboard contents
 Clipboard:= ""  ;empty clipboard
 Send, ^c ;copy first letter
 ClipWait 0 ;wait for copy
If Clipboard Is Upper  ;if capitalized
 {
 First := SubStr(A_ThisMenuItem, 1 , 1)  ;parse first letter
 Second := SubStr(A_ThisMenuItem, 2)   ;parse remainder
 Stringupper First, First  ;capitalize the first letter
 InitialCap := First . Second  ;concatenate (add) cap letter
 SendInput %InitialCap%{raw}%A_EndChar%
 }
Else
 SendInput %A_ThisMenuItem%{raw}%A_EndChar%
 Clipboard:= OldClipboard  ;return former clipboard contents
Return

ComputorEdge AutoHotkey E-BooksAs you can see, it is not a simple one-step process. I was hoping that there would be a Hotstring built-in variable which contains the activating string. That would make the problem much simpler. But since such a variable doesn’t appear to exist for AutoHotkey Hotstrings, I came up with the above code. (If anyone knows of a simpler solution, I all ears…or eyes. I’ll add any reasonable solutions to this blog.) After looking at this code, you may decide that the two sets of Hotstrings (one capitalized and one not) is more acceptable to you.

This approach is not totally new. It involves most of the same tricks used in the blog “Adding Action to AutoHotKey HotStrings, Plus a Trick for Creating IF Conditional Hotstring Text Replacements (Part 3, Beginning Hotstrings).” If you run into trouble understanding how this subroutine works, then that blog may be worth reviewing.

In essence, the above routine 1) captures the first character of the typed string, 2) checks for capitalization, then 3) returns the appropriate form of the the replacement text.

The first few steps select the first letter of the activating text. This is done in WordMenuAction: by deleting all but the first character before starting the screen selection process. The number of backspaces needed is one less than the value of the variable Backspaces and stored to the variable Check:

Check := BackSpaces -1

The BackSpace action is used to delete the last section of the screen text with the SendInput command:

SendInput {BackSpace %Check%}

The remaining first character is selected (highlighted) on the screen by holding down the Shift key, using the cursor to move one character to the left, then releasing the Shift key:

SendInput {Shift down}{Left}{Shift up}

This is effectively the same as clicking the left mouse button on one side of the character, dragging the mouse to the other side of the character, then releasing the left mouse button.

The previous contents of the Windows Clipboard is saved to the variable OldClipboard:

OldClipboard:= Clipboard

The Clipboard is emptied:

Clipboard:= ""

The selected first letter (now selected) is copied to the Clipboard:

Send, ^c ;cuts selected text

AutoHotkey waits for the text to load into the Clipboard:

ClipWait 0

If the Clipboard contents is uppercase (capitalized):

If Clipboard Is Upper

then, using the SubStr() function, the text from the menu item is parsed into the first letter (First) and remainder of the string (Second):

First := SubStr(A_ThisMenuItem, 1 , 1)
Second := SubStr(A_ThisMenuItem, 2)

The first letter is converted to uppercase with the StringUpper command:

Stringupper First, First

The the two string variables are recombined (concatenated) into initial capitalized text and stored in the variable InitialCap:

InitialCap := First . Second

To concatenate (combine into one) strings, position a dot surrounded with spaces between the strings .

Capitalized text replaces the original text:

SendInput %InitialCap%{raw}%A_EndChar%

If the first letter is not capitalized:

Else

send the menu item text unchanged:

SendInput %A_ThisMenuItem%{raw}%A_EndChar%

Restore the Clipboard with its original contents:

Clipboard:= OldClipboard

This approach may seem slightly convoluted, yet it works.

Next time, we’ll take a peek at using Hotstrings to add the current date in various date formats to any document by picking from a menu.

*         *          *

Find Jack’s AutoHotkey e-Books at ComputorEdgeBooks.com.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s