



Searching Databases

Library BenefitsRecently I’ve written a number of scripts which search databases to locate specific types of information. In some cases, I used INI data files (AutoHotkey Quick Reference search and Latin Legal terms) while in other scripts a Web page acted as the information source (SynonymLookup.ahk). Sometimes I know what terms I need to use—such as AutoHotkey commands. However, in many cases, the lookup data seems much more obscure. I’ve found that putting results into filtered menus creates a quick way to find particular data items while making it easy to add action. I’ve developed some useful techniques for accessing data while building a menu which will work in numerous different situations.

This time I use an AutoHotkey Hotstring file (EmojiInsert.ahk) as the database. While EmojiInsert.ahk contains a series of Hotstrings for inserting emojis into any Windows document, it also contains everything I need to build lookup menus. Remember, over 1000 Hotstrings creates an enormous obstacle to recalling all the activating strings.

Unlike the AutoCorrect Hotstring app which instantly fixes common mistakes—even when you don’t know you’ve made any—EmojiInsert.ahk requires that you know and type the activating text before it will operate properly. Even building a help table won’t offer much assistance—too long. I decided to create—based on a keyword—a search capability with lists the emojis and their Hotstring activating text. By using a menu structure, I include the option to insert any emoji directly from the menu into a Windows document.

I use two of my favorite tricks for this short application: the Standard AutoHotkey Clipboard Routine and a data-loading menu building technique. I first implemented this type of menu in the SynonymLookup.ahk script. The combination of these two tricks allowed me to quickly compose the new app. The script originally contained only 37 lines of code, yet it offered incredible power.

How EmojiMenu.ahk Works

After loading the EmojiMenu.ahk script, the user employs a simple two-step procedure. First, highlight any word which may represent the name of an emoji or its category. Next, activate the search with the Hotkey combination CTRL+ALT+E. AutoHotkey searches the EmojiInsert.ahk file for any Hotstring command lines containing the highlighted word—then displays a menu of the matched records.

For example, I added a text comment to many of the emoji Hotstrings to identify the category animal, as shown in the following sample taken from the EmojiInsert.ahk file:

:o:!monkey::🐒         ; animal
:o:!rooster::🐓        ; animal
:o:!chicken::🐔        ; animal
:o:!dog::🐕            ; animal 
:o:!pig::🐖            ; animal
:o:!boar::🐗           ; animal
:o:!elephant::🐘       ; animal
:o:!octopus::🐙        ; animal
:*:!spiral-shell::🐚   ; sea, beach
:*:!bug::🐛            ; bug, insect
:*:!ant::🐜            ; bug, insect
:*:!honeybee::🐝       ; bug, insect
:*:!lady-beetle::🐞    ; bug, insect
:*:!fish::🐟           ; animal
:*:!tropical-fish::🐠  ; animal
:*:!blowfish::🐡       ; animal
:*:!turtle::🐢         ; animal
:*:!hatching-chick::🐣 ; animal

After loading EmojiMenu.ahk, I type the word animal, then highlight the text by dragging the mouse cursor over the word while holding down the left mouse button. Then, I press the Hotkey combination CTRL+ALT+E.

The beauty of this file search lies in the fact that AutoHotkey scours each line of code looking for any match—either within the activating string or its associated category comment. After finding any match for the word animal, the script adds the activating string and the emoji to a pop-up menu:

The pop-up menu for “animal” emojis displays both the activating Hotstring and the emoji. To insert one of the icons, click on the menu item.

Although I wrote this script as a reminder pop-up for the Hotstring activating text, it doubles as an icon insertion tool. Click on any of the items to directly replace the highlighted text with the emoji.

The EmojiMenu.ahk Script

In previous blogs, I found myself using comparable menu techniques to solve similar problems:

I could have create a MsgBox window as a reminder, but—after dismissing the message box—I would still need to enter the activating code for my target. Also, I could have build a GUI window which operates in a similar manner, but I find the Menu command so much easier to use.

Creating this type of action reminder requires three parts:

  1. Keyword capture.
  2. Keyword search of the data source.
  3. Action menu implementation.

Capturing the Search Term

The number one method I use for designating a search term employs the Standard AutoHotkey Clipboard Routine. Highlight any text, then run the Clipboard Routine. The script captures the text for any manipulation or other action. In the information/action menu routine, the Clipboard text acts as the search term. The SynonymLookup.ahk script implements the same Standard Clipboard technique as the EmojiMenu.ahk script:

  OldClipboard:= ClipboardAll
  Clipboard:= ""
  Send, ^c ; copies selected text
  ClipWait 0
  If ErrorLevel
    MsgBox, No Text Selected!
  ;  [Include AutoHotkey Code for searching 
  ;  EmojiInsert.ahk file and creating menu]
  Clipboard := OldClipboard

In the LegalTerms.ahk script, an active Input command captures any keyboard activity until either an end character terminates the command or it times-out. This alternative method works well for those times when you don’t need to see what you’re typing—such as an initial letter of the alphabet.

In both of the above cases, AutoHotkey uses the captured term as a search keyword. Which approach you select (Clipboard or Input command) depends upon your situation. For example, the program Paint.net does not allow the selection of text during text input. That means the Standard AutoHotkey Clipboard Routine can’t operate properly. By switching to the Input command type of text capture, you can produce a workable menu for Paint.net—although you can’t see what your typing.

Search the Data Source

Any source which allows a text search can act as a database. That means any text file (in a variety for formats) or the underlying source code of a downloaded Web page (also all text) will work. You can search either a database file or a Web page. In the LegalTerm.ahk script, I use an INI file containing common Latin legal terms. The Loop (read file contents) command increments through the file line-by-line looking for anything which matches the search conditions. In the case of EmojiMenu.ahk, the search identifies any Hotstring which contains the search keyword:

ItemCount := 0
Loop, read, EmojiInsert.ahk
 If InStr(A_LoopReadLine,Clipboard) and (A_LoopReadLine ~= "::")
    Emoji := StrSplit(A_LoopReadLine , ":")
    Icon := StrSplit(Emoji[5], ";")   ; Remove comments
    If ItemCount = 20   ; Start a new menu column
      Menu, EmojiMenu, add, % Emoji[3] . " | " 
          . Trim(Icon[1]), LoadPage, +BarBreak
      ItemCount := 1
          . Trim(Icon[1]), LoadPage

I used the colons as a delimiter in the StrSplit() function to parse the Hotstring command line into its component strings. The first and fourth values return blanks (i.e. Emoji[1] and Emoji[4]). The third array value (Emoji[3]) contains the activating string and the fifth array value holds the emoji and category comments (Emoji[5]):

:o:!elephant::🐘       ; animal, zoo

Emoji[1] = ""
Emoji[2] = "o" 
Emoji[3] = "!elephant"
Emoji[4] = ""
Emoji[5] = "🐘       ; animal, zoo"

Note the last array value (Emoji[5]) needs re-parsing using the semicolon as a delimiter to isolate the emoji for use in the menu. Furthermore, the Trim() function removes any blank spaces surrounding the emoji.

Tip: The A_LoopReadLine ~= “::” expression does a quick RegEx match inside the line looking for a valid Hotstring. This removes any other extraneous lines of code or comments from contention.

Interestingly, the keyword can match anywhere in the line—either in the activating text or the category comment at the end of the line. This makes it possible to find emojis based upon the activating text or the added classification comments as shown in the sample Hotstring file above. Plus, since it executes a substring search (InStr() function), any piece of a word returns a result. For example, searching for the text ball creates a menu of all balls including basketball and football. However, it also includes the emojis for a ballot box 🗳 and ballpoint pen 🖊.

Use Regular Expressions (RegEx) for Web Pages

Web pages rarely offer the neat layout of Hotstrings files or INI tables. That dictates resorting to powerful Regular Expressions for digging out the target data on the Internet. While not laid out in line-by-line formats, Web pages usually offer some type of pattern within the source code which AutoHotkey can latch onto using a Regular Expression (RegEx) match. The SynonymLookup.ahk script does just this.

Cover 200Note: While the original SynonymLookup.ahk script did break when the powers-that-be changed the source code, it only took me about 15 minutes to figure out how to ignore the changes and pick up the new format. Certainly, working with a data table offers simpler solutions than Web pages, but if you plan to use the Web as a database, then you’ll most likely need RegExs. (See this “Introduction to Regular Expressions (RegEx) in AutoHotkey” or the e-book A Beginner’s Guide to Using Regular Expressions in AutoHotkey.)

Displaying the Menu

As shown in the snippet of code above, AutoHotkey builds the menu as it loops through the lines of the EmojiInsert.ahk Hotstring file. Since many of the menus can get quite long, I use a programming trick which starts a new column every twenty items. I implemented this column breaking code to create columns in the Synonym Menu  and, again, when building a menu of specific future weekday dates,  “A Trick for Inserting Next Friday’s Date into Any Document (AutoHotkey Tip).”

The Look of the Emoji Menu

In the EmojiMenu.ahk script menus, emojis appear in black and white as drab Windows icons. As far as I can tell, you won’t find a procedure in AutoHotkey for changing these emojis in the menu into color images. This probably reaches the limits of Windows context menus. (Possibly, someone might find something that could change the look of the menu emojis in the hidden depths of Windows, but, for now, it’s beyond me.)

After insertion into the final document, how the emoji looks depends upon that Windows program. In some cases switching to the Windows Segoe UI Emoji font may change the appearance of the icons. Also, changing the size of the font should adjust the dimensions of the emoji:

French Fries 🍟 Red Apple 🍎 Snowman ☃

French Fries 🍟 Red Apple 🍎 Snowman ☃

Only experimentation with individual Windows programs will uncover the possibilities.

The Menu Action

Although I originally designed the menu as a Hotstring reminder table, I found no reason not to add the replacement technique to the pop-up. In fact, some people may decide not to load the EmojiInsert.ahk Hotstring script at all—although, you must maintain its availability in the working directory for EmojiMenu.ahk to work. The action Label subroutine InsertEmoji parses the emoji (StrSplit(A_ThisMenuItem , “|”)) using the vertical line ( | ) as the delimiter and employs the SendInput command to insert the icon—trimmed of all spaces (Trim(EmojiIcon[2])).

  EmojiIcon := StrSplit(A_ThisMenuItem , "|")
  SendInput, % Trim(EmojiIcon[2]) 

You can apply these AutoHotkey techniques to any AutoHotkey app which requires a search and replace capability.

Note: It would have been cool to use the emojis as icons at the beginning of each menu item, but I didn’t find a simple way to do it. The implementation would require converting each emoji into an ICO file and calling it by name in the Hotstring file. That means converting over 1000 images! A bit too tedious for me.

Next time, a surprise (yet useful) unintended consequence of the EmojiMenu.ahk script.


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



(Just demonstrating for my granddaughter.)

Leave a Reply


WordPress.com Logo


Google photo


Twitter picture


Facebook photo


Connecting to %s