使用applescript将iCal数据写入文件

时间:2010-04-20 19:19:41

标签: applescript icalendar

我正在尝试使用applescript来阅读今天的事件并将它们写入文件。代码如下:

set out to ""
tell application "iCal"
set todaysDate to current date
set time of todaysDate to 0
set tomorrowsDate to todaysDate + (1 * days)
repeat with c in (every calendar)
set theEvents to (every event of c whose start date ≥ todaysDate and start date <      tomorrowsDate)
repeat with current_event in theEvents
 set out to out & summary of current_event & "
 "
end repeat
end repeat
end tell

set the_file to (((path to documents folder) as string) & "DesktopImage:ical.txt")
try
open for access the_file with write permission
set eof of the_file to 0
write out to the_file
close access the_file
on error
try
close access the_file
end try
end try

return out

正确返回正确的项目,并在Documents / DesktopImage文件夹中正确创建文件ical.txt,但文件为空。

即使我替换

 write out to the_file

 write "Test string" to the_file

它仍然是空白

任何想法?

1 个答案:

答案 0 :(得分:1)

要解决问题,首先需要找出问题所在。您可以在调用错误处理程序时获取错误消息,然后显示它:

on error msg
    display dialog "Error accessing " & (the_file as string) & ": " & msg

执行此操作后,您很快就会发现问题:the_file是字符串,而不是文件,因此所有文件操作都将失败。将the_file定义为:

set the_file to alias (((path to documents folder) as string) & "DesktopImage:ical.txt")