Advertisement
Guest User

Untitled

a guest
May 26th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Parse booking confirmation emails from Tock and add Calendar event
  2. -- Author: Matt Swain <m.swain@me.com>, Version 1.0, License: MIT
  3.  
  4. -- Triggered by AirMail rule.
  5. using terms from application "Airmail 2"
  6.     on processMessage(theMessage)
  7.         try
  8.             tell application "Airmail 2"
  9.                 set {movie, runtime, cert, bref, starttime, addr, screen} to my parseMsg(theMessage)
  10.                 my createEvent(movie, runtime, cert, bref, starttime, addr, screen)
  11.             end tell
  12.         on error e
  13.             display dialog (e as string)
  14.         end try
  15.        
  16.     end processMessage
  17. end using terms from
  18.  
  19. -- Parse the email content to extract movie details.
  20. on parseMsg(msgcontent)
  21.     set name to extractBetween(msgcontent, "Name: ", "Confirmation: ")
  22.     set guest to extractBetween(msgcontent, "Grassini Family Vineyards", " on")
  23.     set addr to extractBetween(msgcontent, "Address ", "8058838118")
  24.     set cref to extractBetween(msgcontent, "Confirmation #: ", "Date: ")
  25.     set datestring to extractBetween(msgcontent, "on ", "Name: ")
  26.     set starttime to parseDateString(datestring)
  27.     return {name, guest, cref, starttime, addr}
  28. end parseMsg
  29.  
  30. -- Extract the substring from between two strings
  31. to extractBetween(theString, startText, endText)
  32.     set tid to AppleScript's text item delimiters
  33.     set AppleScript's text item delimiters to startText
  34.     set startComps to text items of theString
  35.     set AppleScript's text item delimiters to endText
  36.     set endComps to text items of second item of startComps
  37.     set AppleScript's text item delimiters to tid
  38.     return trim(first item of endComps)
  39. end extractBetween
  40.  
  41. -- Trim all whitespace from start and end of a string
  42. on trim(theString)
  43.     set theChars to {" ", tab, character id 10, return, character id 0, character id 8232}
  44.     repeat until first character of theString is not in theChars
  45.         set theString to text 2 thru -1 of theString
  46.     end repeat
  47.     repeat until last character of theString is not in theChars
  48.         set theString to text 1 thru -2 of theString
  49.     end repeat
  50.     return theString
  51. end trim
  52.  
  53. -- Parse date and time from the string given in the email.
  54. on parseDateString(datestring)
  55.     set theDate to current date
  56.     set dateWords to words of datestring
  57.     set day of theDate to text 1 thru -3 of item 2 of dateWords
  58.     set time of theDate to (item 5 of dateWords) * hours + (item 6 of dateWords) * minutes
  59.     set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
  60.     repeat with i from 1 to 12
  61.         if item 3 of dateWords = ((item i of monthList) as string) then
  62.             set monthNumber to (text -2 thru -1 of ("0" & i))
  63.             exit repeat
  64.         end if
  65.     end repeat
  66.     set month of theDate to monthNumber
  67.     return theDate
  68. end parseDateString
  69.  
  70. -- Create a calendar event for the specified tour.
  71. on createEvent(name, mins, guest, cref, starttime, addr, msgid)
  72.     set endtime to starttime + 60
  73.     tell application "Calendar" to tell calendar "Winery"
  74.         set theEvent to make new event with properties {start date:starttime, end date:endtime, summary:"Tour - " & name & " - " & guest}
  75.         set location of theEvent to addr
  76.         set description of theEvent to "Confirmation #: " & cref & return & "Run Time: " & mins & " minutes"
  77.         set url of theEvent to "message://" & "%3c" & msgid & "%3e"
  78.     end tell
  79. end createEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement