Skip to content

Instantly share code, notes, and snippets.

@salamanders
Forked from dmuth/gist:8165763
Last active November 11, 2015 22:38
Show Gist options
  • Save salamanders/d7b2f90615ab2adfc1d8 to your computer and use it in GitHub Desktop.
Save salamanders/d7b2f90615ab2adfc1d8 to your computer and use it in GitHub Desktop.
/*
* This script goes through your Gmail Inbox and finds recent emails where you
* were the last respondent. It applies a nice label to them, so you can
* see them in Priority Inbox or do something else.
*
* To remove and ignore an email thread, just remove the unrespondedLabelStr and
* apply the ignoreLabelStr.
*
* This is most effective when paired with a time-based script trigger.
*
* For installation instructions, read this blog post:
* http://jonathan-kim.com/2013/Gmail-No-Response/
*
* label = GmailApp.createLabel(labelName);
*/
// Edit these to your liking.
var unrespondedLabelStr = 'TODO/No Response',
ignoreLabelStr = 'No Response/Ignore',
minDays = 2,
maxDays = 21,
myEmailAddresses = getEmailAddresses();
function processUnresponded() {
var threads = GmailApp.search('is:sent from:me -in:chats -filename:invite.ics older_than:' + minDays + 'd newer_than:' + maxDays + 'd'),
minDaysAgo = new Date(),
label = GmailApp.getUserLabelByName(unrespondedLabelStr),
threadsToLabel = [],
gmailMessageThreads = GmailApp.getMessagesForThreads(threads);
minDaysAgo.setDate(minDaysAgo.getDate() - minDays);
// Filter threads where I was the last respondent.
Logger.log('Checking ' + threads.length + ' threads...');
for (var i = 0; i < threads.length; i++) {
var thread = threads[i],
threadArr = gmailMessageThreads[i],
lastMessage = threadArr[threadArr.length - 1],
lastFrom = lastMessage.getFrom(),
lastMessageIsOld = lastMessage.getDate().getTime() < minDaysAgo.getTime();
if (isFromMe(lastFrom) && lastMessageIsOld && !threadHasLabel(thread, ignoreLabelStr)) {
threadsToLabel.push(thread);
}
}
label.addToThreads(threadsToLabel);
Logger.log('Updated ' + threadsToLabel.length + ' messages.');
}
function isFromMe(fromAddress) {
for (var i = 0; i < myEmailAddresses.length; i++) {
var address = myEmailAddresses[i],
r = RegExp(address, 'i');
if (r.test(fromAddress)) {
return true;
}
}
return false;
}
function getEmailAddresses() {
var me = Session.getActiveUser().getEmail(),
emails = GmailApp.getAliases();
emails.push(me);
return emails;
}
function threadHasLabel(thread, labelName) {
var labels = thread.getLabels();
for (var i = 0; i < labels.length; i++) {
if (labels[i].getName() == labelName) {
return true;
}
}
return false;
}
function cleanUp() {
var label = GmailApp.getUserLabelByName(unrespondedLabelStr),
iLabel = GmailApp.getUserLabelByName(ignoreLabelStr),
threads = label.getThreads(),
maxDaysAgo = new Date(),
threadsToUnLabel = [];
maxDaysAgo.setDate(maxDaysAgo.getDate() - maxDays);
Logger.log('Checking ' + threads.length + ' threads for cleanup.');
for (var i = 0; i < threads.length; i++) {
var thread = threads[i],
lastMessageDate = thread.getLastMessageDate();
// Remove all labels from expired threads.
if (lastMessageDate.getTime() < maxDaysAgo.getTime()) {
threadsToUnLabel.push(thread);
}
}
label.removeFromThreads(threadsToUnLabel);
iLabel.removeFromThreads(threadsToUnLabel);
Logger.log(threadsToUnLabel.length + ' unresponded messages expired.');
}
function main() {
processUnresponded();
cleanUp();
}
@salamanders
Copy link
Author

Changed the biggest calls to batch operations

@hijonathan
Copy link

Awesome! Mind sending this as a pull request so other people can benefit too? The repo is located here: https://github.com/hijonathan/google-scripts

@CCS86
Copy link

CCS86 commented Nov 11, 2015

Hmm, I'm getting this error:

TypeError: Cannot call method "addToThreads" of null. (line 46, file "")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment