计算下周的事件数量

时间:2013-07-29 20:05:05

标签: date calendar applescript

我需要计算下几周的事件数量。但我的方法太慢了(工作时间超过1.5小时)。我怎么能加快这个脚本?目前我每次weekNumber都会收到日历的所有事件......这很慢但是我无法弄清楚我如何才能获得所有事件一次并且每个事件都有weekNumber

tell application "Calendar"

    set theCalendars to name of calendars

    repeat with theCalendar in theCalendars

        set listCurrentCalendar to {}
        set end of listCurrentCalendar to theCalendar

        repeat with weekNumber from 1 to 13
            set currentWeekMonday to my DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Monday, weekNumber)
            set nextWeekMonday to my DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Monday, weekNumber + 1)

            set numberOfEvents to 0
            set TheEvents to events of calendar theCalendar
            repeat with anEvent in TheEvents

                set eventStartDate to start date of anEvent
                if eventStartDate ≥ currentWeekMonday and eventStartDate ≤ nextWeekMonday then
                    set numberOfEvents to numberOfEvents + 1
                end if
            end repeat

            set end of listCurrentCalendar to numberOfEvents
        end repeat

        set end of listData to listCurrentCalendar
    end repeat

end tell

1 个答案:

答案 0 :(得分:0)

以下是两周的例子:

set myEvents to {}

tell application "iCal"
    set myCalendars to calendars
    repeat with aCalendar in myCalendars
        set calEvents to (every event of aCalendar whose start date > (current date) and end date < ((current date) + (2 * weeks)))
        if calEvents ≠ {} then set myEvents to myEvents & calEvents
    end repeat
end tell