Applescript 和 iCal 交互 [英] Applescript and iCal interaction
问题描述
我正在尝试编写一个 AppleScript 来查询 iCal 并在任何日历中查找给定日期的所有事件.
I'm trying to write an AppleScript to query iCal and find all the events I've got for a given date, in any calendar.
我首先编写了一个简单的脚本,该脚本对给定日历中的每个事件执行一些简单的操作:
I started by writing a simple script that does something simple with every event in a given calendar:
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
然而,即使我没有在循环内做任何复杂的事情,这个简单的脚本也需要很多时间来执行(几秒钟).恐怕真正的脚本执行起来真的要花很多时间.
However, this simple script is taken a lot of time to execute (a few seconds), even if I'm not doing anything complex inside the loop. I'm afraid that the real script will really take a lot of time to execute.
我对 Applescript 不是很熟悉,因此我想我正在做一些会严重影响性能的愚蠢事情.
I'm not very familiar with Applescript, and thus I imagine I'm doing something silly that has severe performance implications.
谁能解释一下为什么要执行这么多?有人可以建议改进我的代码吗?我现在将开始检查事件的日期,并在循环中设置一个条件.我怀疑一定有一种方法可以搜索带有日期的事件(就像 Automator 操作那样),但我一直无法找到一种本机"方式来这样做......
Can anybody explain me why this takes that much to execute? Can anybody suggest something to improve my code? I'm now going to start checking the date of the event, with a condition in the loop. I suspect there must be a way to search for events with a date (like the Automator action does), but I haven't been able to find a "native" way to do so ....
编辑:我使用的是 Mac OS X Tiger (10.4).较新版本的 iCal 可能改进了可用的操作库.
EDIT: I'm using Mac OS X Tiger (10.4). It is possible that newer versions of iCal have improved the library of operations available.
推荐答案
我今天一直在努力解决这个问题,发现您可以按日期过滤(至少在 Snow Leopard 上).所以
I've been grappling with this today and found that you can filter by date (at least on Snow Leopard). So
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
与遍历所有事件相比,将很快返回所有未来事件的摘要.
will return the summary of all future events, and very quickly, compared to iterating through all events.
这篇关于Applescript 和 iCal 交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!