# # Title: Create reminder from selected Mail # By Michael Kummer and partly based on various code snippets I found online # michaelkummer.com # You can use the source as you wish but do so at your own risk # Have a backup of your data to avoid accidental loss # Last update: April 29th, 2021 # Version: 0.2 # Tested on macOS 11.4 Big Sur # # Description # When installed as a Service, this AppleScript script can be used to automatically create a reminder (in pre-defined Reminder lists) # from a selected email and flag the email. # If you run the script against an email that matches an existing reminder it can mark the reminder as completed and clear the flag in Mail # # Contributors and sources # Rick0713 at https://discussions.apple.com/thread/3435695?start=30&tstart=0 # http://www.macosxautomation.com/applescript/sbrt/sbrt-06.html # Set this according to your email account names and Reminder's lists set WorkAccountName to "Exchange" set WorkRemindersList to "Work" set PersonalAccountName to "iCloud" set PersonalRemindersList to "Personal" # On my machine 5 is the Purple flag, which is the color I would like to use for mails flagged as Reminder set FlagIndex to 5 tell application "Mail" set theSelection to selection # do nothing if no email is selected in Mail try set theMessage to item 1 of theSelection on error return end try set theSubject to theMessage's subject # Make sure reminder doesn't already exist so we don't create duplicates tell application "Reminders" set theNeedlesName to name of reminders whose name is theSubject and completed is false if theNeedlesName is not {} then # make sure dialog is in focus when called as a service # without the below, Mail would be the active application tell me activate end tell set theButton to button returned of (display dialog "The selected email matches an existing reminder: " & theNeedlesName & ". Would you like to mark the reminder as complete and clear any remaining flags of this message?" with title "Create Reminder from E-Mail" buttons {"Mark complete", "Cancel"} default button 1) if theButton is "Mark complete" then tell application "Mail" # unflag email/message set flag index of theMessage to -1 end tell # find correct reminder based on subject and mark as complete set theNeedle to last reminder whose name is theSubject and completed is false set completed of theNeedle to true return else if the_button is "Cancel" then return end if end if end tell # present user with a list of follow-up times (in minutes) (choose from list {"Tomorrow", "2 Days", "End of Week", "1 Week", "1 Month", "3 Months"} default items "End of Week" OK button name "Create" with prompt "Set follow-up time" with title "Create Reminder from E-Mail") set reminderDate to result as rich text # Exit if user clicks Cancel if reminderDate is "false" then return if reminderDate = "Tomorrow" then # add 1 day and set time to 9h into the day = 9am set remindMeDate to (current date) + 1 * days set time of remindMeDate to 60 * 60 * 9 else if reminderDate = "2 Days" then set remindMeDate to (current date) + 2880 * minutes else if reminderDate = "End of Week" then # end of week means Thursday in terms of reminders # get the current day of the week set curWeekDay to weekday of (current date) as string if curWeekDay = "Monday" then set remindMeDate to (current date) + 3 * days else if curWeekDay = "Tuesday" then set remindMeDate to (current date) + 2 * days else if curWeekDay = "Wednesday" then set remindMeDate to (current date) + 1 * days # if it's Thursday, I'll set the reminder for Friday else if curWeekDay = "Thursday" then set remindMeDate to (current date) + 1 * days # if it's Friday I'll set the reminder for Thursday next week else if curWeekDay = "Friday" then set remindMeDate to (current date) + 6 * days end if set time of remindMeDate to 60 * 60 * 9 else if reminderDate = "1 Week" then set remindMeDate to (current date) + 10080 * minutes else if reminderDate = "1 Month" then set remindMeDate to (current date) + 43200 * minutes else if reminderDate = "3 Months" then set remindMeDate to (current date) + 129600 * minutes end if # Flag selected email/message in Mail set flag index of theMessage to FlagIndex # Get the unique identifier (ID) of selected email/message set theOrigMessageId to theMessage's message id #we need to encode % with %25 because otherwise the URL will be screwed up in Reminders and you won't be able to just click on it to open the linked message in Mail set theUrl to {"message://%3C" & my replaceText(theOrigMessageId, "%", "%25") & "%3E"} # determine correct Reminder's list based on account the email/message is in if name of account of mailbox of theMessage is WorkAccountName then set RemindersList to WorkRemindersList else if name of account of mailbox of theMessage is PersonalAccountName then set RemindersList to PersonalRemindersList else #default list name in Reminders set RemindersList to "Reminders" end if end tell tell application "Reminders" tell list RemindersList # create new reminder with proper due date, subject name and the URL linking to the email in Mail make new reminder with properties {name:theSubject, remind me date:remindMeDate, body:theUrl} end tell end tell # 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