Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Parse booking confirmation emails from Tock and add Calendar event
- -- Author: Matt Swain <m.swain@me.com>, Version 1.0, License: MIT
- -- Triggered by AirMail rule.
- using terms from application "Airmail 2"
- on processMessage(theMessage)
- try
- tell application "Airmail 2"
- set {movie, runtime, cert, bref, starttime, addr, screen} to my parseMsg(theMessage)
- my createEvent(movie, runtime, cert, bref, starttime, addr, screen)
- end tell
- on error e
- display dialog (e as string)
- end try
- end processMessage
- end using terms from
- -- Parse the email content to extract movie details.
- on parseMsg(msgcontent)
- set name to extractBetween(msgcontent, "Name: ", "Confirmation: ")
- set guest to extractBetween(msgcontent, "Grassini Family Vineyards", " on")
- set addr to extractBetween(msgcontent, "Address ", "8058838118")
- set cref to extractBetween(msgcontent, "Confirmation #: ", "Date: ")
- set datestring to extractBetween(msgcontent, "on ", "Name: ")
- set starttime to parseDateString(datestring)
- return {name, guest, cref, starttime, addr}
- end parseMsg
- -- Extract the substring from between two strings
- to extractBetween(theString, startText, endText)
- set tid to AppleScript's text item delimiters
- set AppleScript's text item delimiters to startText
- set startComps to text items of theString
- set AppleScript's text item delimiters to endText
- set endComps to text items of second item of startComps
- set AppleScript's text item delimiters to tid
- return trim(first item of endComps)
- end extractBetween
- -- Trim all whitespace from start and end of a string
- on trim(theString)
- set theChars to {" ", tab, character id 10, return, character id 0, character id 8232}
- repeat until first character of theString is not in theChars
- set theString to text 2 thru -1 of theString
- end repeat
- repeat until last character of theString is not in theChars
- set theString to text 1 thru -2 of theString
- end repeat
- return theString
- end trim
- -- Parse date and time from the string given in the email.
- on parseDateString(datestring)
- set theDate to current date
- set dateWords to words of datestring
- set day of theDate to text 1 thru -3 of item 2 of dateWords
- set time of theDate to (item 5 of dateWords) * hours + (item 6 of dateWords) * minutes
- set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
- repeat with i from 1 to 12
- if item 3 of dateWords = ((item i of monthList) as string) then
- set monthNumber to (text -2 thru -1 of ("0" & i))
- exit repeat
- end if
- end repeat
- set month of theDate to monthNumber
- return theDate
- end parseDateString
- -- Create a calendar event for the specified tour.
- on createEvent(name, mins, guest, cref, starttime, addr, msgid)
- set endtime to starttime + 60
- tell application "Calendar" to tell calendar "Winery"
- set theEvent to make new event with properties {start date:starttime, end date:endtime, summary:"Tour - " & name & " - " & guest}
- set location of theEvent to addr
- set description of theEvent to "Confirmation #: " & cref & return & "Run Time: " & mins & " minutes"
- set url of theEvent to "message://" & "%3c" & msgid & "%3e"
- end tell
- end createEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement