-- This is the name of the calendar you want your meetings in. property calendar_name : "Work" -- Set your email address here: property myEmailAdress : "joeuser@somedomain.com" -- Apple Glyph property AppleGlyph : (ASCII character 240) set AppleScript's text item delimiters to "" tell application "Mail" -------------------------------------------- -- Get data from selected mail -------------------------------------------- set theSelectedMessage to selection if (count of theSelectedMessage) is equal to 0 then display dialog "Please select a message in Mail first, then run this script again." else if (count of theSelectedMessage) is equal to 1 then set thisMessage to item 1 of theSelectedMessage set theSubject to subject of thisMessage set nameWithEmailAddress to sender of thisMessage tell me set {personWhoSentInvite, EmailOfPersonWhoSentInvite} to extract_person_and_emailaddress(nameWithEmailAddress) end tell set theSource to source of thisMessage tell me set {meeting_start_date, meeting_end_date, theLocation, ddInvitation} to extractInvitation(theSource) end tell else display dialog "Please select only one message in Mail first, then run this script again." buttons {"Cancel"} default button 1 with icon note end if end tell tell application "iCal" -------------------------------------------- -- Look at calendar and decide whether to accept or declien invitation -------------------------------------------- activate switch view to day view view calendar at meeting_start_date display dialog personWhoSentInvite & ddInvitation buttons {"Cancel", "Decline", "Accept"} default button 3 if the button returned of the result is "Accept" then tell me set accept_decline to "Accept" add_meeting_to_iCal(meeting_start_date, meeting_end_date, personWhoSentInvite, theLocation, theSubject) reply_to_invitation(personWhoSentInvite, EmailOfPersonWhoSentInvite, accept_decline, theSubject, meeting_start_date) end tell else if the button returned of the result is "Decline" then tell me set accept_decline to "Decline" reply_to_invitation(personWhoSentInvite, EmailOfPersonWhoSentInvite, accept_decline, theSubject, meeting_start_date) end tell end if end tell on add_meeting_to_iCal(meeting_start_date, meeting_end_date, personWhoSentInvite, theLocation, theSubject) -------------------------------------------- -- Accepted invitation is added to calendar -------------------------------------------- -- sample date: "Friday, October 8, 2004 12:00:00 AM" tell application "iCal" activate set this_calendar to (the first calendar whose title is the calendar_name) tell this_calendar set this_event to make new event at end of events with properties {start date:meeting_start_date, end date:meeting_end_date, summary:theSubject, allday event:false, url:"", status:confirmed} tell this_event make new display alarm at end of display alarms with properties {trigger interval:-15} make new display alarm at end of display alarms with properties {trigger interval:-15} make new display alarm at end of display alarms with properties {trigger interval:-90} make new display alarm at end of display alarms with properties {trigger interval:-90} end tell end tell end tell end add_meeting_to_iCal on get_timeLocationStrings(bodyContent) set {time_string, location_string} to {"not_found", "Location tba"} repeat with i from 1 to number of items in bodyContent set this_item to item i of bodyContent if this_item starts with "When: " then set time_string to this_item end if if this_item starts with "Where: " then set location_string to this_item end if end repeat if time_string is equal to "not_found" then display dialog "Script has failed to find an a valid invitation in the selected message" buttons {"Cancel"} default button 1 with icon caution else return {time_string, location_string} end if end get_timeLocationStrings on reply_to_invitation(personWhoSentInvite, EmailOfPersonWhoSentInvite, accept_decline, theSubject, meeting_start_date) -------------------------------------------- -- Invitation is RSVPed in email -------------------------------------------- set body_of_message to accept_decline & " meeting on " & meeting_start_date tell application "Mail" -------------------------------------------- -- To respond without viewing your message (not necessarily recommended): --1. comment out 'activate', change --2. Change visible to false --3. Uncomment 'send newMessage' -------------------------------------------- activate set newMessage to make new outgoing message with properties  {name:personWhoSentInvite, address:EmailOfPersonWhoSentInvite, subject:"Re: " & theSubject, content:body_of_message & return} tell newMessage -- Default is false. Determines whether the compose window will -- show on the screen or whether it will happen in the background. set visible to true set sender to myEmailAdress make new to recipient at end of to recipients with properties {name:personWhoSentInvite, address:EmailOfPersonWhoSentInvite} end tell --send newMessage end tell end reply_to_invitation on extract_person_and_emailaddress(nameWithEmailAddress) -------------------------------------------- -- This could be considered a klug to separate the "Friendly name" and the actual email address. e.g. "John Doe" -------------------------------------------- if nameWithEmailAddress contains "<" or nameWithEmailAddress contains ">" then tell me set nameWithEmailAddress to replace_chars(nameWithEmailAddress, ">", "") set theDelimiter to "<" set {personWhoSentInvite, EmailOfPersonWhoSentInvite} to delimit_string(nameWithEmailAddress, theDelimiter) as list end tell else set {personWhoSentInvite, EmailOfPersonWhoSentInvite} to {nameWithEmailAddress, nameWithEmailAddress} end if return {personWhoSentInvite, EmailOfPersonWhoSentInvite} end extract_person_and_emailaddress on extractInvitation(theSource) -------------------------------------------- -- Massage and parse message source. -- This is where this script is most brittle. May need to be tweaked to reflect email formats -------------------------------------------- (* -- sample invite string from Microsoft Office (with html and artifacts cleaned out) When: Tuesday, October 12, 2004 2:00 PM-3:00 PM (GMT-08:00) Pacific Time (US & Canada); Tijuana. Where: In the main conference room *~*~*~*~*~*~*~*~*~* *) -- Start with a mess and massage string until each element can be delimited by a space set the theSource to replace_chars(theSource, "" then set the copy_flag to true else if the copy_flag is true then set the clean_text to the clean_text & this_char as string end if end repeat return the clean_text end remove_markup on replace_chars(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars on rm_dup_chrs(this_text, theChr) set seenThis to false set the clean_text to "" repeat with this_char in this_text set this_char to the contents of this_char if this_char is theChr then if seenThis is false then set the clean_text to the clean_text & this_char as string set seenThis to true end if else if this_char is not theChr then set the clean_text to the clean_text & this_char as string set seenThis to false end if end repeat return the clean_text end rm_dup_chrs