Skip to content

Instantly share code, notes, and snippets.

@bchartoff
Last active September 29, 2021 01:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bchartoff/1c9c1919144777b2aebf to your computer and use it in GitHub Desktop.
Save bchartoff/1c9c1919144777b2aebf 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
@bchartoff
Copy link
Author

To install the Script

  • In Outlook, click the script icon in the menu bar (far right)
  • Click "About This Menu..."
  • Click the "Open Folder" button
  • Double click to open one of the scripts in the Apple Script editor
  • Save a copy ("File -> Duplicate" or "File -> Save As") with whatever name you like
  • Delete the old contents, and replace with the above script
  • Save or click "Compile"

To run the Script

  • Select a message with dates/ times in the body
  • Click the script icon in the menu bar
  • Select your script

Caveats

  • This is far from a catch-all solution, it's optimized to make calendar events from certain e-mails in our office. The script uses:
    • The first Month name encountered (expects dates like "January 21, 2015"), as well as whatever one or two digits immediately follow the month
    • The first two times encountered (e.g. "11:00 AM - 12:00 PM")
      • It makes some attempts to intelligently determine AM/ PM (e.g. 12:00 is always noon)
    • The first instance of "2015", "2016", or "2017" or, in their absence, the current year

@bchartoff
Copy link
Author

The MIT License (MIT)

Copyright (c) 2015 Ben Chartoff

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment