Pasting Date Parts into Forms (AutoHotkey RegEx Tips Part 4)

In Most Data Fields, You’ll Find It Simple to Paste Text or Numbers, But Inserting a Parsed Date Often Requires Extra Steps—Plus How to Change Date Paste Action Based on the Active Window Title

In the first three blogs of this RegEx series, I used Regular Expressions (RegEx) to identify and parse data prior to any paste operations. In this blog, I use a RegEx to identify and parse dates during the multi-part date paste operation. I use the previously introduced \d wild card (any numeric digit zero through nine) and the optional match modifier (the question mark ?), plus, I introduced how to create a selection list of possible matches by using a range of options (in this case, a forward slash, a dash, or a dot [/\-.] as date separators).

*          *          *

Cover 200When working with forms, dates often use three separate data entry fields—one for the month, one for the day, and one for the year. (In the UK, they swap it around to day/month/year.) In some forms, you need to enter a forward slash (/) to jump to the next entry item. Sometimes the form automatically jumps to the next field after entering two digits. In many Web pages, you need to tab between each data entry area. Sometimes, sending the entire copied date as a single string works.

Since I wrote the MultiPaste.ahk script to respond to a particular personal finance program, I set it up to Send the date delimited with slashes. If you want to do something similar for a different software package or Web page, then you may need to tailor the script. This blog shows you how to do that.

Checking Date Formats

Many date entries require hitting the slash key between entering each portion of the date. However, the exact format accepted by date input fields varies considerably. I set up the MultiPaste.ahk script to identify a numeric date format and enter it into either individual fields separated by slashes or a plain text field. However, it won’t work properly with Web date fields accessed by tabbing.

In the image below, the MultiPaste.ahk script identifies the first item as a date format using a Regular Expressions (RegEx), parses it into its three component parts, then pastes the data into three fields separated by two slashes (i.e. 05/31/2019).

FinSWPasteBlur
This finance program accepts date separated by slashes. Entering a slash jumps the input to the next portion of the date field.

Identifying the Date Format

Library Benefits

In this situation, we use the common RegEx wild card for any numeric digit \d first introduced in the blog “Finding US Zip Codes (AutoHotkey RegEx Tips Part 1)” and the optional match modifier ? added in “Finding UK Postal Codes (AutoHotkey RegEx Tips Part 2).” Similar to US zip codes, numeric dates worldwide usually consists of a set of numbers separated by slashes or dashes (sometimes dots)—depending upon the date format. In my problem, the copied date contains slashes so I wrote the script for that format. I initially identified dates with the following RegEx:

\d\d?/\d\d?/\d\d(\d\d)?

When you copy a date from another source, it may contain one or two digits for the month or day and two or four digits for the year. We account for these possibilities for both the month and day by making the first number mandatory (\d) and the second number optional (\d?). (This also works for British date formats.) Next, we match either a two or four-digit year by making the first two numbers mandatory \d\d and the second two optional (\d\d)?.

Note: I originally wrote the RegEx as “\d\d\d?\d?” but this form would allow a three-digit year.

Ranges in Regular Expressions

What if you copy a date which uses a dash rather than a forward slash as a separator? Rather than write another RegEx for the dashed format, you can modify the current RegEx to include dashes as an option inside a range of values:

\d\d?[/\-]\d\d?[/\-]\d\d(\d\d)?

Including a range of options [/\-] allows the RegEx to match either the slash or the dash.

Note the backslash preceding the dash \- in the above range. Special RegEx characters lose their super powers inside the square brackets of a range. That means the period or decimal point acts merely as a dot—not the anything wild card which the RegEx dot usually represents. However, any characters which do take on special properties in range (such as the dash in a series, i.e. 09, AZ, etc.) do require escaping with the backslash (\). Other such special characters include ^ for do not match, the range square brackets themselves [ ], and the escape backslash \ (e.g. [\^a][a\-b][a\]], and [\\a]).

Tip: If you want to add dates which use the dot (.) as a separator, include the dot inside the range [/\-.]. (As mentioned, you do not need to escape the dot with a backslash inside a range.)

Using the RegExMatch() Shorthand Operator

AutoHotkey offers a shorthand version of the RegExMatch() function (~=) which you can use inside expressions. I often use this technique to validate data or identify certain types of data. By placing the date-identifying RegEx inside the operator, we quickly match possible date formats:

DataString ~= "\d\d?[/\-.]\d\d?[/\-.]\d\d(\d\d)?"

If the RegEx matches the variable DataString, then the expression returns true. After AutoHotkey identifies any item as a date, it breaks (parses) that date into its component parts and uses the SendInput command to insert the date—adding back the forward slashes to force the cursor to jump to the next date piece. Taken from paste portion of the MultiPaste.ahk script:

If (Transaction[SingleKey] ~= "\d\d?[/\-.]\d\d?[/\-.]\d\d(\d\d)?")
{
  If InStr(Transaction[SingleKey],"/")
    date := StrSplit(Transaction[SingleKey],"/")
  If InStr(Transaction[SingleKey],"-")
    date := StrSplit(Transaction[SingleKey],"-")
  If InStr(Transaction[SingleKey],".")
    date := StrSplit(Transaction[SingleKey],".")
  SendInput, % date[1]
  SendInput, /
  SendInput, % date[2]
  SendInput, /
  SendInput, % date[3]
}
Else If SingleKey != ""
  SendInput, % Transaction[SingleKey]
Return

Based upon the location of the slashes, the StrSplit() function parses the date into three component parts date[1]date[2], and date[3]. AutoHotkey then recombines the date in the input fields using the SendInput command one piece at a time.

MultiPasteDatesThis parsing routine offers the advantages of recombining the original copied date with forward slashes—even for dashed or dotted dates. If the form allows the entire date to appear in one field, then it does just that. Any other non-date MultiPaste item, AutoHotkey Sends as plain text.

This works for my application, but what if you want to paste into tab activated Web date fields?

Sending Tabs Between Data Fields

If you require your script to paste into a Web page then you may need to insert tabs (`t) in place of the slash. The tab causes the input cursor to jump to the next field:

SendInput, % date[1] . "`t" . date[2] . "`t" . date[3]

While this will not work for my application, it does insert dates into Web pages which separate the month, day, and year into individual input fields accessed by tabbing between them. However, not all Web pages work in the same manner. If you have a number of places where you need to MultiPaste dates, then you might want to add conditionals to the script which respond to the active window’s title.

Tailor Output to the Window

If you need a number of different paste formats for various programs or Web pages, then, rather than setting up a new Hotkey for each, you can change how the script formats the insertion date based upon the window title of the currently active application or Web page.

Identify WinTitle (or the beginning text of WinTitle) with the Window Spy utility (right-click any .ahk icon in the System Notification Tray and select Window Spy from the context menu) or use the WindowProbe.ahk script.

The first few words a the window title, when included in the conditional:

If WinActive(WinTitle)

return true for any matching active window using that same WinTitle. This allows you to change AutoHotkey action based upon the active window:

^!9::
If (WinActive("TurboTax"))
{
  MsgBox TurboTax program
}
If (WinActive("Aer Lingus"))
{
  MsgBox Aer Lingus Web page
}
If (WinActive("Fly smart"))
{
  MsgBox Alaska Airlines Web page
}
Return

Substitute the proper date insertion code in place of the MsgBox command lines above.

Note that the Alaska Airlines Web page uses the words “Fly smart” at the beginning of the window’s title for its active tab in your Web browser.

You may need to experiment with your form date field to find the best method for inserting the parsed date (or entering it all in one piece), but, once you find a technique that works, you can isolate it to matching programs or Web pages using the WinActive() function.

In this blog, I briefly reviewed the \d wild card (any digit zero through nine) and the optional match modifier (the question mark ?). I introduced how to create a selection list of options by using a range of options [/\-.].

Cover 200

These blogs only scratch the surface of what you can do with Regular Expressions (RegEx). Many of my scripts use them—if only in a small way. I encourage anyone who wants to maximize their AutoHotkey scripts to dig into how RegEx works. They can get complicated but often a simple RegEx makes a difficult problem simple.

Next, I plan to introduce an AutoHotkey script which totals numbers found in any documents. It also makes extensive use of Regular Expressions. Plus, I plan to build a GUI interface for the selection of various types of numbers (i.e. dollars, British pounds, Euros, any set of numbers, etc).

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

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 )

Connecting to %s