Instantly Add Dates to Your Documents in Different Formats with Hotstring Menus, (Part 9, Beginning Hotstrings)

Don’t Know Which Time or Date Format You Need for Your Next Document or Web Form? Pick from a Hotstring Menu of Options.

*          *          *

Beginning AutoHotkey Hotstrings 200pxAs 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.

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

 *          *          *

When it comes to times and dates there are no easy mathematical formulas. The standards don’t fit into any reasonable numeric system such as base 10—the total of the fingers and thumbs on your two hands. There are 365 (or 366) days and 12 months in a year; anywhere between 28 and 31 days in  a month; 24 hours in a day; 60 minutes in an hour; and 60 seconds in a minute. See a pattern here? Neither do I. That means from a computer standpoint, there is no simple way to insert a time or date into a document. Fortunately, most programming languages, including AutoHotkey, possess some form of time/date calculating/formatting command for dealing with this awkward problem.

Beginner's Guide to AutoHotkeyYet, even with special time/date commands, when added to the fact that times continually change, AutoHotkey Hotstrings offer no simple one-line commands for entering the time or date. Even the simplest form of Hotstring requires real-time calculation.

In A Beginner’s Guide to AutoHotkey, I highlighted the following Hotstring for inserting the current date into any document:

:c*:Anow:: 
  FormatTime, CurrentDateTime,, MMMM d, yyyy 
  SendInput %CurrentDateTime%
Return

Whenever the text Anow is typed (case sensitive), the FormatTime command is used to pull the current date (the default value) and set it to month, day, and year. Next, SendInput inserts the formatted date into the current edit field at the location of the cursor.

This works great when you want this standard format date (e.g. November 5, 2015). In spite of that, there are times when you may need any number of other possible time and/or date formats. Wouldn’t it be useful to see a menu of different format options pop up on command?

While from previous blogs we already know how to create a menu with Hotstrings, we need two more features to include more time and date formats:

  1. A function for formatting today’s time and date into the styles we are most likely to use (DateFormats()).
  2. A slightly modified menu function which accommodates the special needs of date formats which often include commas (TextMenuDate()).

This new date Hotstring is designed to only activate when it is followed a special character. In this case, the hyphen (-) acts as the activating key. By using the B0 option (no backspace) and the C option (case sensitive), the word date will not be deleted unless it is followed by a hyphen and in all lowercase letters. The Hotstring snippet of code is as follows:

:B0C:date::
If A_EndChar = -
{
  SendInput {Backspace 5}
  Date := A_Now
  List := DateFormats(Date) ;creates a list of date formats
  TextMenuDate(List)  ;modified to accommodate commas
}
Return

In this limited case, we could eliminate the If conditional which checks the built-in A_EndChar variable by writing the Hotstring code with the trailing hyphen included:

:B0C:date-::

However, this would limit the future use of the Hotstring when we add features for alternative times and dates. Checking the A_EndChar variable allows us to change the action based upon the last key entered. (More on that in the next blog.)

Tip: In Part 6 of this series, I used the TAB key (`t) as the activating character in a Hotstring. It is probably best to avoid using TAB—especially when working with Web text editing fields. In Web pages and many programs, the TAB is used to jump to the next field. If there are multiple editing fields, then, with TAB as the activator, it is quite possible that AutoHotkey will jump to the next field before doing a replacement. This was happening to me with the WordPress online software.

In the above example, whenever the last character is the hyphen, the first action is to delete the string (SendInput {Backspace 5}) before loading the menu.

The variable Date is set to the current time and date with the build in variable A_Now (Date := A_Now). All that remains is to build a list of time/date formats, then load them into a selection menu.

Since the date is unknown until the time of insertion, the replacement cannot be determined in advance and hardcoded into the script. While the code for making a list of time and date formats could be added directly to the above snippet, putting it in a function allows us to reuse it later (which we will do next time for alternate future and/or past times and dates).

This Hotstring includes two new functions: DateFormats(Date) and TextMenuDate(List). The DateFormats(Date) function uses the FormatTime command to create a list of assorted useful time/date formats for the menu. By setting the variable List equal to the returned value of the function DateFormats(Date), the results are stored to the variable for later use:

DateFormats(Date)
{
 FormatTime, OutputVar , %Date%, h:mm tt ;12 hour clock
 List := OutputVar
 FormatTime, OutputVar , %Date%, HH:mm ;24 hour clock
 List := List . "|" . OutputVar
 FormatTime, OutputVar , %Date%, ShortDate ; 11/5/2015
 List := List . "|" . OutputVar
 FormatTime, OutputVar , %Date%, MMM. d, yyyy
 List := List . "|" . OutputVar
 FormatTime, OutputVar , %Date%, MMMM d, yyyy
 List := List . "|" . OutputVar
 FormatTime, OutputVar , %Date%, LongDate
 List := List . "|" . OutputVar
 FormatTime, OutputVar, %Date%, h:mm tt, dddd, MMMM d, yyyy
 List := List . "|" . OutputVar
 FormatTime, OutputVar, %Date%, dddd MMMM d, yyyy hh:mm:ss tt
 List := List . "|" . OutputVar
 Return List
}
Time/Date Formats
The time and date is available in various formats.

The above function repeats the same code line employing FormatTime to build a list of times and dates. Since so many date formats use commas, it is beneficial to change the list delimiter from a comma to the vertical line (|)—later used for parsing the list into the menu items. (The vertical line is commonly used as a delimiter in scripting and is probably a better choice for all of your Hotstring menu lists.)

Note that the routine saves the different formats in the variable List by concatenating, or adding variables together, by placing dots, each surrounded with spaces, on each side of the delimiting character ( . “|” . ) in front of each additional format. This adds the new date/time item to the end of the previous value of List. This is a common technique used for compiling multiple items into one variable for later use.

The Return command (Return List) is used to send the results back to the calling routine. It’s essential to remember that there is no conflict between the variable List in the Hotstring and the variable by the same name in the function DateFormats(Date). Although, they both possess identical names, they are two separate variables. The variable List inside the function is local and only exists within the function. Therefore, the outside List must be loaded with the Return command.

The same effect can be achieved by adding the Global command (Global List) to the beginning of the DateFormats(Date) function. Then there would only be one variable named List. The Return List line continues to be required, while the List := DateFormats(Date) line of code can be shorten by merely calling the function with DateFormats(Date).

The list of formatted times and dates is sent to the TextMenuDate(List) function.

TextMenuDate(TextOptions)
{
  StringSplit, MenuItems, TextOptions , |
  Loop %MenuItems0%
  {
    Item := MenuItems%A_Index%
    Menu, MyMenu, add, %Item%, MenuAction
  }
  Menu, MyMenu, Show
  Menu, MyMenu, DeleteAll
}

The only difference between this function and the menu building function in previous blogs is the use of the vertical line (|) as a delimiter for parsing the list. Again, using a comma as a delimiter is problematic when there are other commas inside the entries in the list.

The Label subroutine MenuAction: is essentially the same as in previous examples except I’ve added the WinActivate command. I found that with some of the examples I will discuss in later blogs, the primary window loses focus and does not properly replace the activating text. Adding WinActivate helps to ensure that the last window is on top and in focus:

MenuAction:
  WinActivate
  If A_ThisMenuItem Is Alpha
  {
    SendInput %A_ThisMenuItem%{Raw}%A_EndChar%
  }
  Else
    SendInput %A_ThisMenuItem%
Return

I’m not sure that WinActivate is needed for the current exercise, however, when using other pop-up windows in the upcoming examples, it definitely makes the Hotstrings more reliable.

Next time, we add more capabilities to this Hotstring by including pop-up GUI (Graphical User Interfaces) control pop-up windows for selecting alternative times and dates.

*         *          *

Find Jack’s AutoHotkey e-Books at ComputorEdgeBooks.com.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s