Using AppleScript to Perform Date Math and Create Calendar Events

I have an idea for an AppleScript that would allow me to quickly set up calendar events. This would AppleScript would do 4 things, and I only know how to accomplish two of these steps (creating a calendar event). With any luck, one of the AppleScript wizards will have some ideas.

Is it possible to write AppleScript that does the following?

  1. Prompt the user with a list of dates, which are the next 14 Sundays. It would look like:
    July 25
    August 1
    August 8
    August 15
    (and so on)

  2. Once the user chooses one Sunday, it will create a calendar event on that Sunday lasting from 8:30am to 12pm. The event will have a title, a URL, and a location.

  3. After it creates this event, it will find the Wednesday that happened before that Sunday. In other words, it will subtract 4 days from the selected Sunday.

  4. Finally, it will create an event lasting from 7:30pm to 9pm on that particular Wednesday. The event will have a title, a URL, and a location.

I already have AppleScript that can create calendar events at specific dates with URLs and locations included. I am having trouble making steps 1 and 3 happen.

set selectedDate to date (item 1 of (choose from list dateList with prompt "Select Date"))

a list of dates, which are the next 14 Sundays

set dateList to {}
set date1 to (current date) + 1 * days
repeat until weekday of date1 as integer is 1 --Sunday
	set date1 to date1 + 1 * days
end repeat
repeat with weekIndex from 1 to 14
	set the end of dateList to (date1 + (weekIndex - 1) * 7 * days) as string
end repeat

it will find the Wednesday that happened before that Sunday. In other words, it will subtract 4 days from the selected Sunday.

set wedDate to selectedDate - 4 * days

Thanks so much! That was the final piece I needed. Here is the completed script.

  --Each calendar event has the same URL.
tell application "Google Chrome"
	set ChromeURL to URL of active tab of first window
end tell

set CalendarURL to the text returned of (display dialog "Planning Center URL?" default answer ChromeURL)
-- display dialog CalendarURL

--Let's find out what the last Sunday was.
set theDate to ((current date) - 1)
repeat until (weekday of theDate) = Sunday
	set theDate to theDate - 1 * days
end repeat

-- Choose from many upcoming Sundays.
set dateList to {}
set the end of dateList to theDate + 7 * days as string
set the end of dateList to theDate + 14 * days as string
set the end of dateList to theDate + 21 * days as string
set the end of dateList to theDate + 28 * days as string
set the end of dateList to theDate + 35 * days as string
set the end of dateList to theDate + 42 * days as string
set the end of dateList to theDate + 49 * days as string
set the end of dateList to theDate + 56 * days as string
set the end of dateList to theDate + 63 * days as string
set the end of dateList to theDate + 70 * days as string
set the end of dateList to theDate + 77 * days as string
set the end of dateList to theDate + 84 * days as string
set the end of dateList to theDate + 91 * days as string
set the end of dateList to theDate + 98 * days as string
set the end of dateList to theDate + 105 * days as string
set the end of dateList to theDate + 112 * days as string
set the end of dateList to theDate + 119 * days as string
set the end of dateList to theDate + 126 * days as string
set the end of dateList to theDate + 133 * days as string
set the end of dateList to theDate + 140 * days as string
set the end of dateList to theDate + 147 * days as string
set the end of dateList to theDate + 154 * days as string
set the end of dateList to theDate + 161 * days as string
set the end of dateList to theDate + 168 * days as string

set selectedDate to date (item 1 of (choose from list dateList with prompt "Select Date"))

copy selectedDate to sundayStartDate
copy selectedDate to sundayEndDate

-- Start Date for Sunday.
set hours of sundayStartDate to 8
set minutes of sundayStartDate to 30
set seconds of sundayStartDate to 0

-- End Date for Sunday.
set hours of sundayEndDate to 12
set minutes of sundayEndDate to 0
set seconds of sundayEndDate to 0

-- Start Date for Wednesday.
set wedStartDate to selectedDate - 4 * days
set hours of wedStartDate to 19
set minutes of wedStartDate to 30
set seconds of wedStartDate to 0

-- End Date for Wednesday.
set wedEndDate to selectedDate - 4 * days
set hours of wedEndDate to 21
set minutes of wedEndDate to 0
set seconds of wedEndDate to 0

-- Make Wednesday Event (Rehearsal)
tell application "Calendar"
	set theCurrentDate to current date
	set newEvent to make new event in calendar "Personal" at end with properties {summary:"Church Rehearsal", location:"Church", start date:wedStartDate, end date:wedEndDate, url:ChromeURL}
	tell newEvent
	end tell
end tell

-- Make Sunday Event (Service)
tell application "Calendar"
	set theCurrentDate to current date
	set newEvent to make new event in calendar "Personal" at end with properties {summary:"Church Worship", location:"Church", start date:sundayStartDate, end date:sundayEndDate, url:ChromeURL}
	tell newEvent
	end tell
end tell