(* Script for automatically extracting calendar items from meeting requests and adding them into iCal. *) using terms from application "Mail" on perform mail action with messages theMessages for rule theRule (* path to which to save the tmp file. This may need to be changed, AppleScript's handling of paths leaves much to be desired *) set thePath to POSIX file "/tmp/tmp_invite.ics" tell application "Mail" repeat with theMessage in theMessages set theSource to the source of theMessage (* Find the range of the message source that is an ics message Note: this works both on messages that detect the .ics attachement, and on crappy Exchange invites that show up as an owa url. *) set vcalBegin to the offset of "BEGIN:VCALENDAR" in theSource set vcalEnd to (the offset of "END:VCALENDAR" in theSource) + (the length of "END:VCALENDAR") (* Get the ics data *) set theInvite to the text vcalBegin thru vcalEnd of theSource (* Only deal with requests *) if (the offset of "METHOD:REQUEST" in theInvite) is equal to 0 then (* do nothing *) else (* write to a temp file *) set fh to open for access thePath with write permission write theInvite to fh as string close access fh (* open in iCal *) tell application "iCal" to open the thePath (* delete the file *) tell application "Finder" to delete the thePath end if end repeat end tell end perform mail action with messages end using terms from