Skip to content

Instantly share code, notes, and snippets.

@snown
Created September 29, 2011 17:32
Show Gist options
  • Save snown/1251355 to your computer and use it in GitHub Desktop.
Save snown/1251355 to your computer and use it in GitHub Desktop.
TextExpander AppleScript Snippet for next or previous X day of the week
(*
AppleScript for use in a TextExpander snippet to print out the date of the next Monday (or whatever day of the week you define) in the format of "Monday 13 April 2011"
Maintained at https://gist.github.com/1251355
*)
(* Set Weekday and Month names in your language of choice here (for example, French) *)
set day_names to {"dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"}
set month_names to {"janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"}
(* Set your goal day of the week
1 = Sunday
2 = Monday
3 = Tuesday
4 = Wednesday
5 = Thursday
6 = Friday
7 = Saturday
*)
set goal_day to 2 -- Monday
(* Set if you want to go to the next or previous week, by setting calculating_next_week to either true or false respectfully *)
set calculating_next_week to true
-------------------------------------------------------
-- DO NOT MODIFY PAST THIS POINT --
-------------------------------------------------------
set theDate to (current date)
set d to (goal_day - (weekday of theDate as integer))
if calculating_next_week then
if d > 0 then
set theDate to offsetDate(theDate, d) -- If current day of the week is before the end day of the week (Monday) just add the number of days until the end day
else
set theDate to offsetDate(theDate, (d + 7)) -- If current day of the week is after the end day of the week (Monday) just add a week to the date, and then add the negative number of days between current day of week, and the goal day of week. (i.e. subtract the difference in days from a week)
end if
else
if d < 0 then
set theDate to offsetDate(theDate, d) -- If current day of the week is after the end day of the week (Monday) just add the negative number of days until the end day
else
set theDate to offsetDate(theDate, (d - 7)) -- If current day of the week is before the end day of the week (Monday) just subtract a week to the date, and then add the negative number of days between current day of week, and the goal day of week. (i.e. subtract the difference in days from a week)
end if
end if
return ((item (weekday of (theDate)) of day_names) & " " & (day of theDate) & " " & (item (month of (current date)) of month_names) & " " & (year of theDate)) as string
on offsetDate(aDate, anOffset)
copy aDate to newDate
if ((newDate's day) + anOffset) ≤ 0 then
set {year:y, month:m} to newDate
set m to ((m as integer) - 1)
if m ≤ 0 then
set {y, m} to {y - 1, m + 12}
set newDate's year to y
end if
set newDate to date ((m & "/01/" & y) as string)
set newDate's day to 32
set newDate to date ((m & "/" & (32 - (newDate's day) + (anOffset + (aDate's day))) & "/" & y) as string)
else
set newDate's day to ((newDate's day) + anOffset)
end if
return newDate
end offsetDate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment