Skip to content

Instantly share code, notes, and snippets.

@ankushnarula
Forked from bchartoff/email to calendar
Created September 29, 2021 01:44
Show Gist options
  • Save ankushnarula/0e0708ddcfd9e8b18b45d15003deb830 to your computer and use it in GitHub Desktop.
Save ankushnarula/0e0708ddcfd9e8b18b45d15003deb830 to your computer and use it in GitHub Desktop.
An AppleScript to automatically create a calendar event from an e-mail containing a date and time of the form "March 10, 2015 12:00 - 2:00 PM". Distributed under MIT license (see below)
(*
Create Calendar event from Message
*)
set theContent to string
-- A subroutine to strip all tags from html
on remove_markup(this_text)
set copy_flag to true
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 "<" then
set the copy_flag to false
else if this_char is ">" 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
tell application "Microsoft Outlook"
-- get the currently selected message or messages
set selectedMessages to current messages
-- if there are no messages selected, warn the user and then quit
if selectedMessages is {} then
display dialog "Please select a message first and then run this script." with icon 1
return
end if
repeat with theMessage in selectedMessages
-- get the information from the message, and store it in variables
set theName to subject of theMessage
set theCategory to category of theMessage
set theContent to content of theMessage
-- Strip out all the newlines and returns, so the text can be piped into echo
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232, character id 40, character id 41}
set newText to text items of theContent
set AppleScript's text item delimiters to {" "}
set newText to newText as text
-- Get rid of all the html tags
set newText to my remove_markup(newText)
"echo " & quoted form of newText & " | grep -o .*"
set theMonth to do shell script "printf " & quoted form of newText & " | grep -o \"January\\|February\\|March\\|April\\|May\\|June\\|July\\|August\\|September\\|October\\|Novermber\\|December\" | head -n1"
set theDay to do shell script "echo " & quoted form of newText & " | grep -o \"" & theMonth & " [0-9]\\{1,2\\}\" | grep -o \"[0-9]\\{1,2\\}\""
set theYear to do shell script "echo " & quoted form of newText & " | grep -o \"2015\\|2016\\|2017\" | head -n1"
--If no year is specified, assume it's for the current year
if (length of theYear) = 0 then
set theYear to year of (current date)
end if
set startTime to do shell script "printf " & quoted form of newText & " | grep -o \"[0-9]\\{1,2\\}:[0-9]\\{1,2\\}\" | head -n1"
set endTime to do shell script "printf " & quoted form of newText & " | grep -o \"[0-9]\\{1,2\\}:[0-9]\\{1,2\\}\" | tail -n1"
set ampm to do shell script "echo " & quoted form of newText & " | grep -o \"AM\\|PM\\|pm\" | head -n1"
-- create a new event with the information from the task
set startDate to date (theMonth & " " & theDay & ", " & theYear & " " & startTime & " " & ampm) as date
set endDate to date (theMonth & " " & theDay & ", " & theYear & " " & endTime & " " & ampm) as date
set newEvent to make new calendar event with properties {subject:theName, category:theCategory, content:theContent, start time:startDate, end time:endDate}
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment