Sunday, July 09, 2023

Organizing my Gmail

 With a little bit of help from ChatGPT and trial-and-error, I have a script that helps me keep my Gmail accounts more organized.  

I have five labels:

**0**, **1**, **2**, **3** and **4**

What the script does:

For mail over 12 hours old, add label **0** and **1**

For mail over 30 days old, remove label **1** and add label **2**

For mail over 90 days old, remove label **2** and add label **3**

For mail over 1 year old, remove label **3** and add label **4**

For any mail that's read, flip it back to unread.

It runs every hour.  (Label **0** is a holdover from something else and it's less important.)


Anyhow, this allows new emails to come into my inbox and if I deal with them right away (that is, I delete them), then everything's good.  If it's not something I can handle right away, it moves out of the inbox.  

It means when I open my mail on my phone, it's never going to be overwhelming, it's just going to be the new stuff.  (I have other filters that move a lot of commercial/bulk mail out of the inbox right away because I don't need to be alerted about sales coupons.)


function processEmails() {
var threads = GmailApp.search("older_than:1y label:**3** -in:trash -in:sent -is:chat");

for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
thread.addLabel(GmailApp.getUserLabelByName("**4**"));
thread.removeLabel(GmailApp.getUserLabelByName("**3**"));
}

var threads = GmailApp.search("older_than:90d label:**2** -in:trash -in:sent -is:chat");

for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
thread.addLabel(GmailApp.getUserLabelByName("**3**"));
thread.removeLabel(GmailApp.getUserLabelByName("**2**"));
}

var threads = GmailApp.search("older_than:30d label:**1** -in:trash -in:sent -is:chat");

for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
thread.addLabel(GmailApp.getUserLabelByName("**2**"));
thread.removeLabel(GmailApp.getUserLabelByName("**1**"));
}

var threads = GmailApp.search("older_than:12h -label:**0** -in:trash -in:sent -is:chat ");

for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
thread.addLabel(GmailApp.getUserLabelByName("**0**"));
thread.addLabel(GmailApp.getUserLabelByName("**1**"));
thread.moveToArchive();
}

var query = "is:read -in:trash -in:sent -is:chat"; // Replace with your desired query
var threads = GmailApp.search(query);
threads.forEach(function(thread) {
var messages = thread.getMessages();
messages.forEach(function(message) {
message.markUnread();
});
});


}



To use this script, follow these steps:
  1. Open your Gmail account in a web browser.
  2. Click on the gear icon in the top right corner and select "View all settings".
  3. Go to the "Advanced" tab.
  4. Scroll down to the "Desktop notifications" section and enable the "Allow external programmatic access to Gmail" option.
  5. Click "Save Changes" at the bottom of the page.
  6. Open the Google Apps Script editor by visiting https://script.google.com/.
  7. Create a new script file and replace the default content with the script provided above.
  8. Save the script and give it a name (e.g., "EmailProcessor").
  9. Click on the clock icon in the toolbar to set up a time-driven trigger.
  10. Configure the trigger to run the processEmails function at your desired interval (e.g., every hour, every day).
  11. Save the trigger and confirm any necessary permissions.
The script will now run automatically based on the trigger you set up and perform the specified actions on your Gmail messages. Make sure to review and adjust the script as needed before deploying it to your Gmail account.

No comments: