Tuesday 28 January 2014

Archively: Delayed Archive/Delete in Gmail

Ever wanted some emails to go away on their own? It might be an "I'm away for two weeks" email, and you don't want to delete the email right now or you'll forget that they're not contactable. Or do you get emails like "Fridge is being cleaned this Friday"? Well, now you can set them to be deleted or archived (or both!) when you want them to be. Enter Archively, a Google Apps Script written in JavaScript.

What Archively does is scan for labels that instruct it to archive and/or delete emails. There are two top level labels, namely, "delete" and "arkive" (the label "archive" is reserved by Gmail). To archive, the label should be nested under "arkive", with a name like "in 5 days". To the Google Apps Script, the label will look like "arkive/in 5 days". Here are some examples of labels that are understood by Archively:
  • arkive/at EOD this will archive the message/thread just after midnight (at End Of Day).
  • arkive/at EOD+1d this will archive the message/thread after midnight the following day (at End Of Day plus one more day).
  • arkive/when 7wks old will archive the message/thread when the last message in the thread is 7 weeks old. If any new messages arrive (in this thread), it will have to wait till the latest message is 7 weeks old.
  • delete/in 2 weeks will delete the message/thread two weeks after the label is spotted.
  • delete/on 3 Feb 2014 will delete the message/thread at 12 AM (00:00) on the specified date.
  • delete/on 3 Feb 2014 9:00 will delete the message/thread within an hour after the specified time (I recommend that the script only runs once an hour).
  • There are a few more examples listed at the top of the source code.

One of the main ways I am using Archively for is via a filter. One of the filters I have picks out marketing style emails with a specific keyword in it, and then labels the email with delete/at EOD+2d. To get Archively working for you, follow these easy steps:
  1. Copy the text out of my copy of the script here or here.
  2. Go to Google Drive and create a new Script. If you don't see a "Script" option, you'll need to choose "Connect more apps" and then pick Google Apps Script (also says "Script in Google Drive").
  3. In the new Script, paste in the text that was copied in step 1.
  4. Do a test run if you want: Select the checkEmAll function and run it.
  5. Select menu Resources > Current project's triggers. Add a trigger to run checkEmAll hourly, and cleanDbRecords weekly (or some other frequencies as desired).
  6. At some point you'll need to authorise the script to work with your Gmail account.
  7. From within Gmail, create the base labels delete and arkive. Then you will easily be able to create labels nested under them.
  8. Now you can apply labels directly, or set up filters to apply Archively labels for you.
For those of you that are interested, here is an approximate description of the grammar of the labels understood by Archively in Extended Backus-Naur Form. Note that it is not case-sensitive.
LabelText = "delete/" | "arkive/", TimeSpec;
TimeSpec = EndOfSpec | AgeSpec | WhenSpec | FromNowSpec;

EndOfSpec = "TOMORROW"
          | (["AT", {" "}], "EO", "D"|"W"|"M"|"Y"|"FY", {
TimeSpan});
    Tomorrow is an alias for EOD. The end of day (D), week (W), month (M), year (Y) and financial year (FY) are available. A month is defined as 31 days.
AgeSpec = ["WHEN "], {TimeSpan}, " OLD";
WhenSpec = "ON ", JavaScriptDate;
    The description of JavaScriptDate is left to the Internet. DateTimes such as "14 Jan 2014", and
"14 Jan 2014 11:00" should work just fine.FromNowSpec = ["IN "], {TimeSpan};TimeSpan ={" "}, ["-"|"+"], {" "}, Number, {" "}, TimeUnit;
TimeUnit =
("H", ["R" | "RS" | "OUR" | "OURS"])
         |
("D", ["AY" | "AYS"])
         |
("W",
["K" | "KS" | "EEK" | "EEKS"])
         | ("M", ["ONTH" | "ONTHS"])

         | ("Y",
["R" | "RS" | "EAR" | "EARS"]);
Number = (Digit, {Digit}, [".", {Digit}])
       | (".", Digit, {Digit});
Digit = "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";
Get the the script here or here. Enjoy!
Creative Commons License
Archively by Jonathan Johansen is licensed under a Creative Commons Attribution 4.0 International License.