A Trick for Inserting Next Friday’s Date into Any Document (AutoHotkey Tip)

An AutoHotkey Technique for Determining Date for Any Coming Day of the Week, Plus a Pop-up for Picking Future Weekdays

“Do You Know Next Friday’s Date?”

We record upcoming events on our monthly calendars, but we live one week at a time. Most people work Monday through Friday and relax on Saturday and Sunday. If someone gives us a date for an occasion, we ask, “What day is that?”—meaning “Give me the day of the week.”

*          *          *

Library Benefits

I’m currently working on a new book which includes a great deal of unpublished AutoHotkey information, plus many of the current blogs, updated and revised with new techniques (including AutoHotkey Version 2.0 tips). The size of the book imposes a massive problem. I can’t offer the tome on Amazon due to the online retailer’s autocratic rules for independent publishers, but, when ready, it will appear at ComputorEdge E-Books.

Plus, writing this blog creates an obstacle to getting the new book out. Every time I write a new blog, I say to myself, “This one should appear in the book.” That means the book continually expands while very little book editing gets done. At this rate, I’ll never get it out!

I need to draw a line under the number of chapters in the first edition and finish the book. But, “What about the numerous other blogs which deserve a place in the book?”

I decided to turn this next book into a “living book”—meaning it will continue to grow even after publication. Anyone who owns the book will get all future editions without any additional cost. Plus, as part of the ComputorEdge AutoHotkey Library, members get their first copy of this expanding book at a tremendous discount, plus, all future editions without any further charge.

I expect to publish this new AutoHotkey tips volume in the Fall of this year. At least, that’s my goal. Some of my future blogs (such as this one) will highlight particular chapters in the new book—although without as much detail as appears in the tome. When ready, I’ll announce the book in this blog. Then I’ll immediately start working on adding more material for the next edition.

*          *          *

 

MonthCalGUIConverting from dates to weekdays offers a particularly challenging problem for computers. In this blog, I investigate an AutoHotkey tip for adding next Friday’s date to any document. You can target any day of the week, but for me, Friday’s date acted as a pivotal weekly point since ComputorEdge Magazine posted to the Web on that day. I could use the AddDate.ahk script found at the “ComputorEdge Free AutoHotkey Scripts and Apps” site which pops up the calendar input graphic (shown at left), but that returns the date in the standard American format (January 24, 2018). I wanted an abbreviated format (1-24-18) which AutoHotkey inserts instantly after typing a short Hotstring (AFri).

This AutoHotkey trick works for calculating the date for any chosen day of the week. You only need to change the constant numeric weekday value for the target day. A person could set up a different Hotstring for each weekday (ASun, AMon, ATue, AWed, AThu, AFri, and ASat), but I only needed Friday. (You can find both a function for calculating dates and a menu technique for weekday date selection in the AddNextWeekday.ahk script.)

I do a quick calculation using today’s date to determine next Friday’s date. AutoHotkey offers all the tools to do the job:

:c*:AFri::
  Today := A_Now
  Today += 6-A_WDay, days
  FormatTime, Friday, %Today%, M-d-yy
  SendInput %Friday%
Return

Notice that I use a short and easy to remember Hotstring (AFri for “Add Friday”). The c option between the first two colons makes the Hotstring case sensitive. Without the case sensitive option, if I typed the word “Africa” it would convert the first part to Friday’s date (1-24-18ca). I know of no word which starts with AFri—the first two letters capitalized. We include the * option to immediately add the date the instant we type the letter i at the end of the Hotstring.

Tip: You’ll discover Hotstrings (short text entries) often act as a better option for running subroutines than Hotkey combinations (usually activated in conjunction with the CTRL, WIN microsoft_key, ALT, or SHIFT key). If you pick the right text, you’ll find Hotstrings much easier to remember while not interfering with other Windows shortcuts or program key combinations.

To calculate Friday’s date we start with today (Today := A_Now). The built-in AutoHotkey variable A_Now contains the current computer date.

Next, the script increments today’s date to Friday using the shortcut version of the EnvAdd command (Today += 6-A_WDay, days). If using the full EnvAdd command, you’ll find the equivalent expression interchangeable: EnvAdd, today, 6-A_WDay, days. This line of code adds the number of days from today’s date to the next Friday.

The built-in AutoHotkey variable A_WDay contains today’s day of the week in numeric form (Sunday = 1, Monday = 2, and so on) with Friday assigned 6. To calculate how many days until the next Friday, subtract the current numeric day of the week from Friday (6). The days parameter tells AutoHotkey to do the time calculation in day increments.

In the above example, Saturday (7) does not work since it produces a negative number and jumps to the prior, rather than the following Friday. To correct this problem, add a conditional for Saturdays:

Today := A_Now
  If A_WDay = 7
    Today += 6, days
  Else
    Today += 6-A_WDay, days

If you write the code for any other day of the week, then change the conditional to If A_WDay > 6—replacing the number 6 throughout the snippet with the weekday number of the target day and that number plus one week (seven days). For example, this snippet deals with all possibilities for next Wednesday’s date:

Today := A_Now
  If A_WDay > 4
    Today += 11-A_WDay, days
  Else
    Today += 4-A_WDay, days

If greater than 4 (Wednesday), then 4-A_WDay plus one week (add 7 days to get the 11) yields the next Wednesday date. Otherwise (Else), the next Wednesday occurs 4-A_WDay days later. (In this example, I would change the Hotstring to AWed.)

The FormatTime command puts Friday’s date in the proper format (FormatTime, Friday, %today%, M-d-yy) and stores it in the variable Friday. The parameter M returns the month without a leading zero (1 – 12); d the day of the month without leading zero (1 – 31); and yy the two-digit year.

Lastly, the SendInput command replaces the Hotstring with the value of Friday (SendInput %Friday%) in the current Windows document or input field. The Return command terminates the list of the Hotstring commands.

AutoHotkey Version 2.0 Note For Calculating Dates

The EnvAdd command does not exist in AutoHotkey V2.0. The incrementing DateAdd() function and DateDiff() function do the job in V2.0. Plus, the V1.1 FormatTime command converts to the V2.0 FormatTime() function:

:c*:AFri::
  Today := DateAdd(A_Now, 6-A_WDay, "days")
  Friday := FormatTime(today, "M-d-yy")
  SendInput Friday
 Return

This V2.0 example shows the original AFri script without the correction for Saturday.

Rather than repeating the code for each weekday, I streamline the script by creating a function for our AddNextWeekDay.ahk Hotstrings. I explain how it works in detail in the forthcoming book, but you can find the code for it now at the “ComputorEdge AutoHotkey Free Scripts” page. Plus, the same script includes the codes for the Weekday pop-up menu shown below.

A Menu of Multiple Fridays in Multiple Formats

Using the function I developed for the chapters in the new book, I implemented the Input statement technique found in  “More Hotstring Tricks Using the Input Command and a Data Table (AutoHotkey Legal Lingo Tips)” and the pop-up menu from the thesaurus SynonymLookup.ahk script “Build Your Own Dream Thesaurus Word Replacement Tool (AutoHotkey Web Application).” Thus, I created a pop-up selection menu for the next four Sundays (or Monday, Tuesday, Wednesday, etc):

AddWeekdays
After activating the CTRL+ALT+N Hotkey combination, you get ten seconds to enter the first three letters of the desired weekday (e.g. fri) before it times out. The script pops up a menu of the next four weekdays displaying both American and British date formats. Pick the desired date to enter it into a document.

While I don’t know how often people need to access a menu offering the next four Sundays in multiple date formats, this script furnishes a pretty cool solution. After executing the Hotkey combination CTRL+ALT+N (^!n), you merely need to type the first three letters of the target weekday to open a menu offering numerous date formats—including both American and British. Select one to insert it into any document or Web editing field.

Note: The items displaying the labels “American” and “British” act as non-functional informational headers.

I’ve found combining the Input statement with a menu structure a useful technique for creating simulated Hotstrings—especially for difficult to remember terms such as those found in “More Hotstring Tricks Using the Input Command and a Data Table (AutoHotkey Legal Lingo Tips).” I’ve included this Input/Menu script in the AddNextWeekDay.ahk file.

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 )

Facebook photo

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

Connecting to %s