(* Script for automatically extracting calendar items from meeting requests and adding them into iCal. modified to run from AppleScript menu http://www.macosxhints.com/article.php?story=20060821073102694 Abstracted the grep and sed to work better with other Exchange invites. *) --change this value to what your iCal time zone expects. --This is *not* your local timezone. It is the timezone of the Exchange server in proper iCal format. property ical_tzid : "US/Central" using terms from application "Mail" on perform mail action with messages theMessages for rule theRule set thepath to ((path to startup disk) as string) & "tmp:temp_invite.ics" tell application "Mail" to set theMessages to selected messages of message viewer 1 my make_iCal_Event(thepath, theMessages) end perform mail action with messages end using terms from on run {} set thepath to ((path to startup disk) as string) & "tmp:temp_invite.ics" tell application "Mail" to set theMessages to selected messages of message viewer 1 my make_iCal_Event(thepath, theMessages) end run on make_iCal_Event(thepath, theMessages) repeat with theMessage in theMessages tell application "Mail" to 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 my fix_TZID(thepath) (* open in iCal *) tell application "iCal" to open alias thepath (* delete the file *) tell application "Finder" to delete alias thepath end if end repeat tell application "Mail" to set theSource to "" tell application "Mail" to set theInvite to "" end make_iCal_Event on fix_TZID(thepath) --fix for Outlook TZID weirdness set tPath to POSIX path of thepath --this should automatically grab the TZID that Exchange gives set exchange_tzid to do shell script "grep -o ^TZID.*$ " & tPath & " | sed -e 's|TZID:\\(.*\\)|\\1|g'" --set local_tzid to do shell script "ls -l //etc//localtime | grep -o zoneinfo.*$ | sed -e 's_zoneinfo/\\(.*\\)_\\1_g'" --display dialog exchange_tzid & return & ical_tzid & local_tzid set quoted_exchange_tzid to "\"" & exchange_tzid & "\"" --do shell script "sed 's_\"\\(.*\\) Time (.*\"_US/\\1_g' " & tPath & " > " & tPath & "2" do shell script "sed 's|" & quoted_exchange_tzid & "|" & ical_tzid & "|g' " & tPath & " > " & tPath & "2" do shell script "mv " & tPath & "2 " & tPath --do shell script "rm " & tPath end fix_TZID