Skip to content

Instantly share code, notes, and snippets.

@dmuth
Created December 28, 2013 23:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmuth/8165763 to your computer and use it in GitHub Desktop.
Save dmuth/8165763 to your computer and use it in GitHub Desktop.
Some changes I made to the script found at http://jonathan-kim.com/2013/gmail-no-response Specifically: I added the ignoreThisSubject() and threadHasIgnoreLabel() functions.
/*
* 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 unrespondedLabel and
* apply the ignoreLabel.
*
* 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/
*/
// Edit these to your liking.
var unrespondedLabel = '1 No Response';
var ignoreLabel = '1 No Response/Ignore No Response'; // This is a label in a label
var minDays = 3;
var maxDays = 21;
//var maxDays = 6; // Debugging
var ignoreSubjects = [
"drupal_cron"
];
var ignoreSubjectsRe = [];
function main() {
//
// Create regexps of our subject once, up front so we don't hammer the CPU
//
for (var k in ignoreSubjects) {
var subject = ignoreSubjects[k];
var r = new RegExp(subject, "i");
ignoreSubjectsRe.push(r);
}
processUnresponded();
cleanUp();
}
function processUnresponded() {
var threads = GmailApp.search('is:sent from:me -in:chats older_than:' + minDays + 'd newer_than:' + maxDays + 'd'),
numUpdated = 0,
minDaysAgo = new Date();
minDaysAgo.setDate(minDaysAgo.getDate() - minDays);
// Filter threads where I was the last respondent.
for (var i = 0; i < threads.length; i++) {
var thread = threads[i],
messages = thread.getMessages(),
lastMessage = messages[messages.length - 1],
lastFrom = lastMessage.getFrom(),
lastMessageIsOld = lastMessage.getDate().getTime() < minDaysAgo.getTime(),
subject = thread.getFirstMessageSubject()
;
if (
isFromMe(lastFrom)
&& lastMessageIsOld
&& !ignoreThisSubject(subject)
&& !threadHasIgnoreLabel(thread, subject)
) {
markUnresponded(thread);
numUpdated++;
}
}
Logger.log('Updated ' + numUpdated + ' messages.');
}
/**
* Check to see if this thread has the "ignore" label on it.
* I put this in a spearate function so I can more easily debug things.
*/
function threadHasIgnoreLabel(thread, subject) {
if (threadHasLabel(thread, ignoreLabel)) {
Logger.log("Thread '" + subject + "' has the 'ignore' label set!");
return(true);
}
Logger.log("Thread '" + subject + "' is NOT to be ignored!");
return(false);
} // End of threadHasIgnoreLabel()
/**
* Check and see if this subject is one that should be ignored.
*
* @return {boolean} True if this subject should be ignored, false otherwise.
*/
function ignoreThisSubject(subject) {
for (var k in ignoreSubjectsRe) {
var r = ignoreSubjectsRe[k];
if (r.test(subject)) {
return(true);
}
}
//
// Assume we did not find a match
//
return(false);
} // End of ignoreThisSubject()
function isFromMe(fromAddress) {
var addresses = getEmailAddresses();
for (i = 0; i < addresses.length; i++) {
var address = addresses[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 (i = 0; i < labels.length; i++) {
var label = labels[i];
if (label.getName() == labelName) {
return true;
}
}
return false;
}
function markUnresponded(thread) {
var label = getLabel(unrespondedLabel);
label.addToThread(thread);
}
function getLabel(labelName) {
var label = GmailApp.getUserLabelByName(labelName);
if (label) {
} else {
label = GmailApp.createLabel(labelName);
}
return label;
}
function cleanUp() {
var label = getLabel(unrespondedLabel),
iLabel = getLabel(ignoreLabel),
threads = label.getThreads(),
numExpired = 0,
twoWeeksAgo = new Date();
twoWeeksAgo.setDate(twoWeeksAgo.getDate() - maxDays);
if (!threads.length) {
Logger.log('No threads with that label');
return;
} else {
Logger.log('Processing ' + threads.length + ' threads.');
}
for (i = 0; i < threads.length; i++) {
var thread = threads[i],
lastMessageDate = thread.getLastMessageDate(),
subject = thread.getFirstMessageSubject();
// Remove all labels from expired threads.
if (lastMessageDate.getTime() < twoWeeksAgo.getTime()) {
numExpired++;
Logger.log("Thread '" + subject + "' expired");
label.removeFromThread(thread);
iLabel.removeFromThread(thread);
} else {
Logger.log("Thread '" + subject + "' NOT expired");
}
}
Logger.log(numExpired + ' unresponded messages expired.');
}
@franzellin
Copy link

I need help with your evolution script,

1?) in line 23, if I put commas, may I also have more Subjects that I would like to ignore?
2?) If a counterpart answers my mails, but changes the subject I originally wrote for instance (original text), adding something before my text, for instance ( answer to: "original text") I would like that my mail does not appears in the no response ones
3?) if the same person of ?2 answers also with a different mail address, I would like that my mail does not appears in the no response ones

If You have an idea , it would be appreciated due to the fact that with your evolution you centered the problem...

thanks - renato

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