Applescript空列表停止执行

时间:2013-09-17 17:31:56

标签: calendar applescript

我正在编写一个AppleScript来从订阅的日历中获取所有事件,并将其转换为另一个日历中的allday事件。以下是我目前不完整的代码:

tell application "Calendar"
    tell calendar "Canvas"
        set listEvents to every event whose allday event is false
    end tell

    tell calendar "Update"

        set noMatchList to {}
        set listAllDayEvents to every event whose allday event is true
        if listAllDayEvents is null then
            set listAllDayEvent to {0}
        end if
        repeat with firstEvent in listEvents
            repeat with secondEvent in listAllDayEvents
                if firstEvent is not equal to secondEvent then
                    set end of noMatchList to firstEvent
                end if
            end repeat
    end repeat
...

我遇到的问题是,如果listAllDayEvents为空,即更新日历中没有allday事件,则执行将停止,并且永远不会进入if语句。有什么问题,是否有办法绕过它?

1 个答案:

答案 0 :(得分:1)

两件事。

不确定为什么使用null。

请改用{}。你也错过了变量名末尾的“s” 在:

 set listAllDayEvent to {0}

应该是:

set listAllDayEvents to {0}

<强>更新

另外,如果我明白你要做什么。我认为你的代码中的逻辑有点偏。

您应该在第一次重复中测试 listAllDayEvents 中的项目。

如果在 listAllDayEvents 中找到了项目,请检查它们是否在第二次重复中匹配。

如果 listAllDayEvents 中没有项目,则无需进入第二次重复。

只需将 listEvents 中的所有项目添加到 noMatchList 列表中,以便稍后进行处理。

tell application "Calendar"
    tell calendar "Canvas"
        set listEvents to every event whose allday event is false
    end tell

    tell calendar "UpDate"

        set listAllDayEvents to every event whose allday event is true

    end tell

    set noMatchList to {}

    repeat with firstEvent in listEvents
        if listAllDayEvents is not {} then

            repeat with secondEvent in listAllDayEvents
                if firstEvent is not equal to secondEvent then
                    set end of noMatchList to firstEvent
                end if
            end repeat

        else

            set end of noMatchList to firstEvent
        end if

    end repeat

end tell


注意:任何想要测试此代码的人。创建两个新日历,每个日历上都有几个事件。而不是使用任何您可能建立的日历。 日历可能非常大,特别是当他们有一些重复事件或回溯多年。 这意味着您可能整天都在等待脚本完成运行

相关问题