# Build a Barebones Web Browser Using the AutoHotkey ActiveX GUI Control

## Want to Add a Web Page to a Graphical User Interface (GUI) Window? Use the ActiveX GUI Control!

I’ve often seen the ActiveX GUI control sitting in the AutoHotkey online documentation without investigating it. Only recently when I contemplated adding maps to my IPFind.ahk GUI did my curiosity reactivate my interest. The ActiveX control adds a stripped-down version of an Internet Explorer window to your GUI pop-ups.

## While Fixing the IPFind.ahk Script for Listing the Geographic Location of an IP Address, I Added Links for the IP Identification Site and OpenStreetMap

Occasionally, Web page scraping apps fail (or display strange results) due to changes in source page data formats. It usually only takes a few minutes to review the code and make the necessary RegEx adjustments to restore acceptable results. This time while repairing the IPFind.ahk script, I noticed that the Web page source code also offered IP longitude and latitude. I thought, “Why not add a map link to the display window for anyone curious about its geographic position?” The IP site (which I also added as a link) includes a map, but I wanted one with greater detail.

Note: I recently discussed the Link GUI control in “Turn Web Addresses into Hotlinks for the AHK File Peek Window.”

# Working with Classic Units of Measurements (AutoHotkey Tricks)

## Figuring Odd or Even, Working with Fractions, and Calculating Quarts, Pints, and Cups

Last time in “Fake Math Tricks Using the Floor() and Mod() Functions (AutoHotkey Tips),” I introduced math techniques based upon the Floor() and Mod() functions. Although making basic calculations more complicated, many age-old units of measurement (e.g. yards, feet, and inches with fractions or gallons, quarts, pints, and cups with fractions) persist in common use among everyday endeavors. By obtaining the results from old-fashion division arithmetic showing quotients and remainders, we can write AutoHotkey routines for calculations in these varied types of units.

In order to provide more insight into how to use the Floor() function (quotient without remainder) and Mod() function (remainder or modulo), I offer a few examples of how to build calculators for these types of classic measurements.

### Odd or Even?

Depending upon your line of work, you may or may not need to determine the parity (odd or even) of an integer. As humans, we instantly recognize odd or even with a glance at the last digit (1, 3, 5, 7, 9 odd or 0, 2, 4, 6, 8 even). Computers must make a calculation—the easiest in AutoHotkey divides the number by 2 and looks at the remainder using the Mod() function:

``````test := 35
MsgBox % Mod(test, 2)=1 ? test " is odd!" : test " is even!"``````

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

# AutoHotkey Speed Tips

## A Collection of Techniques for Speeding Up Your AutoHotkey Scripts

I recently received the follow message from an AutoHotkey user perusing my blogs:

Jack, you can’t post false statements like this:

“However, the ternary does not provide better performance than the traditional If-Then-Else statement format.”

The ternary operator is inarguably faster than if/else and I encourage you to try this yourself.

I recently switched the core logic of my JSON parser from If/Else to ternary and saw an incredible speed increase. To the point where I only use If/If Else/Else statements when absolutely necessary (Ex: If I must have a loop in the middle of a check).

You can code entire blocks using nothing but ternary as long as you use proper parentheses, commas, and function calls.

There is a very well done AHK forum post that covers script optimizations and they report that ternary performs FORTY PERCENT FASTER than if/else statements.

Not sure if hyperlinks are allowed in these comments, so instead I’ll advise googling “AHK How to optimize the speed of a script as much as possible.” The first result should be the article in question. Pages 1 and 4 have tons of script speed gold in them.

Groggy Otter

Comment on “AutoHotkey Toggles and the Ternary Operator”

I wrote that blog five years ago and I don’t know that I was talking about performance speed. I think I was referring to how the two expressions operate. In any case, I used a poor choice of words and have drawn a line through the comment.