Last active
October 20, 2021 21:38
-
-
Save logic2design/74df149adae7ec50b41ecee904b54c0d to your computer and use it in GitHub Desktop.
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
################################################################################# | |
# Title: Create Calendar Event from eMail | |
################################################################################# | |
# Iain Dunn | |
# Logic2design.com | |
# logic2design@icloud.com | |
## v3 - added AllDay event | |
################################################################################# | |
# Configuration | |
################################################################################# | |
#Set Current Date & Time | |
set todayDate to current date | |
set eventDate to short date string of todayDate as text | |
set eventTime to (do shell script "date +\"%l:%M %p\" | awk '{$1=$1;print}'") | |
#Event Time | |
display dialog "When is the Event?" default answer eventDate & " " & eventTime | |
set theStartDate to date (text returned of result) | |
display dialog "How long is the Event? (minutes) | |
Set to 0 for an all day event" default answer 30 | |
set appt_length to text returned of result | |
if appt_length < 1 then | |
set appt_mins to (0) | |
set theEndDate to theStartDate + (appt_mins * minutes) | |
set allDay to true | |
else | |
set appt_mins to (appt_length) | |
set theEndDate to theStartDate + (appt_mins * minutes) | |
set allDay to false | |
end if | |
################################################################################# | |
# Script | |
################################################################################# | |
#get eMail | |
tell application "Mail" | |
set theSelection to selection as list | |
# do nothing if no email is selected in Mail | |
try | |
set theMessage to item 1 of theSelection | |
on error | |
return | |
end try | |
set theSummary to theMessage's subject | |
set theDescription to theMessage's content | |
set theOrigMessageId to theMessage's message id | |
set theUrl to {"message:%3C" & my replaceText(theOrigMessageId, "%", "%25") & "%3E"} | |
end tell | |
#select Calendar | |
tell application "Calendar" | |
set clist to name of calendars | |
set cname to choose from list clist with prompt "Select Calendar" | |
set cal to cname as text | |
end tell | |
#Create Event | |
tell application "Calendar" | |
tell calendar cal | |
make new event with properties {summary:theSummary, description:theDescription, url:theUrl, start date:theStartDate, end date:theEndDate, allday event:allDay} | |
end tell | |
end tell | |
tell application "Calendar" to activate | |
################################################################################# | |
# Functions | |
################################################################################# | |
# string replace function | |
# used to replace % with %25 | |
on replaceText(subject, find, replace) | |
set prevTIDs to text item delimiters of AppleScript | |
set text item delimiters of AppleScript to find | |
set subject to text items of subject | |
set text item delimiters of AppleScript to replace | |
set subject to "" & subject | |
set text item delimiters of AppleScript to prevTIDs | |
return subject | |
end replaceText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where do I have to put the code in order to work?