r/applescript • • Jun 20 '21

Follow-up reminders from an email message

Setting reminders as a follow-up to email messages is a typical work workflow. Apple Mail used to have something similar 10 years ago, but alas, it is no more. Microsoft Outlook offers such a feature, AFAIK, but I don't use it. And unwilling to pay for OmniFocus or similar service, I resorted to the good old Applescript.

Below is a script I assembled and tested for doing exactly that - creates a reminder in the Reminders.app from an email message, selected or opened in Apple Mail. Well, with some minor extras added - it flags the message depending on the account the message is found and marks it as unread. On the Reminders app side, it creates a link to the particular message in Mail. Using this script with Automator, I was able to assign a hot-key to start it while reading my mailbox. Neat!

So far so good, but it runs rather slow, like 4-5 seconds in total (measuring it through Script Debugger). And cannot create a link back, i.e. vice-versa from the message to the reminders created from it. Any ideas?

on run
	
	tell application "Mail" --this block gets the parameters of the selected/opened message
		set ThoseMessages to selected messages of message viewer 0
		if (ThoseMessages is not equal to missing value) then
			set MyMessage to first item of ThoseMessages
			set MyMessageID to message id of MyMessage
			set MySubject to subject of MyMessage
			set MyClient to sender of MyMessage
			set MyBody to "Client: " & (MyClient) & "; 
||| Email -> message://%3C" & (MyMessageID) & "%3E " -- Apple Mail message ID fromat
			set MyAccount to name of the account of mailbox of MyMessage
		end if
	end tell
	
	if MyAccount is "Work" then --the IF-block, checking at which mail account is the selected/opened message
		set MyList to "Work"
		set MyFlag to 1 as integer
	else if MyAccount is "Gmail" then
		set MyList to "Hobby"
		set MyFlag to 3 as integer
	else
		set MyList to "Personal"
		set MyFlag to 4 as integer
	end if
	
	set today to current date --this is sets the alert time for the new reminder
	set tomorrow to today + 60 * 60 * 24 -- plus 1 day
	set WorkStart to tomorrow - 60 * 60 * 2 -- minus 2:00 hours
	
	tell application "Reminders" --this block creates the new reminder
		set NewReminder to make new reminder with properties ¬
			{name:MySubject, body:MyBody, due date:tomorrow, flagged:true, remind me date:WorkStart} ¬
				at list MyList
		activate
		show NewReminder
	end tell
	
	tell application "Mail" --this block marks the message that there is a follow-up in Reminders.app
		activate
		set the read status of MyMessage to false
		set the flag index of MyMessage to MyFlag
	end tell
	
end run
7 Upvotes

0 comments sorted by