Adding Web Links to the AutoHotkey IPFind.ahk Script

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.

An IP address site can provide a great deal of information—including approximate longitude and latitude.

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

Continue reading

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!"
Continue reading

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.

Continue reading

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.

Continue reading

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

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

I received the following query from a reader:

Regular Expressions in AutoHotkey
Regular Expressions (RegEx) can be mysterious in any language.

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

Continue reading

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.

DateConvertSend
When combined with the HowLongYearMonthDay.ahk script, the DateStampConvert.ahk script directly converts various ambiguous date formats selected in documents or Web pages into the standard datetime stamp format for inserting into the time-span calculating GUI pop-up.

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.

Continue reading

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.

I hope you’ll consider correcting/updating this article.

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.

Continue reading

Sending E-Mail and AutoHotkey

After Working Out the Kink’s, AutoHotkey Sends Individual E-Mails Smoothly

RobotEmailCartoon

The scourge of the Internet, Spam haunts our daily lives—whether in the form of phishing e-mails or unwanted phone calls. While never eliminated, we minimized its impact through filtering and blocking. As a side effect of our efforts, we now commonly check our Spam folder when searching for an errant missive. Due to this problem e-mail providers now add layers of protection to their servers—usually in the form of what content we can transmit, message size, and the number of e-mails sent in a specific period of time.

Generally, we never think about these limitations because our local e-mail program restricts us enough to prevent our abusing the system. This confines us safely within the parameters of our e-mail provider. Only setting up our own e-mail server removes these restrictions.

It is important to understand that sending a mass email through your Gmail does have some limits (a total of more than 500 recipients in a single email and or more than 500 emails sent in a day). There is a maximum of email recipients a user can have in one single email, as well as a maximum amount of emails a user can send in 24 hours. It will not work by sending them at 11:50 pm and again at 12:05 am; the system requires a full 24 hours to pass.

How to Send Mass Email in Gmail – Few Easy Options
Continue reading

Adapting Web Scraping Routines to Changing Web Pages (AutoHotkey Tip)

When the Horoscope Web Page I Use for E-mails Altered Its Format, I Quickly Adjusted the Script

Last year, I wrote a script that e-mails a daily horoscope to my wife, “E-mail the Daily Horoscope to Yourself (AutoHotkey Trick).” Every morning she receives on her tablet an e-mail containing her daily horoscope. (I don’t send it to myself because I don’t want to know that much about my future—and I don’t listen to advice.) Recently, she pointed out that the e-mail started coming up blank. I immediately realized that the target Web site had changed its source code. (I’ve experienced the same problem with the SynonymLookup.ahk script.) I knew I could repair the Regular Expression (RegEx) in the broken script fairly quickly by following some basic steps:

  1. Access the source code for the target Web page and locate the key text.
  2. Copy the critical portion of the source code, including any unique HTML tags surrounding the target text, then paste the selection into Ryan’s RegEx Tester.
  3. Adjust the RegEx to include key unique tags surrounding the text—then extracting the paragraph.
  4. In the script, replace the old RegEx found in the RegExMatch() function with the new one from Ryan’s RegEx Tester.
  5. Make any necessary adjustments to the RegEx—primarily escaping double quotation marks.

The new horoscope e-mail script now includes more details and a link to the site.
Continue reading