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 mink 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: 32 Responses to “Build an Automator Quick Action To Snooze Email”

    Brad Simmons
    10 months 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
    10 months ago

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

    Wei
    10 months 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.

    10 months ago

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

    Jörg Brandt
    10 months ago

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

    Jörg Brandt
    10 months 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?

    10 months ago

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

    Lawrence Moore
    10 months 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?

    10 months ago

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

    Lawrence Moore
    10 months 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
    10 months ago

    Thanks Gary.
    Oops, I hit the wrong button.

    Jonathan
    10 months 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?

    10 months ago

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

    Jonathan
    10 months ago

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

    Jörg
    10 months ago

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

    Dean Kutzler
    10 months 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.”?

    10 months ago

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

    Eyal Dinelton
    9 months 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

    9 months 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
    9 months 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.

    9 months 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
    8 months 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

    8 months 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
    7 months 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
    7 months 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)…

    7 months 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
    7 months 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
    7 months 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
    7 months 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.

    7 months ago

    Ben: Are you selecting a message before running it?

    Brian
    4 months 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.

    4 months 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.

Leave a New Comment Related to "Build an Automator Quick Action To Snooze Email"

:
:
:
0/500 (500 character limit -- please state your comment succinctly and do not try to get around this limit by posting two comments)