write ical events to a new word doc

I’m trying to gather all the event summaries and dates for a given calendar and then past the list into a new Word document. I have no problem finding the information in ical, but how do I push this into a new MS Word doc? Also, is there a way to combine the summary and date into one string? When I try set newSummary to summary and start date of thisEvent I get runtime errors about not being able to make summary into type reference.


tell application "iCal"
	get every event of calendar "School"
	repeat with thisEvent in result
		set newSummary to summary of thisEvent
		set newDate to start date of thisEvent
	end repeat
end tell

Thanks,
Steve

Model: Mac G4
AppleScript: 1.1
Browser: Safari 412.2
Operating System: Mac OS X (10.4)

Hi,

You might be having problems with ‘result’ as your repeat parameter.

tell application “iCal”
set schoolCal to first calendar whose title is “School”
set eventList to every event of schoolCal
set theText to “”
repeat with thisEvent in eventList
set newSummary to summary of thisEvent
set newDate to (start date of thisEvent) as string
set eventText to newSummary & " - " & newDate
set theText to theText & eventText & return
end repeat
end tell
theText

gl,

Thanks. That cleaned up the ical part. I’m still having problem pushing this into a new word doc. Any suggestion on how to do this?
–Steve

Hi Steve,

The script below adds the text to the clipboard. You can then paste the text into a new word document.

tell application "iCal"
	get every event of calendar "School"
	set theText to ""
	repeat with thisEvent in result
		set theText to theText & ((summary of thisEvent) & return & (start date of thisEvent) as text) & return & return
	end repeat
	set the clipboard to theText as text
end tell

Best wishes

John M

That’s not going to work. Also, it’s proper to launch or activate an application before using it. I would use something like this:

tell application "iCal"
	launch
	set theText to ""
	
	repeat with thisEvent in (every event of calendar "School")
		set theText to theText & summary of thisEvent & tab & ((start date of thisEvent) as text) & return
	end repeat
	
	set the clipboard to theText as text
	--quit
end tell

If it matters. I believe ‘return’ is a carriage return (ASCII 13, traditionally used by Mac OS). If you want a line feed (ASCII 10, used by Unix), you could change ‘return’ to ‘ASCII character 10’.

Hi guardian34,

Thanks for the correction in the script. Swapping the second and third line of my script works for me.

Can you explain why it would be proper to launch the application?

tell application "iCal"
	set theText to ""
	get every event of calendar "Home"
	repeat with thisEvent in result
		set theText to theText & ((summary of thisEvent) & return & (start date of thisEvent) as text) & return & return
	end repeat
	set the clipboard to theText as text
end tell

Thanks

John M

Hi Steve,

I was having a problem with the unicode text returned from the summary. It wouldn’t coerce to plain text. So when I tried to set the text of Word document to text containing the summary text, Word wouldn’t take it. Anyway, I used a filter to filter the characters of the summary. Your version of iCal and word may not need to filter the summary.

tell application “iCal”
– my versions of iCal doesn’t allow alendar reference by name
– this works in all versions
set schoolCal to first calendar whose title is “School”
set eventList to every event of schoolCal
set theText to “”
repeat with thisEvent in eventList
set newSummary to (summary of thisEvent)
– my version needs to filter the summary for input into MS Word
set t to “”
repeat with this_char in newSummary
set ascii_num to (ASCII number this_char)
set t to t & (ASCII character ascii_num)
end repeat
– end filter
set newDate to (start date of thisEvent) as string
set eventText to (t & " - " & newDate)
set theText to (theText & eventText & return)
end repeat
end tell
tell application “Microsoft Word”
activate
set text of front document to theText
end tell

Note that it’s better to set a variable to a list first and use that variable as the repeat loop parameter. It’s ususally faster, as you don’t need to get items of the list, but have references. You can do time tests using Jon’s Commands’ ‘the ticks’. Also, using ‘result’ as the repeat loop parameter is not efficient.

Crunching your script saves space for text, but it makes debugging harder. Beginning scripters have great difficulty debuggin these crunched scripts.

gl,

Thanks for all the responses. I settled on:


property theText : ""

tell application "iCal"
	activate
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {", "}
	
	set schoolCal to first calendar whose title is "School"
	set eventList to every event of schoolCal
	set theText to ""
	repeat with thisEvent in eventList
		set newSummary to summary of thisEvent
		set newDate to (start date of thisEvent) as string
		set eventText to newSummary & "	" & word 1 of newDate & ", " & word 2 of newDate ¬
			& "  " & word 3 of newDate & ", " & word 4 of newDate
		set theText to theText & eventText & return
	end repeat
	set the clipboard to theText as text
	set AppleScript's text item delimiters to ASTID
end tell

tell application "Microsoft Word"
	make new document
	activate
	set curdoc to the active document
	paste object selection
	select the text object of curdoc
	convert to table the text object of curdoc separator "	" number of columns 2
end tell

The convert to table is not working exactly as I’d hoped, I was trying to break the line into two columns, one holding the name the second holding the date. This works in word, but I’m doing something wrong with the convert command I guess.
–Steve

I found my error on converting a text object to a two column table. I had the separator command wrong
Instead of
convert to table text object of curdoc separator “\t”
it should have been
convert to table the text object of curdoc separator separate by tabs

–Steve

Hi Steve,

You can get the date without using "word 1 of newDate & “, " & word 2 of newDate” etc.

Try:

set timeNow to (current date)
set dateString to date string of (current date)

Best wishes

John M