Build an Automator Quick Action To Snooze Email

Want to clear your inbox but need to set reminders to get back to email messages later on? You can use this Automator Quick Action to set a reminder with a link to the message and then archive the message.

function run(input, parameters) {
	
	// set up applications
	var Mail = Application("Mail");
	Mail.includeStandardAdditions = true;
	var Reminders = Application("Reminders");
	var SystemEvents = Application('System Events');
	
	// get the selected messages
	var messages = Mail.selection();
	
	// loop through all messages
	for(var i=0;i<messages.length;i++) {
	
		// get the subject, sender and message id
		var subject = messages[i].subject();
		var sender = messages[i].sender();
		var id = messages[i].messageId();
		
		// prompt for subject modifications and the snooze time
		var dialogResult = Mail.displayDialog('Email Snooze Reminder Title',
			{ defaultAnswer: subject + ' from ' + sender,
			buttons: ['4 PM Today', '4 Hours From Now', 'Cancel'],
			defaultButton: '4 PM Today',
			cancelButton: 'Cancel',
			withTitle: 'Mail Snooze' });

		// calculate the due date time
		var due = new Date();
		if (dialogResult.buttonReturned == '4 PM Today') {
			due.setHours(16);
			due.setMinutes(0);
			due.setSeconds(0);
		} else if (dialogResult.buttonReturned == '4 Hours From Now') {
			due.setHours(due.getHours() + 4);
		} else {
			return; // cancelled, so quit
		}
		
		// get the new title
		reminderTitle = dialogResult.textReturned;
		
		// create a new reminder and add it
		var newReminder = Reminders.Reminder({ name: reminderTitle, body: "message://<"+id+">", dueDate: due});

		Reminders.lists.byName("Email Snooze").reminders.push(newReminder);
		
	}
		
	// send a Control+Command+a to archive the selected messages
	SystemEvents.keystroke('a', { using: ['control down', 'command down'] });
}

Comments: 34 Responses to “Build an Automator Quick Action To Snooze Email”

    Brad Simmons
    4 years ago

    Your excellent "Snooze Email" program is a real help, even for this 85 year old. Thank you for making the instructions so clear and understandable. The service you offer is a real blessing. Brad

    Wei
    4 years ago

    I tried this but got an error: "Can't get object", when running the Javascript. I wonder what went wrong.

    Wei
    4 years ago

    I figured this out. The script assumes that a Reminder list "Email Snooze" already exists. I needed to create this list first for it to work.

    4 years ago

    Wei: Yes, I say that at 5:52.

    Jörg Brandt
    4 years ago

    Hi, what a helpful thing I was looking for a long time! Thanks a lot and greetings from Germany!

    Jörg Brandt
    4 years ago

    Just one thing: I tried to set the reminder at 9pm in the script, which doesn't work. It still shows me 4pm. How to change this?

    4 years ago

    Jörg: Look at the script carefully. You have to change those things in several places.

    Lawrence Moore
    4 years ago

    This looks like a great convenience. Assuming I install the script on both computers, will I encounter any issues if I access my three email accounts from two computers, a laptop and an iMac?

    4 years ago

    Lawrence: It should act just like if you did these steps manually on one computer. Try it and see.

    Lawrence Moore
    4 years ago

    This looks like a great convenience. Assuming I install the script on both computers, will I encounter any issues if I access my three email accounts from two computers, a laptop and an iMac?

    Lawrence Moore
    4 years ago

    Thanks Gary.
    Oops, I hit the wrong button.

    Jonathan
    4 years ago

    This is fantastic. I would however, like to set reminder to 9am the following day (as one of the options). I can see how to edit the time but not the date. Any ideas anyone?

    4 years ago

    Jonathan: Try to add + 1 to the line:
    var due = new Date()

    Jonathan
    4 years ago

    That's excellent thank you. I tried other things, but not that!

    Jörg
    4 years ago

    Found it. I had to change "due.setHours(16) to (21). Thanks!

    Dean Kutzler
    4 years ago

    This is awesome!! How do I fix: The action “Run JavaScript” encountered an error: “Error: Error: Automator Workflow Runner (WorkflowServiceRunner, Email Action Required) is not allowed to send keystrokes.”?

    4 years ago

    Dean: Did you allow permissions like I show in the video?

    Eyal Dinelton
    4 years ago

    Hi Gary
    Its very useful tool, however what do I have to add to the script if I want to define specific Date & Time ?
    Thanks and greeting from Israel

    4 years ago

    Eyal: The first line where the "due" variable is set you can put a specific date there using JavaScript. But if you want a while interface to appear to ask you pick a time and a date, that will take a lot of work. Probably best to just use the Reminders app unless you are a good coder.

    Scott O'Neill
    4 years ago

    I get an execution error - Error: Error: Can't get object.
    I have allowed permission & verified
    Mail is listed under reminders.
    I script runs & get the dialog box with the email info and displays correctly, then it does not matter which of the two buttons I select it come up with this error.
    Under Accessibility mail was not listed so I added. I tried to close mail and it refuse to close and would not allow the system to reboot. Eventually mail closed, so I rebooted
    Still get this error.

    4 years ago

    Scott: You just need to check and recheck everything. If you still can't get it to work, perhaps find a programmer friend who can help you debug it firsthand.

    Marco
    4 years ago

    Hi Gary, great solution! I work with groups in my reminder, so the script wouldn't be run, if the list are lying in a group. Have you a tip for me, how the code line must look like, with a group-path?

    Cheers, from Germany

    4 years ago

    Marco: I don't think Groups will make a difference. But try it and see. You can check out the documentation in Script Editor to try something different, but it will take work.

    Thomas
    4 years ago

    Hi Gary,
    I was experimenting to add a "This Saturday" button but my JavaScript knowledge is very limited. May you just give me a hint how to calculate the variable starting from today (the time may be the same) ? The way I tried it doesn't work :(

    Thomas
    4 years ago

    My code example inside 'smaller-than' and 'greater-than-signs' disappeared. Here it is: due.setDate((6 - due.getDate())*24); I was tempting to calculate the difference from the value of today's weekday and Saturday (6)…

    4 years ago

    Thomas: Maybe use .getDay() and get the current day of the week, subtract that from 6 to get how many days to add.

    Thomas
    4 years ago

    Gary: Thanks for your help. I finally found a way to get my mail snoozed until a special weekday. This code replaces the "4 Hours From Now" section in your example:
    } else if (dialogResult.buttonReturned == 'Until Saturday') {
    var dueDay = 6;
    var diff = due.getDay() - dueDay;
    if (diff > 0) {
    due.setDate(due.getDate() + 6);
    } else if (diff < 0) {
    due.setDate(due.getDate() + ((-1) * diff))
    }

    Ben Ormerod
    4 years ago

    Fantastic! Now I can delete Spark which keeps losing my emails.
    Re:
    Jonathan: Try to add + 1 to the line:
    var due = new Date()

    I couldn't get this to work, so I made this change:
    if (dialogResult.buttonReturned == '9 AM Tomorrow') {
    due.setDate(due.getDate() + 1);
    due.setHours(09);
    due.setMinutes(0);
    due.setSeconds(0);

    This seems to work.

    Ben Ormerod
    4 years ago

    The automation has started looping; I deleted it and put in a new one with no changes but it repeats the dialogue several times before archiving the email and then quitting. I end up with a pile of identical reminders.

    4 years ago

    Ben: Are you selecting a message before running it?

    Brian
    3 years ago

    This is good. I've been looking for something like this. Thank you.

    Question though. Is it possible to change things in that java script to have it ask you the date and time on when you'd like it to be sent?

    Thank you again.

    3 years ago

    Brian: Yes, it will just take a lot more work to build that. If you are an experienced programmer, it should be no problem. If not, you may want to team up with a coder to get it done.

    Sourav Mandal
    3 years ago

    Hi Gary,

    I copy-pasted the code but it fails with an execution error: "Error: Error: Can't get object." which doesn't indicate in which line the problem is. Can you please help with this?

    Thanks,

    Sourav Mandal
    3 years ago

    Oh, stupid! have just seen the comment from Wei, 11 months ago & fixed it :D

Comments Closed.