views:

171

answers:

1

I am trying to get applescript to read todays ical events and write them to a file. The code is as follows:

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

The ical items are getting returned correctly and the file ical.txt is created correctly in the folder Documents/DesktopImage, but the file is blank.

Even if I substitute

 write out to the_file

with

 write "Test string" to the_file

it still comes up blank

any ideas?

Ty

+1  A: 

To fix something, you first need to find out what's wrong. You can get the error message when your error handler is invoked, then display it:

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

Once you do this, you quickly see the problem: the_file is a string, not a file, so all the file operations will fail. Define the_file as:

set the_file to alias (((path to documents folder) as string) & "DesktopImage:ical.txt")
outis
Works a treat. Thanks
Guy