Delete events in iCal within a range of dates?

Hi,

I have a script which deletes events from the current date forward to the end of the month. I currently have this version of my script working…

set theDate to (current date)

-- myStartDate is actually from the rest of the script and is the start date of an event already added to iCal. Just using +24 hours to make the script work on it's own.
set myStartDate to ((current date) + 24 * hours)

-- To select the appropriate calendar

tell application "iCal"
	activate
	set myCals to name of every calendar whose writable is true
	set theCal to (choose from list myCals with prompt "Attach to which calendar?" & return & return & "This will become your default calendar every time!" without multiple selections allowed and empty selection allowed) as text
	set defaultCal to theCal as text
end tell


--To determine the end date of the current month

set tid to AppleScript's text item delimiters -- save them for later.
set the text item delimiters to {""}
set start_date to date (short date string of (current date)) -- midnight of current date
set day of start_date to 1 -- midnight of first day of current month
copy start_date to end_date
set month of end_date to ((month of end_date as integer) + 1) -- midnight of first day of next month
set end_date to end_date - 1 -- 23:59:59 of last day of current month	
set AppleScript's text item delimiters to tid -- back to original values

--if the .ics month is the current month then only delete the current months events forward from todays date

if month of myStartDate is equal to month of theDate then
	
	tell application "iCal"
		tell calendar defaultCal
			set futureEvents to events whose start date > (current date)
			repeat with anEvent in futureEvents
				if start date of anEvent is less than (current date) then
				else
					if start date of anEvent > end_date then
					else
						delete anEvent
					end if
				end if
			end repeat
		end tell
	end tell
else
	
	--if the .ics month is next month then only delete next months events
	
	if month of myStartDate as integer is equal to ((month of theDate) + 1) as integer then
		
		tell application "iCal"
			tell calendar "Untitled"
				set futureEvents to events whose start date > end_date
				repeat with anEvent in futureEvents
					if start date of anEvent is greater than next_end_date then
					else
						delete anEvent
					end if
				end repeat
			end tell
		end tell
	end if
end if

I get an .ics file with events for the current month or next month. If my events change this script finds all the events in my particular calendar and deletes any event forward of the current date to the end of the current month or if the .ics file is for the following month, then all of those following month events (and none of the current month’s events)

My problem is in the line of code:

set futureEvents to events whose start date > (current date)

and

set futureEvents to events whose start date > end_date

this slows down my script considerably because it has to look forward forever to find all events. I can’t seem to constrain the set futureEvents variable to only set events that are after the current date but only until the end of the current month OR only events in the next month.

My best effort was this, … it doesn’t work

set futureEvents to every event whose (month of start date) is equal to (month of (current date))

Any help would be greatly appreciated.

Regards,

Kev

You can combine conditions/filters like so:

tell (current date) to set {lastDay, its day, its month} to {it, 1, (its month) + 1} -- first day of next month

set lastDay to lastDay - 1 * days -- last day of current month

-- and set its time to 23:59:59 (24:00:00 will rollover to 0:00:00 next day)
tell lastDay to set its time to 24 * 60 * 60 - 1

set now to current date
tell now to set its time to 0

tell application "iCal"
	tell calendar "myFlights"
		set futureEvents to events whose start date > now and start date < lastDay
	end tell
end tell

(It’s advisable to not do ‘current date’ within a tell block: it’s not an application command)

Thank you alastor933

This isn’t the first time you’ve come to my aid… That saved me lines of code and sped up the performance of my script.

Again, Thank you.

PS. Starting a new post with a question about monitoring a file download.