Skip to content

Instantly share code, notes, and snippets.

@ajot
Created August 26, 2023 12:41
Show Gist options
  • Select an option

  • Save ajot/556cfdb6abd3a227a6e5e06b23a14963 to your computer and use it in GitHub Desktop.

Select an option

Save ajot/556cfdb6abd3a227a6e5e06b23a14963 to your computer and use it in GitHub Desktop.
Automating Weekly Review Date Headers with AppleScript and TextExpander
-- Blog Post: https://ajot.me/finding-the-current-weeks-start-and-end-dates-in-applescript/
-- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment