Sorting Lists for Emoji Menus (AutoHotkey Sort Command Tip)

If a Menu Gets Too Long, the Sort Command Helps to Put Your Emoji List in Alphabetical Order

I received the following e-mail with regard to the blog “Put Your Emoji Hotstrings in a Pop-up Menu (AutoHotkey Trick)“:

Hello Jack,

Library Benefits

I enjoy your emails and have gotten a lot out of your books. I tried out EmojiMenu and it pops up but in Office 2013, (Word and Outlook), the emojis inserted are black, not color.

However, they are in color in Gmail as I compose this email. 🎂 Any idea why that is?

Also, some feedback/suggestions:

What are the categories available? From the column, I know about Animal and I guessed Food, but the others…? Is there a list of them?

A helpful improvement would be to alphabetize the lists. I don’t think to look for “catface” in the third column of Animal.

Thank you and best regards.

~Dale

Hi, Dale,

Perhaps, if you change the font to Segoe UI Emoji in Office, the emojis will appear in color. It depends upon how the program supports emojis. It worked in my mail program, but not in LibreOffice Writer or WordPad. In Writer and WordPad, the style of the emoji changed, but not the color. (I don’t use Office 2013.)

The available categories merely consist of comments (a semicolon followed by text) which I added to each Hotstring in the EmojiInsert.ahk file. I did not attempt to categorize all of them. You can add anything you like and even insert multiple categories by adding more terms to the comments after the semicolon (e.g. :*:!cloud::☁   ; weather, rain). Edit the EmojiInsert.ahk file to see, add or change Hotstrings and/or categories.

Plus, the Hotstrings themselves can act as categories. For example, the term arrow produces many options by matching that string inside the activating text which starts with an exclamation point:

Emoji Arrow
Using the EmojiMenuSort.ahk script, this menu displays all emojis which include the word arrow in the Hotstring activating text from EmojiInsert.ahk. —alphabetized with the techniques described below.

In essence, the Hotstring activating characters can act as individual categories—either as specific keys or substrings of any text appearing on the same line.

Tip: I did not adjust most of the activating strings in the EmojiInsert.ahk script to something simple. For example, no one would use the ridiculously long Hotstring:

:*:!mobile-phone-rightwards-arrow-at-left::📲

Plus, even if they tried, the Hotstring !mobile-phone ⇒ 📱 would pop-up before ever completing the longer Hotstring. If you want to implement the Hotstrings rather than the menu, for ease-of-use, you will need to make your own changes to the activating string.

You can implement the AutoHotkey Sort command to alphabetized the menu list. You would need to save the matched data to a variable, then sort it before creating the menu. I did this for the menu above and explain how it works below. I would not sort the EmojiInsert.ahk file since it seems to have some logical grouping. I drew the list from a table on the Web and they are probably in emoji Unicode number order.

Thanks,

—Jack

Sorting AutoHotkey Menus

Unfortunately, I don’t see an easy method for sorting a menu after its construction. Even if one did exist, my menu column format would probably cause a problem. That means I must sort the items before inserting them into the menu. On the plus side, I found this relatively easy to do.

The Sort command only works on variables, not file contents. We must first place our results into a variable for sorting. Then, we create our menu in alphabetical order by parsing the sorted variable in order.

In the EmojiInsert.ahk script, AutoHotkey reads directly from the EmojiInsert.ahk file matching Hotstrings with the search term—immediately placing the results in the menu. To create a sorted menu, we need to break this sequence into a two-loop process: Load matched Hotstrings from the file into a variable for sorting, then (after using the Sort command) load the ordered variable contents into the menu. We separate key parts of the original loop into two snippets of code.

First, AutoHotkey loads the variable EmojiItems with the matched records directly from the file:

EmojiItems := ""
Loop, read, EmojiInsert.ahk
{
  If InStr(A_LoopReadLine,Trim(Clipboard)) and (A_LoopReadLine ~= "::")
  {
    Emoji := StrSplit(A_LoopReadLine , ":")
    Icon := StrSplit(Emoji[5], ";")
    EmojiItems := EmojiItems . Emoji[3] . " | " . Trim(Icon[1]) . "`n"
  }
}
Sort, EmojiItems, U

This snippet of code uses the bulk of the code from the original loop: matches the Hotstrings in the EmojiInsert.ahk file; formats the text for the upcoming menu; and appends each value to the variable EmojiItems followed by the newline (`n) character. The Sort command rewrites the variable—placing line-by-line all the entries in alphabetical order. The U option (unique) in the Sort command removes duplicates—if any.

The second loop switches to the variable parsing Loop, Parse command to build the menu from the new sorted variable:

 ItemCount := 0
 Loop, Parse, EmojiItems , `n
 {
   If ItemCount = 20
   {
     Menu, EmojiMenu, add, % A_LoopField, InsertEmoji, +BarBreak
     ItemCount := 1
   } 
   Else
   {
     Menu, EmojiMenu, add, % A_LoopField, InsertEmoji
   ItemCount++
 }

This code places the sorted items into the menu while breaking the listings into columns with a maximum of 20 items. Delimited by the newline character (`n) added in the first loop, AutoHotkey iterates through the variable EmojiItems. Each step represents one menu item, or one line, contained in the AutoHotkey variable A_LoopField.

By replacing the main loop in EmojiMenu.ahk with the two loops discussed here, you create an alphabetized menu similar to the one shown in the image above.

You’ll find many times when you may want to temporarily sort data for display in a Menu, GUI window, MsgBox, or other types of objects. As a rule, you need to:

  1. Place the data in a variable.
  2. Use the Sort command to order the data.

This technique represents one more tool to add to your AutoHotkey toolbox.

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 )

Facebook photo

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

Connecting to %s