Extracting Exchange vCalendars from Mail.app

Jan 28, '03 10:16:51AM

Contributed by: lx

When I receive a vCalendar request from someone through Microsoft Exchange, Mail.app doesn't recognize the included vCalendar as an attachment and so it renders the content of the vCalendar instead of giving me an icon to click. I was able to remedy this shortcoming in Mail by setting up a new Mail rule which runs the AppleScript given below. For the Mail rule, you will need to "Edit Header List..." in order to add the header "Content-Class" which should be set to contains "calendarmessage" and then perfrorm action Run AppleScript with the script given below saved with the Script Editor as a compiled script.

This AppleScript will extract the vCalendar from the message and prompt you to save it to your disk. You should give the file the extension .ics. The first time you do this, the resulting file will likely appear as a text file. To tell the Finder that it is an iCal file, "Get Info" on the file and set "Open with" to "iCal". If you also press "Change All...", then in the future these files will belong to iCal by default.

This AppleScript will extract the vCalendar from the message and prompt you to save it to your disk. You should give the file the extension .ics. The first time you do this, the resulting file will likely appear as a text file. To tell the Finder that it is an iCal file, "Get Info" on the file and set "Open with" to "iCal". If you also press "Change All...", then in the future these files will belong to iCal by default.

property beginTag : "BEGIN:VCALENDAR"
property endTag : "END:VCALENDAR"

on perform_mail_action(info)
tell application "Mail"
set theMessages to |SelectedMessages| of info
repeat with thisMessage in theMessages
set thisContent to content of thisMessage

if (thisContent contains beginTag) ¬
and (thisContent contains endTag) then

-- extract vCalendar from message
set thisCal to ¬
text from character (offset of beginTag in thisContent) ¬
to character ((offset of endTag in thisContent) ¬
+ (length of endTag)) of thisContent

-- save vCalendar to file
set thisFileName to ¬
choose file name with prompt "Save calendar item from " ¬
& sender of thisMessage ¬
& " as:" default name subject of thisMessage & ".ics"
set thisFile to open for access thisFileName with write permission
write thisCal as string to thisFile
close access thisFile
end if
end repeat
end tell
end perform_mail_action
If you set this up on your computer, you will probably want to keep an eye on the Apple Mail updates as I would expect them to fix this eventually so you don't need this remedy. Also note that this script will only find one vCalendar per message, which is all I have ever received.

Comments (17)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030128071651999