Fake Math Tricks Using the Floor() and Mod() Functions (AutoHotkey Tips)

Although Not Really Fake Math, These Two AutoHotkey Functions Can Solve Eclectic, Yet Practical, Everyday Problems!

The Irish philosopher George Berkeley proved that mathematics does not exist in reality. It turns out nobody cares! Even though math resides only in our heads, we continue to use it anyway.

Berkeley did not doubt that calculus produced real-world truth; simple physics experiments could verify that Newton’s method did what it claimed to do. “The cause of Fluxions cannot be defended by reason”,[48] but the results could be defended by empirical observation, Berkeley’s preferred method of acquiring knowledge at any rate. Berkeley, however, found it paradoxical that “Mathematicians should deduce true Propositions from false Principles, be right in Conclusion, and yet err in the Premises.” In The Analyst, he endeavoured to show “how Error may bring forth Truth, though it cannot bring forth Science”.[49] Newton’s science, therefore, could not on purely scientific grounds justify its conclusions, and the mechanical, deistic model of the universe could not be rationally justified.[50]

https://en.wikipedia.org/wiki/George_Berkeley#Philosophy_of_mathematics

Fortunately, we don’t need to agree with (or even understand) the above citation to take advantage of the results from mental calculation. Scientists may show concern about the theoretical but engineers only care about what works.

Calculating Dates in AutoHotkey by Adding Years, Months, and/or Days

The HowLongYearsMonthsDays.ahk Script Calculates Time Spans—This New DateCalc() Function Yields New Dates Based on Adding or Subtracting Years, Months, or Days

Allan recently inspired me to write (and helped me debug) an AutoHotkey function for calculating new dates based on entering years, months, and/or days. While not long or involved, the function includes a couple of interesting techniques.

Note: I have no doubt that many others have written similar date-calculating functions in AutoHotkey. This merely represents my stab at it.

The lack of consistency in days between 12-month years (leap years) and the varying numbers of days in months underlies the basic date calculating problem. While AutoHotkey includes a special tool (EnvAdd) for determining new dates based upon adding days, hours, minutes, or seconds to any valid date-time stamp format, writing a more complete date finding function requires accounting for the year and month variables separately.

Using Parts to Build a New AutoHotkey Script (HowLongInstant.ahk)

While Many Users Find the Original GUI Based HowLong Script Valuable, Combining Snippets of Code Creates a New Instant HowLong Script

Last time in “Extracting Multiple Dates from Text Using AutoHotkey RegEx,” I wrote a Regular Expressions (RegEx) that copied the first and last date (in a variety of formats) found in a selection from a document or Web page. (I recently updated that RegEx to make it more robust.) That represented the first step in building an instant HowLongYearsMonthsDay.ahk script. The goal, as defined by the reader, included highlighting a section of text which bounds two dates, pressing a Hotkey combination, then immediately calculating and displaying the timespan—no delaying the process with an input GUI or clicking a calculate button. As with many new scripts, I took pieces of it from other scripts and integrated them to produce a new one.

The chunks I used to produce the new script included:

1. The Standard Clipboard Routine for capturing the selected text.
2. The RegEx for identifying and capturing the target dates. (Discussed in my last blog.)
3. The DateConvert() function found in the DateStampConvert.ahk script for formatting the parsed dates as the standard TimeDate stamp (YYYYMMDD).
4. The HowLong() function found in the HowLongYearsMonthsDays.ahk script for calculating the timespan between the two TimeDate stamp parameters.
5. A MsgBox for instantly displaying the results.

Extracting Multiple Dates from Text Using AutoHotkey RegEx

While Not Simple (and a Little Bit “Greedy”), the RegEx for Two-Date Parsing Only Requires One Selection

Hi! Is it possible to highlight the entire date range (e.g. 16 March 2021 to 21 May 2021) when the Hotkey is triggered, feed it into the timespan ahk, and share the timespan as result?

Working with AutoHotkey Date Formats and Timespan Calculations

Yes, it is! You’ll find using Regular Expressions (RegEx) to simultaneously parse the two dates from the text the key to success. Plus, you’ll want to streamline the process by eliminating the GUI and feeding the dates directly into the HowLong() function found in HowLongYearsMonthsDays.ahk script. Implementing the instant calculation requires three steps:

1. Writing a RegEx for identifying and capturing the target dates. (Discussed in this blog.)
2. Using DateStampConvert.ahk code to format the parsed dates in the standard TimeDate stamp (YYYYMMDD).
3. Calculate the timespan by running the HowLong() function using the two dates as parameters.

This approach should provide you with an instant timespan calculation between any two dates matched in a text selection.

I have not done all the work, but I have developed a RegEx which locates the first and last date in a text selection;

``````sx)(\b[[:alpha:]]+.?\s\d\d?,?\s\d?\d?\d\d|\b\d\d?[-\s]?[[:alpha:]]+[-\s]?\d\d\d?\d?|\b\d\d?[-/]\d\d?[-/]\d\d\d?\d?)
.*(\b\[[:alpha:]]+.?\s\d\d?,?\s\d?\d?\d\d|\b\d\d?[-\s]?[[:alpha:]]+[-\s]?\d\d\d?\d?|\b\d\d?[-/]\d\d?[-/]\d\d\d?\d?)``````

Update March 26, 2021: `\w` in original RegEx changed to `[[:alpha:]]` to include only alphabetic characters.

While I don’t discuss every aspect of this RegEx here, I cover the important aspects of its construction. (I’ve written numerous blogs and an entire book discussing the basics of AutoHotkey Regular Expressions.)

Working with AutoHotkey Date Formats and Timespan Calculations

AutoHotkey Date and Time Calculations Require Special Handling—Check Out This List of How-to’s for Working with Dates

Over the years, I’ve written a number of blogs and many chapters about formatting and calculating dates, but one of my AutoHotkey apps that I think most demonstrate the full range of these capabilities include the scripts HowLongYearMonthDay.ahk and DateConvert.ahk.

Rather than using AutoHotkey commands for converting the standard datetime stamp into one of the numerous worldwide date formats, this conversion tool does the reverse and reformats selected dates into the universal datetime stamp.

When I first wrote the HotstringMenu.ahk script, I planned it as a substitution for ambiguous or multiple option Hotstring text replacements. After typing one of the activation strings, a menu of alternative replacements pops-up. Since the Hotstring menus include many varied item lists, it made sense to create each only when needed. However, when I decided to combine a number of these menus into a group under one main menu, preloading the numerous static submenus seemed more practical. That left me with one problem. Some menus, such as current time and date insertion, need updating each time they pop up.

AutoHotkey Tip of the Week: A Look at the New Switch/Case Command

In the DateStampConvert.ahk Script, Rather than Using a Series of If-Else Statements (or the Ternary Operator), the New Switch Command Sets Up Case Statements for Alternative Results—Plus, Easily Add Conversions for Spanish, German, French, and Italian Date Formats

Over a year ago, I used a cascading series of the ternary operators to convert English text month names into their numeric values within a single function (“Use the Ternary Operator to Create Conditional Case Statements or Switches“). The ternary operator shortcut acts as If-Else statements in abbreviated form.

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

*          *          *

When 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. Continue reading

Save AutoHotkey Script Settings in Your Windows Registry

While You Can Use an External File to Save Your Script Defaults, You Can Also Call-on the Windows Registry to Store App Settings

In most cases, I’ve used a separate file (text or INI) to save settings. While this works well, it requires the creation and tracking of that independent file. For the script to read the settings, it must know where to locate that file. But, if you want to save script settings without the baggage of that extra file, then consider using your Windows Registry.

You’ll find a few advantages to maintaining your script settings in the Windows Registry:

1. Unlike a separate settings file, you don’t need to keep track of the Windows Registry’s location—it never moves. Regardless of where you locate your AutoHotkey script on your computer, you will always find its settings in the exact same place.
2. You won’t accidentally lose your settings by manually deleting what may seem like an extra file in Windows File Explorer.
3. Your settings remain semi-hidden from public view in a semi-permanent form.

This approach to storing your script defaults deep inside the recesses of Windows adds an air of mystery to your apps—especially if you compile them into EXE files. Continue reading

Wrapping Up the DateStampConvert.ahk Script (AutoHotkey Tips)

In Previous Blogs, I Used Regular Expressions to ID Dates Formats in Documents and Simulated Case/Switch Statements to Convert Month Names to Numbers. Now, I Build the Standard DateTime Stamp, Check for Valid Dates, and Deal with Two-digit Years, Plus Use the Function ByRef Method to Bypass Local Variables.

While in conversations with a reader who uses AutoHotkey to calculate the time span between two dates for figuring out new leases, I realized that a tool which captures formatted dates from any document and converts them into the DateTime Stamp format (yyyymmdd) would make using the HowLongYearsMonthsDays.ahk script even easier. That prompted me to write the DateStampConvert.ahk script. Continue reading