Wednesday, September 22, 2021

#1 2021-09-09 05:38:30 pm

bocciaman
Member
Registered: 2019-07-22
Posts: 12

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


Filed under: , applescript, TextExpander

Offline

 

#2 2021-09-09 06:49:14 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1043

Re: AppleScript to list 5 days of the week

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.

Applescript:

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.

Applescript:

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

Last edited by peavine (2021-09-10 07:16:10 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#3 2021-09-09 08:08:29 pm

bocciaman
Member
Registered: 2019-07-22
Posts: 12

Re: AppleScript to list 5 days of the week

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

Last edited by bocciaman (2021-09-09 08:09:49 pm)

Offline

 

#4 2021-09-10 01:12:04 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5426

Re: AppleScript to list 5 days of the week

peavine wrote:

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.


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.

Applescript:

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:

Applescript:

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

Last edited by Nigel Garvey (2021-09-10 05:16:06 am)


NG

Offline

 

#5 2021-09-10 02:35:11 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 880

Re: AppleScript to list 5 days of the week

Here is other way that use NSCalendarUnitWeekday. The weekday return integer from
1 (Sunday) to 7 (Saturday) so to get Monday to Friday I use 2 - 6 in if statement.

OP like to have a list of weekdays so this example does not do that but instead use current date

Applescript:

use framework "Foundation"
use scripting additions

set theCal to current application's NSCalendar's currentCalendar()
set theComp to theCal's components:(current application's NSCalendarUnitWeekday) fromDate:(current application's NSDate's |date|())
set theDay to theComp's |weekday|()

if theDay is 2 then
   return "Monday" & space & (current date)'s month & space & (current date)'s day & "," & space & (current date)'s year
else if theDay is 3 then
   return "Tuesday" & space & (current date)'s month & space & (current date)'s day & "," & space & (current date)'s year
else if theDay is 4 then
   return "Wednesday" & space & (current date)'s month & space & (current date)'s day & "," & space & (current date)'s year
else if theDay is 5 then
   return "Thursday" & space & (current date)'s month & space & (current date)'s day & "," & space & (current date)'s year
else if theDay is 6 then
   return "Friday," & space & (current date)'s month & space & (current date)'s day & "," & space & (current date)'s year
end if

Last edited by Fredrik71 (2021-09-10 02:38:51 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#6 2021-09-10 08:08:33 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1043

Re: AppleScript to list 5 days of the week

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


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)