Add Methods for Inserting Any Future or Past Date or Time into Your Windows and Web Documents with AutoHotkey GUI (Graphical User Interface) Pop-ups Using Hotstring Menus.

As a convenience for people who want to get this entire series (plus more) in one place, I have now published the e-book Beginning AutoHotkey Hotstrings which is available on the ComputorEdge E-Books site (in three formats—EPUB for mobile devices, MOBI for Kindle, and PDF for printing and computer) and through Amazon. Regardless of whether you’re interested in the book or not, it’s worth your time to peruse some of the blogs linked at “Beginning AutoHotkey Hotstring Techniques” found under the “AutoHotkey Topics and Series” tab in the top menu bar. They just might inspire your next AutoHotkey script.

* * *

When I first started working on this series about AutoHotkey Hotstrings, I thought it would consist of only a couple of blogs covering the use of Hotstring options. However, as I worked on each blog I kept getting more practical ideas about how AutoHotkey Hotstrings could create an easier life for any Windows computer user. (See the top level blog for a list of the past articles about Hotstrings.) This road has finally brought me to a place where more powerful AutoHotkey features warrant an introduction. Admittedly, the techniques discussed here are becoming slightly more complex, but none of them are beyond the capability of a novice script writer.

This time I delve into areas which take advantage of a couple of the built-in AutoHotkey GUI (Graphical User Interface) windows. I’ve cover these powerful little pop-ups in most of my other books. (In particular, the book AutoHotkey Applications discusses almost all of the GUI types and gives at least one practical script for each.) I will not attempt to explain every nuance of the two date/time GUIs discussed here and in the upcoming blog. You can find that information in the linked online documentation or my other AutoHotkey books. (All the books are also available at Amazon for the Kindle.)

Expanding the Date/Time Hotstring Menu with Future and Past Options

The AutoHotkey MonthCal GUI makes it simple to add dates with any Hotstring by popping up a complete calendar.

The first addition to the date/time Hotstring menu is the MonthCal GUI control (seen at right). This pop-up is an intuitive calendar interface allowing you to select any day from January 1, 1601 to December 31, 9999. GUI controls are loaded and launched with the GUI command.

There are a few additional pieces of code required to construct and activate a GUI control. Fortunately, once understood, all of the various GUIs are similar in syntax and function which makes using them much easier. (You might consider this blog a mini tutorial on GUI controls, although much more is possible with the various pop-ups than what I cover here. For a glance at what the available pop-ups look like, see “The Use and Images of AutoHotkey GUI Control Popup Windows.”)

The time/date Hotstring from the last blog is now modified to display the MonthCal GUI in response to hitting, as the activating end character (A_End_Char), the semicolon (;) key rather than the hyphen (–):

The Hotstring first checks the text date for termination with the hyphen (-). If not (Else) it checks for the semicolon (which must be escaped with the backtick `;). The GUI routine runs only when the Hotstring is activated with the semicolon.

Truthfully, the Else If structure is not needed here. A single If would suffice in this example. The Else If is only needed when 1) only one true option (the first one encountered) is allowed; and/or 2) the last condition included is an Else for those times when no other condition is true. In this case, since the two options are mutually exclusive and there is no general alternative, the simple If conditional is adequate. However, since I may add other options in the future, the Else If structure may come in handy.

Building a GUI (Graphical User Interface)

GUIs are constructed with the Gui, Add command. Each Gui, Add command line of code places another control (i.e Text, Edit, UpDown, Picture, Button, Checkbox, Radio, DropDownList, ComboBox, ListBox, ListView, TreeView, Link, Hotkey, DateTime, MonthCal, Slider, Progress, GroupBox, Tab, StatusBar) on the current pop-up window. In the example above, the MonthCal control and a Button control named Submit are added with the two Gui, Add lines.

The MonthCal control line creates a variable called DayPick. (The vDaypick option at the end of the line of code creates the new variable.) DayPick will ultimately save the selected date for further processing in the Hotstring.

Note: The name AddDate: had been added to the Gui commands to differentiate this pop-up window from others created in the same script. It’s not necessary to use such a name in a standalone script with only one GUI, but it prevents conflicts with other GUIs if more than one GUI is created. Once a GUI name is applied, it must be carried through the remainder of the GUI commands and any automatically created Label subroutines, as discussed later.

(The following If conditional:

IfWinExist, Select Date Gui AddDate:Destroy

has a specific purpose for preventing errors and is discussed later in this blog.)

The Submit Button is used to trigger the DayPick save (and other) action. The Gui, Show command displays the pop-up window on the screen and assigns it the title Select Date. Every GUI must utilize the Gui, Show command to make the window visible (and functional) on the computer display.

It’s critical to understand that nothing is saved from the GUI until until the Gui, Submit command is issued. This can be done by including the standard Label subroutine AddDateButtonSubmit: in the script:

The Button GUI control (if there is no gLabel option included) automatically uses a Label subroutine (if it exists) by the name ButtonSubmit:— which includes the word Button and the any text appearing on the button (Submit). However, if there is also a name for the GUI itself (i.e. AddDate), then it must be concatenated (added) to the front of the new Label name (i.e. AddDateButtonSubmit). If this Label subroutine does not exist (and there is no gLabel option specified), then the Button control will not respond when the GUI window is operational.

The AddDateButtonSubmit: subroutine runs when the Submit button is clicked. The first line of the subroutine uses the Gui, Submit command (Gui, AddDate:Submit) to save the selected date in the variable DayPick.

Warning: Novices commonly forget to include the Gui, Submit command in their GUI built-in or gLabel subroutines. Then, even though data changes appear in a GUI window, they wonder why the selections or additions weren’t saved.

Destroying the GUI to Prevent Errors

As discussed in A Beginner’s Guide to AutoHotkey, a common error from using a GUI which includes the vVariable option occurs when attempting to recreate the same GUI a second time. Since the variable already exist from the first loading of the GUI, an error is thrown when AutoHotkey attempts to recreate the variable on a second load. This is certainly an issue with Hotstring GUIs which are often called multiple times in the same session. There are a couple of ways to prevent this problem, although one of the easiest is after each use to destroy the GUI (Gui, AddDate:Destroy). That’s the reason for the inclusion of the Gui, AddDate:Destroy line of code.

Also, looking back to the original Hotstring, the added IfWinExist conditional checks for the existence of the pop-up from a previous usage and, if found, deletes it:

IfWinExist, Select Date
Gui AddDate:Destroy

This is necessary for those times when the user takes no action, ignores the GUI, and simply moves on. The windows still exists and will throw an error if there’s an attempt to recreate it.

If the window is closed by clicking the tiny x in the upper right-hand corner, then the window is merely hidden and not deleted. To prevent this from causing the same variable creation error the following built-in Label subroutine is included:

AddDateGuiClose:
Gui AddDate:Destroy
Return

This standard Label subroutine is named GuiClose. The subroutine (if included in the script) runs every time a GUI window is closed by clicking the tiny x in the upper right-hand corner. As with the AddDateButtonSubmit Label, the GUI name AddDate must be added to the front of GuiClose (i.e. AddDateGuiClose).

The remainder of the subroutine includes the two functions called in the AddDateButtonSubmit: subroutine: DateFormats(DayPick) and TextMenuDate(List)—discussed in some detail in the last blog. Rather than repeat those two functions, I’ve included them is a working script I call DateMenus.ahk. It can be downloaded from in the ZIP file DateMenus.zip located at the ComputorEdge AutoHotkey download site.

Also from last time, the script contains the action Label subroutine MenuAction:.

The key change made to MenuAction: was the addition of the WinActivate command. This made the Hotstring replacement in the any document more reliable when a separate window (the GUI) has been opened and closed.

What about the Time?

On the down side the MonthCal GUI provides only the date—not the time. The time always appears as 12:00 AM for any selected dates. Fortunately there is another AutoHotkey GUI control called DateTime which does allow the setting and saving of a specific time—as well as the date. In the next blog, we’ll add the DateTime GUI control to the time/date Hotstring.