2010-04-20 45 views
0

我試圖讓applescript讀取今天的ical事件並將它們寫入文件。代碼如下:使用applescript將iCal數據寫入文件

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 

的iCal的項目越來越正確返回,並在文件夾中的文件/ DesktopImage正確地創建了文件ical.txt,但該文件是空白。

即使我代替

write out to the_file 

write "Test string" to the_file 

它仍然出現空白

什麼想法?

Ty

回答

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") 
+0

作品一個款待。謝謝 – Guy 2010-04-21 08:43:20