More Hotstring Tricks Using the Input Command and a Data Table (AutoHotkey Legal Lingo Tips)

The AutoHotkey Input Command Makes It Easy to Use the LegalInput.ini Data Table in Multiple Ways

A few weeks back I demonstrated how to build an INI data table for driving AutoHotkey scripts. I used the INI file format (LegalInput.ini) because it includes one index for quickly looking up records. In the file, I created four-character codes for accessing records which include the English legal definition, the Latin term, and a description (if any).

Legal Input Lex Scripta

While essential to the AutoHotkey feature discussed last time, you’ll find the INI file structure incidental to the technique discussed in this blog. You can use an INI file either for its index or as a standard data table with no special capabilities. As demonstrated in this piece, you’ll find a number of different ways to take advantage of a data table—without requiring an INI file. An INI file gives you a convenient means for quickly accessing data, but (especially for short files) you’ll discover numerous other methods for extracting the information you want.

Note: In addition to the built-in lookup key, the automatic file writing capabilities found in the AutoHotkey INI commands make data file updating through AutoHotkey scripts much easier.

As I continue with this series of blogs, I focus on two primary themes:

  1. Using data tables to drive AutoHotkey scripts.
  2. Exploring the many techniques available in the Input command for creating expanded AutoHotkey tools.

I add this next feature to the original Hotkey (CTRL+ALT+O) by executing a search based on NOT finding a value in the Input command’s MatchList.

The Power of the Input Command

Beginning AutoHotkey Hotstrings 200pxI first introduced the Input command over two years ago in “Create Instant Temporary AutoHotkey Hotstrings with the Input Command, (Part 12, Beginning Hotstrings)” and “Reduce Code and Increase Power with the AutoHotkey Input Command (Part 13, Beginning Hotstrings)” now included in the book Beginning AutoHotkey Hotstrings. While these articles demonstrated how to use Input, those techniques only scratch the surface of the command’s capability and flexibility.

At first, when implementing the Input command, we only think about launching action based upon a value found in the Input command’s MatchList. In the next few blogs, I’ll demonstrate how to add a multitude of features to one Hotkey combination by using all the parameters in this unique command:

Input, OutputVar, Options, EndKeys, MatchList

While primarily aiming for a hit in the MatchList to direct action, the Input command can expand scripting opportunities significantly by not finding a match when:

  1. Using an unmatched OutputVar for alternative actions.
  2. Using EndKeys to initiate alternative routines.

These two Input command parameters yield an almost unlimited number of possibilities. This time we look at how to create a pop-up a menu of Latin legal terms when not finding an acronym in the MatchList.

The Problem with Hotstring Acronyms—Remembering Them!

Last time, I created hundreds of four-letter abbreviations to locate both the Latin legal term and the associated English translation in one record of the data table. I attempted to devise terms which might remind a legal scholar of the English translation:

aper=at the person|ad hominem|Attacking an opponent's character
                              rather than answering his argument.
tsam=to the same thing|ad idem|In agreement.
tinf=to infinity|ad infinitum|To continue forever.

While these short strings seem to make sense (at least to me), I have no idea if they offer any value to someone working in the legal profession. As with any abbreviation, if used often enough, remembering each gets easier. Yet, times always arise when a person must look up some of the less used acronyms. While you want to make your computing life easier with shortcuts,  they serve no useful purpose if you can’t remember them.

It occurred to me to create a menu of Latin legal term options for those times when I (or anyone else) can’t remember the codes. In those situations, I might find it easier to enter the first couple of letters of the Latin words rather than any four-letter code. Then, after AutoHotkey finds no match, it could scan the data table while extracting each Latin term beginning with the keyed letters.

I came up with a routine which, when not finding a code, gives me hints. When I enter the letter “i” and hit Return, the following menu pops up:


If I enter “in” followed by a space, then hit Return, I get a shorter menu:


The list narrows to only those terms starting with in[space]. If I add the action for sending the term to any document, I can instantly insert a Latin legal term:

   SendInput %A_ThisMenuItem%{raw}

I took much of the code for this menu structure from the SynonymLookup.ahk script which offers a similar type of pop-up menu. The SynonymLookup.ahk script takes its data from a Web page while this LegalTerms.ahk script draws from a file on your computer—no Internet required!

How It Works

We rarely consider initiating action with the Input command when we don’t find a corresponding item in the MatchList. However, Input always saves the keystrokes in OutputVar. That means we can continue to use our keystrokes regardless of any matching action. In this case, we deliberately bypass the matching by entering a term we know AutoHotkey won’t find in the MatchList.

Rather than displaying an error message box, AutoHotkey searches the INI files for terms starting with the entered letters. The function for creating the pop-up menu appears as follows:

  Global   ; Use variables outside function
  ItemCount := 0    ; Counter for menu columns
  Loop, read, LegalInput.ini
    Legal := StrSplit(A_LoopReadLine , "|")
      If (Legal[2] ~= "i)^" . OutputVar)  ; Match first few characters
        If ItemCount = 20       ; Start new menu column
          Menu, LegalLatin, add, % Legal[2], LegalAction, +BarBreak
          ItemCount := 1
          Menu, LegalLatin, add, % Legal[2], LegalAction
Menu_item_count := DllCall("GetMenuItemCount", "ptr"
         , MenuGetHandle("LegalLatin"))
If Menu_item_count >= 1
  Menu, LegalLatin, Show
  MsgBox, No matches found for %OutputVar%!

If Menu_item_count >= 1
  Menu, LegalLatin, DeleteAll


After inserting this function in the LegalTerms.ahk script from the earlier blog, call the function LatinList() in place of displaying the following error message:

MsgBox No match! %ErrorLevel% %OutputVar%

Plus, you need to add the Label name LegalAction for the new menu:

  SendInput %A_ThisMenuItem%{raw}

For now, you don’t need any action, but you do need the Label name to avoid any errors.

EndKeys for Initiating Action

Unlike Hotstrings, the Input command does not include a default list of EndKeys. That means the command line from the last blog either waits ten seconds before continuing or until the user enters four keys. That works fine for four-letter acronyms, but, if we only want to enter one or two letters, ten seconds can seem like forever. By adding the Enter key as an EndKey, we have a quick method for terminating the Input and generating the menu:

Input, OutputVar, L4 T10, {Enter} , %Input_String%

Hitting the Return key allows the alternative course of action—which displays a menu of Latin legal terms—to execute immediately.

Reading the File

In the SynonymLookup.ahk script, we created the menu by looping through a variable containing the code from a downloaded Web page. In LegalTerm.ahk, we loop through the data table—this time ignoring the INI file structure—with the Loop (read file contents) command.

Loop, Read, LegalInput.ini

This Loop command reads the file one line at a time allowing us to inspect each for matching terms.

Parsing the Data

We call the StrSplit() function to parse each line (A_LoopReadLine) employing the vertical line ( | ) as a delimiter.

Legal := StrSplit(A_LoopReadLine, "|")

For example, the following line from the INI file:

ixtn=in the extended|in extenso|In extended form, or at full length.

breaks up into:

Legal[1] ⇒ "ixtn=in the extended"
Legal[2] ⇒ "in extenso"
Legal[3] ⇒ "In extended form, or at full length."

We only need to match the keyed-in characters to Legal[2].

Matching the Terms

Similar to the SynonymLookup.ahk matching, LegalTerms.ahk uses the shorthand form of the RegExMatch() function:

If (Legal[2] ~= "i)^" . OutputVar)  ; Match first few characters

The variable OutputVar contains the keystrokes from the Input command. The abbreviated RegExMatch() function attempts to match the Latin term (Legal[2]) to those characters (“i)^” . OutputVar). The i)^ added to the beginning for OutputVar causes the RegEx to ignore the case (upper or lower) of the letters i) and match only at the beginning ^ of the haystack. See the Regular Expressions (RegEx) – Quick Reference.

Creating Menu Columns

To shorten the height of high volume SynonymLookup.ahk menus, I wrote a routine which breaks the menu into columns. In this script, I use almost exactly the same Menu column-break routine creating a new column every 20 items:

Menu, LegalLatin, add, % Legal[2], LegalAction, +BarBreak

This Menu command uses the Latin term (Legal[2]) to insert a new menu item into a new column (+BarBreak).

Knowing Menu Length

In the SynonymLookup.ahk script, I used a DllCall() function to determine the length of the menu. This acted as a condition for initiating a number of different actions—including retrying the Internet download.

Menu_item_count := DllCall("GetMenuItemCount", "ptr"
    , MenuGetHandle("LegalLatin"))

In the current script, I use the variable Menu_item-count to determine if the menu exists, then take the appropriate action:

If Menu_item_count >= 1
  Menu, LegalLatin, Show
  MsgBox, No matches found for %OutputVar%!

If Menu_item_count >= 1
  Menu, LegalLatin, DeleteAll

This blog only represents the first variation I plan for using the Input command in conjunction with a data table. Stay tuned!


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: Logo

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

Facebook photo

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

Connecting to %s