iCal Event sort by date

Hello all,

I am getting a list of events back from ical (successfully), but the returned list’s order depends upon the whent he event was created, not the event start date. I expected the line
“set these_events to sort these_events by start date” to sort them, but I am getting
“Syntax Error: Expected end of line, etc. but found identifier.” when I compile.

I am new to AppleScript and imagine that I’m missing something basic: I’ve been digging though google and the forums here and haven’t had any luck. Many thanks in advance for any help.


-- returns a list of events with the prefix supplied in argv
--params         calendar name
--			todo prefix

on run argv
	
	tell application "iCal"
		--		activate
		using terms from application "iCal"
			
			if (count of argv) is 0 then set argv to {"work", "CCD"}
			
			set calName to item 1 of argv
			set this_prefix to item 2 of argv
			set returnVal to {}
			
			set this_calendar to (the first calendar whose title is the calName)
			tell this_calendar
				--try  --comment out for debugging
				set these_events to (every event whose start date is greater than or equal to the (current date) and summary starts with this_prefix)
				
				set these_events to sort these_events by start date
				set myLength to length of these_events
				
				repeat with aNum from 1 to (myLength)
					set thisevent to (item aNum of these_events)
					set temp to summary of thisevent & " on " & start date of thisevent
					set returnVal to returnVal & temp
				end repeat
				
				return returnVal
				--end try
				return null
			end tell
			
		end using terms from
	end tell
	
end run

My approach is to grab all the summaries and dates as separate lists, then use the handler below to sort them by date while keeping the summaries in corresponding order. It’s easy to extend this to more than two separate lists by repeating the way the second list is handled with a third, fourth, etc.

to sort2Lists(sortList, SecondList) -- sorts on the first, keeps the second in sync.
	-- This version is much faster than the other if the lists are large.
	script O
		property L1 : missing value
		property L2 : missing value
	end script
	set O's L1 to sortList
	set O's L2 to SecondList
	tell (count O's L1) to repeat with i from (it - 1) to 1 by -1
		set s to (O's L1)'s item i
		set r to (O's L2)'s item i
		repeat with i from (i + 1) to it
			tell (O's L1)'s item i to if s > it then
				set (O's L1)'s item (i - 1) to it
				set (O's L2)'s item (i - 1) to (O's L2)'s item i
			else
				set (O's L1)'s item (i - 1) to s
				set (O's L2)'s item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > (O's L1)'s end then
			set (O's L1)'s item it to s
			set (O's L2)'s item it to r
		end if
	end repeat
	return {O's L1, O's L2}
end sort2Lists

Hi,

iCal doesn’t know a sort command, so the events must be sorted “externally”

on run argv
	tell application "iCal"
		if (count of argv) is 0 then set argv to {"work", "CCD"}
		set calName to item 1 of argv
		set this_prefix to item 2 of argv
		set returnVal to {}
		
		set this_calendar to (the first calendar whose title is the calName)
		tell this_calendar
			--try  --comment out for debugging
			tell (every event whose start date is greater than or equal to the (current date) and summary starts with this_prefix) to set {these_events, these_dates} to {it, start date of it}
			set these_events to my sort_items(these_dates, these_events)
			set myLength to length of these_events
			
			repeat with aNum from 1 to (myLength)
				set thisevent to (item aNum of these_events)
				set temp to summary of thisevent & " on " & start date of thisevent
				set returnVal to returnVal & temp
			end repeat
			-- end
			return returnVal
		end tell
	end tell
end run

to sort_items(sortList, SecondList)
	script L
		property srt : sortList
		property sec : SecondList
	end script
	tell (count L's srt) to repeat with i from (it - 1) to 1 by -1
		set s to (L's srt)'s item i
		set r to (L's sec)'s item i
		
		repeat with i from (i + 1) to it
			tell (L's srt)'s item i to if s > it then
				set (L's srt)'s item (i - 1) to it
				set (L's sec)'s item (i - 1) to (L's sec)'s item i
			else
				set (L's srt)'s item (i - 1) to s
				set (L's sec)'s item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > (L's srt)'s end then
			set (L's srt)'s item it to s
			set (L's sec)'s item it to r
		end if
	end repeat
	return L's sec
end sort_items

Notes:
¢ using terms from is only needed, if the argument for tell application is not a literal string
¢ a run handler with parameters works actually only in a script object

Many, many thanks for the help and explaination. I had been banging my head against the wall for a while and knew I was missing several key things!