AppleScript to list 5 days of the week

Is there a way to list all 5 days of the week with AppleScript? Like so…

Monday, August 30, 2021
Tuesday, August 31, 2021
Wednesday, September 01, 2021
Thursday, September 02, 2021
Friday, September 03, 2021

I’ve seen lots of scripts that give you a specific day but none that list all the five days of the workweek?

Model: 2009 iMac
AppleScript: 2.7
Browser: Google Chrome Version 93.0.4577.63
Operating System: macOS 10.14

This is a simple solution to your request. The user specifies the date of the desired Monday, and the script creates a list which is coerced to text.

set startingDate to date "September 13, 2021"

set dateList to {date string of startingDate}
repeat with i from 1 to 4
	set theDate to startingDate + i * days
	set end of dateList to date string of theDate
end repeat

dateList --> {"Monday, September 13, 2021", "Tuesday, September 14, 2021", "Wednesday, September 15, 2021", "Thursday, September 16, 2021", "Friday, September 17, 2021"}

set {TID, text item delimiters} to {text item delimiters, linefeed}
set dateList to dateList as text
set text item delimiters to TID

dateList

This script uses a starting date of the Monday of the current week. It’s clumsy and I suspect there’s a much better solution.

set theCurrentDate to (current date)
set theWeekday to weekday of theCurrentDate

if theWeekday = Monday then
	set startingDate to theCurrentDate
else if theWeekday = Tuesday then
	set startingDate to theCurrentDate - 1 * days
else if theWeekday = Wednesday then
	set startingDate to theCurrentDate - 2 * days
else if theWeekday = Thursday then
	set startingDate to theCurrentDate - 3 * days
else if theWeekday = Friday then
	set startingDate to theCurrentDate - 4 * days
else
	error number -128
end if

set dateList to {date string of startingDate}
repeat with i from 1 to 4
	set theDate to startingDate + i * days
	set end of dateList to date string of theDate
end repeat

dateList --> {"Monday, September 6, 2021", "Tuesday, September 7, 2021", "Wednesday, September 8, 2021", "Thursday, September 9, 2021", "Friday, September 10, 2021"}

set {TID, text item delimiters} to {text item delimiters, linefeed}
set dateList to dateList as text
set text item delimiters to TID

dateList -- a string of paragraphs of the dates

Exactly, what I needed. Thank you!

Model: 2009 iMac
AppleScript: 2.7
Browser: Google Chrome Version 93.0.4577.63
Operating System: macOS 10.13.6

Hi peavine.

You could take advantage of the fact that AppleScript’s weekdays can be coerced to integer, both explicitly and implicitly, with Sunday being 1 and Saturday 7.

tell (current date) to set precedingSaturday to it - (its weekday) * days
set dateList to {}
repeat with i from 2 to 6
	set theDate to precedingSaturday + i * days
	set end of dateList to date string of theDate
end repeat

dateList

Another way to get the date of a particular preceding weekday — more for fun nowadays — is to calculate from a known date with that weekday in the past:

set now to (current date)
copy now to knownSaturday
tell knownSaturday to set {its day, its month, its year} to {1, January, 2000}
set precedingSaturday to now - (now - knownSaturday - days) mod weeks - days

Thanks Nigel. I wasn’t aware one could coerce a weekday to an integer, and that’s very useful.