Radically Improving AutoHotkey GUI Apps with Menu Bars

While GUI Menu Bars Make Your AutoHotkey Apps More User-Friendly, the Benefits from Adding One to Your Script Go Far Beyond the Obvious

* * *

This blog represents the first in a series that revisits the InstantHotstring.ahk script introduced and developed in previous posts—starting with “Create Instant Hotstrings Using the AutoHotkey Hotstring() Function.” In this new endeavor, I add a GUI menu bar which significantly alters my view of the app. The benefits of implementing a GUI menu bar greatly exceed its functional use.

* * *

I consider most of my scripts demonstrations of how to implement AutoHotkey possibilities—not completed applications. I rarely go back to do all the little things that will make a script a finished product—in two senses of the word: virtually completed and fine-tuned. Many of my favorites (QuickLinks.ahk, MousePrecise.ahk, SynonymLookup.ahk, AutoCorrect.ahk, ChangeVolume.ahk, etc.) don’t require much additional work—if any—although, a script rarely achieves perfection. Most of my scripts use menus, Hotkeys, or Hotstrings while running in the background—not requiring extra visual bells and whistles. However, once you base an AutoHotkey script on a GUI (Graphical User Interface) pop-up window, the need for additional finishing touches increases—especially if it opens and saves files.

One of the best methods for finishing an AutoHotkey GUI app involves adding a menu bar. (You might also argue that the writing of a GUI script should start with a menu bar. It creates a road map to the finished product.) On the surface, a GUI menu bar makes the app more user friendly, but, more importantly, the process forces you to rethink the design and structure of your script.

Using GUI Menu Bars in AutoHotkey Scripts

A while back I wrote the blog “Use the GUI Menu Bar for Instant Hotkeys” emphasizing the convenience of setting up keyboard shortcuts via a GUI menu bar system. But for many scripts, adding a menu bar can do so much more by driving your GUI window toward better thought-out subroutines and functions. Implementing a GUI menu bar:

  1. Creates a convenient place to add numerous app actions without consuming extra space in the GUI window.
  2. Makes it possible to separate and highlight individual settings and actions previously buried as options in a series of related actions.
  3. Can match familiar menu structures found in many Windows programs making the app more user-friendly (i.e. Open File, Save File, etc).
  4. Makes adding window specific shortcuts to a GUI app easy without resorting to AutoHotkey Hotkeys.
  5. Forces the rethinking of the script design and structure for modularization of the target settings and actions.

This last rationale (“rethinking of the script design and structure”) may offer the most important incentive for incorporating a GUI menu bar in your script. Until you attempt to break up the functions into separate actions or settings for inclusion in the menu, you may not understand how convoluted your code has become—another argument for starting with a GUI menu bar in the first place.

Adding a Menu Bar to the InstantHotstring.ahk Script

Library Benefits

Some GUIs such as Ryan’s RegEx Tester won’t derive much benefit from an added menu bar—it does what it does very effectively and without much explanation. But for other GUIs—especially those that read and write files—the menu bar adds tremendous functionality.

By inserting a menu bar for accessing various features into the InstantHotstring.ahk script, I developed a clearer picture if how I can reorganize the script—plus, the menu design process generated more ideas for new capabilities. However, it also generated a long list of tasks I must complete before I can claim the new version of the script ready for primetime.

In this series of blogs (I don’t know how many), I plan to document each step and the motivation for my decisions. I know that I will encounter a number of obstacles (I always do!) and I may need to do some major rewrites of the code. Fortunately, I will explore many useful AutoHotkey techniques for adding those little touches that make an okay script more-than-okay.

The File Menu

In my first step, I added a dummy non-functioning menu bar listing the current and planned action items. This menu bar acts as my roadmap. (The process caused me to wonder whether I had enough ambition to make all the readily apparent changes. It appears that I have a long journey before me.) As you would expect, I placed all of the file actions in the File menu:

  • Open File
    Currently, the “Load Hotstrings” button opens the file selection window, then adds any Hotstrings found in the selected file to the current list of active Hotstrings —activating each. But what if I want to clear the old Hotstrings before loading the new file? As with most Window programs, this Open File menu selection should do that.
  • Append File
    The current “Load Hotstrings” button operates in this fashion (as explained above) and should remain an option.
  • Save
    The current “Save Hotstrings” button runs through a series of steps allowing the replacement or addition of Hotstrings to a file. Then, it asks if you would like to set the file as the default for loading on startup. While it increments through important options, the “Save Hotstrings” button does too much in one sweep. The Save menu option should only update the currently open file—and even that should only occur when the script senses any changes. Sensing when data changes adds another task to my to-do list.
  • Save as…
    The current “Save Hotstrings” button serves more as a Save as… menu item than a save button. Modularizing the routines will make it easier to break out individual functions including the following Set default file option.
  • Set default file
    You’ll find this option buried in the “Save Hotstrings” button’s subroutine. It needs an easily-found home of its own.
  • Clear Hotstrings
    In the InstantHotstring.ahk script, resetting the GUI (and Hotstrings) is not as simple as overwriting (or deleting) the text in editing software. A similar approach would require the tedious individual deactivation of each Hotstring, then removal from the GUI DropDownMenu. To circumvent this problem, the current “Start Clean” button—rather than attempting to disable all the Hotstrings and clearing the DropDownList—reloads the script (wiping out the current list of Hotstrings) without importing a default file. With potentially thousands of active Hotstrings loaded, reloading just makes sense—and saves a ton of time. I’ll probably need to incorporate this reload approach in the Open File menu item for importing an lone alternative file.
  • Exit
    Why not?

As you can see, this menu structure has already provided me with an ample to-do list of action items. I must pursue these if I want to turn the script into a more complete app. But, I’m not done yet!

The Edit Menu

I added items for facilitating adding, editing, updating, and searching Hotstrings to the Edit menu:

  • New Hotstring
    Currently, the user creates new Hotstrings by filling in the pertinent fields and hitting the “Set Hotstring” button. If the Hotstring exists, the script updates it, if not, it creates it. I need a trap for creating new Hotstrings while preventing the use of an existing activator.
  • Set Hotstring
    This one should work as is but I won’t know until I get there.
  • Disable/Enable Hotstring
    This one should work as is but I won’t know until I get there.
  • Find Hotstring
    No search function currently exist. When I forget a Hotstring I look for it in the DropDownList. If the list gets too long, it can take forever. I need a search tool for locating and highlighting individual Hotstrings.

The Help Menu

The Help menu offers another challenge for finishing the app. As ideas flood in, I expect that I will make numerous changes to the current menu bar. When the new options occur to me, I plan to document them in this blog.

The new InstantHotstring.ahk GUI menu bar code:

Menu, FileMenu, Add, &Open File`tCtrl+O, MenuBarAction
Menu, FileMenu, Add, &Append File`tCtrl+Alt+O, MenuBarAction
Menu, FileMenu, Add, &Save`tCtrl+S, MenuBarAction
Menu, FileMenu, Add, &Save as…`tCtrl+Shift+S, MenuBarAction
Menu, FileMenu, Add, Set &default file`tCtrl+D, MenuBarAction
Menu, FileMenu, Add, &Clear Hotstrings`tCtrl+K, MenuBarAction
Menu, FileMenu, Add, E&xit`tCtrl+X, MenuBarAction

Menu, EditMenu, Add, &New Hotstring`tCtrl+N, MenuBarAction
Menu, EditMenu, Add, S&et Hotstring`tCtrl+E, MenuBarAction
Menu, EditMenu, Add, &Disable/Enable Hotstring`tCtrl+D, MenuBarAction
Menu, EditMenu, Add, &Find Hotstring`tCtrl+F, MenuBarAction

Menu, HelpMenu, Add, &Help`tCtrl+F1, MenuBarAction
Menu, MyMenuBar, Add, &File, :FileMenu 
Menu, MyMenuBar, Add, &Edit, :EditMenu 
Menu, MyMenuBar, Add, &Help, :HelpMenu
Gui, Hotstring:Menu, MyMenuBar

For testing purposes, the MenuBarAction subroutine:

MenuBarAction:
  MsgBox %A_ThisMenuItem%
Return

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

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.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

One thought on “Radically Improving AutoHotkey GUI Apps with Menu Bars

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s