Created
April 6, 2014 11:40
-
-
Save mcs07/10004830 to your computer and use it in GitHub Desktop.
Parse booking confirmation emails from Cinema and add Calendar event.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Parse booking confirmation emails from Cinema and add Calendar event | |
-- Author: Matt Swain <m.swain@me.com>, Version 1.0, License: MIT | |
-- Triggered by Mail rule. | |
using terms from application "Mail" | |
on perform mail action with messages msgs for rule theRule | |
tell application "Mail" | |
repeat with msg in msgs | |
try | |
set msgcontent to content of msg | |
set msgid to message id of msg | |
set {movie, runtime, cert, bref, starttime, addr, screen} to my parseMsg(msgcontent) | |
my createEvent(movie, runtime, cert, bref, starttime, addr, screen, msgid) | |
end try | |
end repeat | |
end tell | |
end perform mail action with messages | |
end using terms from | |
-- Parse the email content to extract movie details. | |
on parseMsg(msgcontent) | |
set movie to extractBetween(msgcontent, "You are going to see: ", "Cert: ") | |
set cert to extractBetween(msgcontent, "Cert: ", "Running Time: ") | |
set runtime to extractBetween(msgcontent, "Running Time: ", " minutes") | |
set bref to extractBetween(msgcontent, "Booking Reference: ", "Date: ") | |
set datestring to extractBetween(msgcontent, "Date: ", "Cinema: ") | |
set addr to extractBetween(msgcontent, "Cinema: ", "Screen: ") | |
set screen to extractBetween(msgcontent, "Screen: ", "Number of people going: ") | |
set starttime to parseDateString(datestring) | |
return {movie, runtime, cert, bref, starttime, addr, screen} | |
end parseMsg | |
-- Create a calendar event for the specified movie. | |
on createEvent(movie, mins, cert, bref, starttime, addr, screen, msgid) | |
set endtime to starttime + mins * minutes | |
tell application "Calendar" to tell calendar "Home" | |
set theEvent to make new event with properties {start date:starttime, end date:endtime, summary:"Cinema: " & movie} | |
set location of theEvent to screen & ", Cineword " & addr | |
set description of theEvent to "Booking Reference: " & bref & return & "Run Time: " & mins & " minutes" & return & "Certificate: " & cert | |
set url of theEvent to "message://" & "%3c" & msgid & "%3e" | |
end tell | |
end createEvent | |
-- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment