A Maneuver That Opts for Italic Output by Tricking the AutoHotkey Input Command, Plus a Tip for Creating Italic Hotstrings
In the past few blogs, I’ve explored using a data table to drive an AutoHotkey application featuring the enigmatic Input command. The article “Input Command Creates Temporary Hotstrings from Data Table (AutoHotkey INI File Technique)” demonstrates how to initiate access to a data table using the Input command. Following up, “More Hotstring Tricks Using the Input Command and a Data Table (AutoHotkey Legal Lingo Tips)” gives us an alternative menu generated from the data table when no match occurs in the Input command. With a simple modification of the AutoHotkey Input command, we add an option for converting the Latin legal terms into italics for particular word processors.
Blog Update, January 25, 2019: If your word processing software supports control key toggles such as CTRL+I for italics then you may want to take a look at “Adding Italics to Hotstrings in Word Processing Software (AutoHotkey Quick Tip).”
When Do You Italicize Foreign Words?
We’ve been told to italicize foreign words in our writing—unless we commonly use those words or phrases in our everyday speech. For example, “caveat emptor” (buyer beware) might not get special emphasis due to frequent use, while you might italicize “Je ne sais pas.” (French for “I don’t know.”) because English speaking people rarely use this—unless they think they’re French.
In the LegalTerm.ahk script, I offer hundreds of Latin legal terms in a menu format. Many of the familiar ones need no special treatment, but the general obscurity of numerous other legal terms make them good candidates for extra emphasis (at least, for regular non-lawyer people). The most recent form of the Latin legal terms menu script Sends only the plain vanilla text to a document. Wouldn’t a key that optionally italicizes selected terms (in programs which accept special formatting) come in handy? Fortunately, we can change the formatting of our menu output by changing the terminating character in the Input command.
But, first, let’s talk italics.
Italics in a Word Processor
In a text editor which only support plain-vanilla characters, you can’t produce italic or bold characters. To stress words when working with HTML code, you must surround the emphasized text with tags (e.g. <i>Actori incumbit onus probatio</i>). But when using most word processors, e-mail programs, and many Web blogs, you can add italics by highlighting the word or phrase and executing the CTRL+I key combination.
The SendInput command makes it easy to insert plain text:
SendInput %A_ThisMenuItem%{Space}{raw}
But, italicizing text greatly complicates the Send activity. After inserting the raw text, AutoHotkey must select the entire term or phrase, then use the CTRL+I shortcut to convert the words to the slanted form. In editing programs, that takes a number of actions:
SendInput %A_ThisMenuItem%{raw} Length := StrLen(A_ThisMenuItem) ; Capture length of phrase SendInput, {Shift down} ; Hold SHIFT key down SendInput, {Left %Length%} ; Select phrase SendInput, {Shift up} ; Release SHIFT key SendInput, ^i ; Issue italics command SendInput, {Right} ; Deselect phrase to right SendInput, ^i ; Deselect italics SendInput, {Space} ; Add space to end
(In this example, I only display separate SendInput commands to demonstrate the step-by-step process involved in setting special features in word processing programs.)
The script above:
- Selects the target term(s) by holding down the SHIFT key ({Shift down}) while moving the cursor across the length (Length := StrLen(A_ThisMenuItem)) of the term(s) to the left ({Left %Length%}), then releases the SHIFT key ({Shift up}).
- Issues the italics shortcut CTRL+_I (^i) to convert the selected term(s).
- Deselects the term(s) using the right arrow ({Right}) which sets the text cursor on the right end of the original selection. Some word processors require more that one right arrow press to reach the end ({Right %Length%})—more on that complication later.
- Issues the italics shortcut CTRL+_I (^i) to turn off the mode.
- Adds a space ({Space}) to the end of the inserted term(s).
You can combine the set of extra commands into one line:
SendInput %A_ThisMenuItem%{raw}
Length := StrLen(A_ThisMenuItem) ; Capture length of phrase
SendInput, {Shift down}{Left %Length%}{Shift up}^i{Right}^i{Space}
Alert: Not all word processor work in the same manner. In WordPad and the online WordPress blogging software I use, AutoHotkey deselects the phrase with a simple right arrow key and sets the text cursor to the right end (as coded above). However, the free LibreOffice Writer requires the exact number of right arrow presses to reach the end of the newly inserted and italicized phrase:
SendInput, {Right %Length%}
If you use multiple word processing platforms, see the “Hotstring Tip Caution” below for how to deal with an assortment of word processors.
Italic Hotstring Tip
Suppose you have specific words or phrases which you always want to italicize (when possible). Put the replacement into a Hotstring routine. For example, the French je ne sais quoi means that certain indescribable something (“I don’t know what!”). Since I plan to make this one of those pretentious (and annoy) phrases that I’ll interject at random times, I turn it into an AutoHotkey Hotstring command text expansion:
::jq:: SendInput , je ne sais quoi{Shift down}{Left 15}{Shift up}^i{Right}^i{Space} Return
Now, every time I want to interject je ne sais quoi, I can just type “jq” followed by a space or period. If my media accepts the universal italics code CTRL+I, it converts to the proper form. If not, it displays regular text…maybe.
Hotstring Tip Caution: Not every word processor works in the same manner. You need to test your programs for how each accentuates text—starting from the right end of a phrase. Select the text by holding down the SHIFT key while pressing the left arrow key, activate the CTRL+I combination, then unearth the effect of the right arrow action. Does one depression of the right arrow place the cursor on the right side of the phrase or do you need multiple key presses? Unless you always use the same word processor, the Hotstring action must change for the differing conditions:
::jq:: If WinActive("ahk_class Chrome_WidgetWin_1") or WinActive("ahk_class WordPadClass") SendInput , je ne sais quoi{Shift down}{Left 15}{Shift up}^i{Right}^i{Space} Else If WinActive("ahk_class SALFRAME") { SendInput , je ne sais quoi{Shift down}{Left 15}{Shift up}^i{Right 15}^i{Space} } Else SendInput, je ne sais quoi{space} Return
(This routine uses line continuation techniques to split some single lines into two for display purposes.)
I used the WindowProbe.ahk script to identify the ahk_class of each Windows program. By hovering over any open program window, the WindowProbe.ahk script displays a variety of window information—similar to Window Spy which opens by right-clicking on a running AutoHotkey script icon in the Windows System Tray and selecting Window Spy. The window class (e.g. Class: ahk_class WordPadClass, shown at right) identifies the running program. When used in conjunction with the WinActive() function, AutoHotkey can isolate the Hotkey action to specific software. If not one of the included programs, the Hotstring inserts plain text. This technique makes it possible to tailor your Hotstrings to your word processor or Web editing window.
Changing the Menu to Insert Italics
We tell the AutoHotkey Input command whether or not to italicize output from the menu by adding an alternative termination key. The original script used the RETURN key for early termination. As a rule, when terminated with a RETURN, the LegalTerms.ahk script prompts the Label subroutine LegalAction to inject non-emphasized text from the menu into any document:
LegalAction: SendInput %A_ThisMenuItem%{Space}{raw} Return
To provide an alternative route for creating italics, we include an additional terminating character in the Input command—in this case, TAB:
Input , OutputVar, L4 T10, {Enter}{Tab}, %Input_String%
Next, just after the Input command, we capture the terminating character name found in the ErrorLevel variable in the form of “EndKey:Tab” or “EndKey:Return”:
EndKey := ErrorLevel
Depending upon how we terminate the Input command, AutoHotkey attempts to italicize the output:
LegalAction: If (EndKey = "EndKey:Tab") { SendInput %A_ThisMenuItem%{raw} Length := StrLen(A_ThisMenuItem) SendInput, {Shift down}{Left %Length%}{Shift up}^i{Right}^i{Space} } Else SendInput %A_ThisMenuItem%{Space}{raw} Return
When using RETURN to launch the menu you get plain text output everywhere:
Actori incumbit onus probatio
When using TAB to launch the menu you get italics in your word processor or WYSIWYG Web editing fields:
Actori incumbit onus probatio
If your text editor doesn’t support italics and you hit TAB, the routine goes through the italicizing routine but has no effect—leaving only the raw text. (Actually, the results are unpredictable in text editors. In Notepad, the key combination CTRL+I inserts a TAB thereby deleting the output.) The same menu gives a different result depending on how you decide to end the Input command.
If you want to add more flexibility to the italics option (and solve the Notepad problem), use a conditional routine similar to that shown in the previous Italic Hotstring Tip.
You can use the same technique to add a variety of styles to your Input menus (e.g. bold, underline, etc)—or, for that matter, different actions and/or menus. You only need to insert additional terminating characters to the Input command—each one directing an alternative menu and/or action.
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.)
Nice column (as usual) Jack. Very informative and useful.
LikeLike
[…] little while back, I wrote the blog “Italicize Your Hotstring Replacements with this Input Command Ploy (AutoHotkey Tip)” which demonstrated tricks for adding special features for both the Input command and […]
LikeLike