gpt4 book ai didi

Applescript 和 iCal 交互

转载 作者:行者123 更新时间:2023-12-04 02:45:03 27 4
gpt4 key购买 nike

我正在尝试编写一个 AppleScript 来查询 iCal 并在任何日历中查找给定日期的所有事件。

我首先编写了一个简单的脚本,它对给定日历中的每个事件执行一些简单的操作:

tell application "iCal"
tell calendar "Reuniones"
set the_events to every event
repeat with an_event in the_events
-- do something with every event
set value to summary of an_event
end repeat
end tell
end tell

但是,即使我在循环中没有做任何复杂的事情,这个简单的脚本也需要很长时间才能执行(几秒钟)。恐怕真正的脚本执行起来真的要花很多时间。

我对 Applescript 不是很熟悉,因此我想我正在做一些对性能有严重影响的傻事。

谁能解释一下为什么这需要这么多才能执行?任何人都可以提出一些改进我的代码的建议吗?我现在要开始检查事件的日期,在循环中有一个条件。我怀疑必须有一种方法可以搜索带有日期的事件(就像 Automator 操作一样),但我一直无法找到一种“本地”方法来这样做......

编辑:我使用的是 Mac OS X Tiger (10.4)。较新版本的 iCal 可能改进了可用的操作库。

最佳答案

我今天一直在努力解决这个问题,发现您可以按日期过滤(至少在 Snow Leopard 上)。所以

tell application "iCal"
set out to ""
set todaysDate to current date
set time of todaysDate to 0
repeat with c in (every calendar)
set theEvents to (every event of c whose start date ≥ todaysDate)
repeat with current_event in theEvents
set out to out & summary of current_event & "\n"
end repeat
end repeat
return out
end tell

将返回所有 future 事件的摘要,并且与遍历所有事件相比非常快。

关于Applescript 和 iCal 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/928273/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com