Skip to content

Instantly share code, notes, and snippets.

@varontron
Last active January 19, 2023 23:20
Show Gist options
  • Save varontron/ebe65370e0b7e10a0d8ad3f719f5cadb to your computer and use it in GitHub Desktop.
Save varontron/ebe65370e0b7e10a0d8ad3f719f5cadb to your computer and use it in GitHub Desktop.
Applescript to copy and format selected outlook event data to "MergeCSV" in Notion
property MY_NOTION_TOKEN : "redacted"
property MTG_DATABASE_ID : "redacted"
tell application "Microsoft Outlook"
-- is an evet selected in the calendar window?
if class of front window is main window and view of front window is calendar view then
set theEvt to the selection
-- is the event unsaved or is the buggy "New Outlook" interface being used
else if class of front window is window and object of front window is missing value then
display dialog "Unsaved events cannot be copied. Save your event and run this script again." with title "Alert!" with icon 2 buttons {"OK"} default button {"OK"}
-- is the event the top window?
else if class of front window is window and (class of object of front window) is calendar event then
set theEvt to object of front window
else
display dialog "Select an event from your calendar first or open its window." with title "Alert!" with icon 2 buttons {"OK"} default button {"OK"}
end if
-- save delims
set tid to AppleScript's text item delimiters
-- set delims to linefeed types
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
-- get the subject
set subj to text items of (subject of theEvt as text)
-- get the start time
set startdate to text items of (start time of theEvt as text)
set start to my parseTime(startdate, false)
-- get the rest of the data
set body to (plain text content of theEvt as text)
set link to text items of (my getLink(body) as text)
set AppleScript's text item delimiters to tid
my curl(subj, start, link, body)
end tell
tell application "Notion"
activate
end tell
on curl(subj, start, link, body)
set silent to "-s "
set post to "-X POST "
set dest to "https://api.notion.com/v1/pages "
set auth to "-H \"Authorization: Bearer " & MY_NOTION_TOKEN & "\" "
set type to "-H \"Content-Type: application/json\" "
set vsn to "-H \"Notion-Version: 2021-05-13\" "
set _body to my convertBody(body)
set json to "--data '{\"parent\": { \"database_id\":\"" & MTG_DATABASE_ID & "\"}, \"properties\": {\"Name\": { \"title\": [{\"text\": {\"content\":\"" & subj & "\"}}]},\"Start Timestamp\": {\"number\":" & start & "},\"Zoom Link\":{\"url\":\"" & link & "\"},\"Purpose\":{\"rich_text\":[{\"type\":\"text\",\"text\":{\"content\":\"" & _body & "\"}}]}}}'"
log json
set args to silent & post & dest & auth & type & vsn & json
set cmd to "curl " & args
do shell script cmd
end curl
on convertBody(body)
set x to do shell script "python -c 'import sys;import re;print(re.sub(\"[\\r\\n]\",\"\\\\\\\\n\",\"\"\"" & body & "\"\"\",flags=re.DOTALL)[0:2000])'"
return x
end convertBody
-- parse the time and convert to 24
on parseTime(startdate, twelve)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set theMonth to word 2 of (startdate as text)
set theDate to word 3 of (startdate as text)
set theYear to word 4 of (startdate as text)
set theHour to word 6 of (startdate as text)
set theMin to word 7 of (startdate as text)
set theAmPm to word 9 of (startdate as text)
set theTime to ""
if (twelve) then
set theTime to (theHour & ":" & theMin & " " & theAmPm)
else
if (theAmPm is "PM" and (theHour as number) is not 12) then
set theHour to ((theHour as number) + 12)
else if (theAmPm is "AM" and theHour is 12) then
set theHour to "00"
end if
set theTime to ((theHour as text) & ":" & (theMin as text) & ":00")
end if
set strp to (theMonth & " " & theDate & ", " & theYear & " " & theTime)
set start to do shell script "printf \"" & strp & "\" | python -c 'import sys,time,datetime; print int(time.mktime(datetime.datetime.strptime(sys.stdin.read(), \"%B %d, %Y %H:%M:%S\").timetuple()))'"
set AppleScript's text item delimiters to tid
return start & "000"
end parseTime
on getLink(body)
set rx to "https?:\\/\\/(?:(?:www|fogpharma)\\.)?(?:zoom|(?:goto)?meet(?:ing)?|ringcentral)[^\\s]*(?:\\.(?:com|me|io|us))\\/[^\\s\\r\\n]+"
set match to do shell script "echo \"" & body & "\" | python -c 'import sys,re;print(re.findall(\"" & rx & "\", sys.stdin.read() ,re.MULTILINE)[0])'"
return match
end getLink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment