Amit Jotwani

Amit Jotwani

Automating Weekly Review Date Headers with AppleScript and TextExpander

Tinkering

While reviewing my week in Day One, I like to kick things off with a heading that reads something like "Week of Date 1 to Date 2." Sounds simple, right? But almost always, there's a bit of friction to this. I have to pause my reflection, switch gears, and open up my calendar app to find the start and end dates of the week. Then, I manually type them out. All these steps disrupt my flow.

So, I automated this task using AppleScript, and now a simple TextExpander shortcut does it all for me. I just type wk; and voila, the script fills in the week's date range.

Here's how you can do it too.

The Complete AppleScript

Although you can paste this script directly into TextExpander (choosing "AppleScript" as the content type), I find saving and using the Script Editor for initial testing helpful because it provides code formatting and allows you to debug easily.

-- Get the current date
set currentDate to current date

– Get the current day of the week as a number (Sunday is 1, Monday is 2, etc.) set currentDay to weekday of currentDate as integer

– Calculate the date of the beginning of the week (Monday) – We use “currentDay - 2” because in AppleScript, Monday is represented by 2. – So, we’re asking, “How many days back do we have to go to reach Monday?” set beginningOfWeek to currentDate - (currentDay - 2) * days

– Calculate the date of the end of the week (Sunday) – We use “8 - currentDay” to find out how many days we need to move forward to get to Sunday. set endOfWeek to currentDate + (8 - currentDay) * days

– AppleScript’s current date includes the time. We reset this to focus only on the date by setting start to 00:00:00 and end of day to 23:59:59. set time of beginningOfWeek to 0 set time of endOfWeek to (23 * hours + 59 * minutes + 59)

– Function to convert date to string in the desired format on formatDate(theDate) set theWeekday to weekday of theDate as string set theMonth to month of theDate as string set theDay to day of theDate as integer set theYear to year of theDate as integer return theWeekday & “, " & theMonth & " " & theDay & “, " & theYear end formatDate

– Format the dates set formattedBeginningOfWeek to formatDate(beginningOfWeek) set formattedEndOfWeek to formatDate(endOfWeek)

return formattedBeginningOfWeek & " to " & formattedEndOfWeek

Testing the script in Script Editor

  1. On your Mac, open the Script Editor application.
  2. Copy the entire AppleScript code above and paste it into a new Script Editor window.
  3. Save the file, say GetCurrentWeek.applescript
  4. Then click "Run" to execute it. If everything is set up correctly, you should see the week's date range.

Optional: If you want the date format to look different, you can tweak the formatDate function in the script. Make your changes, run the script again, and see if you like the output.

Using the Script with TextExpander

Now that we have the script tested the script, the next step is integrating it with TextExpander for seamless use. This way, you can fire off this script anytime, anywhere, just by typing a quick "abbreviation shortcut"

  1. If you haven't already, download and install TextExpander from their official website.
  2. Open TextExpander and create a new snippet. Choose "AppleScript" from the content type dropdown.
  3. Copy the AppleScript code you've prepared and paste it into the snippet content area.
  4. This is the shortcut you'll use to trigger the snippet. For instance, you could set it to wk;
  5. Save your snippet. Open a text editor, type your abbreviation - wk; and see if the week's date range gets filled in as expected.

What do you think? Share Your Hacks!

So, that's my little hack for making weekly reviews less of a chore. What about you? Got any neat tricks for planning your week? Or maybe there's something you keep doing manually and wish you could automate?

Hit me up on Twitter @amit. I'd genuinely love to hear your thoughts or swap some productivity tips.