Skip to content

Instantly share code, notes, and snippets.

@robby1066
Last active March 20, 2021 22:14
Show Gist options
  • Save robby1066/5274713 to your computer and use it in GitHub Desktop.
Save robby1066/5274713 to your computer and use it in GitHub Desktop.
A short Google Apps script that will label any starred items in your inbox that are older than 14 days with a "DEAL WITH ME" label. Go to https://developers.google.com/apps-script/reference/gmail/ for documentation.
/**
* Get the "DEAL WITH ME" label, or create it if it doesn't exist
* This is called by the other two functions, you shouldn't need to call it directly
*/
function _getNaggingLabel() {
/**
* If you'd like your label to say something different, modify it here
*/
var nagging_label_text = "DEAL WITH ME";
var label = GmailApp.getUserLabelByName(nagging_label_text);
if (label == null) {
var label = GmailApp.createLabel(nagging_label_text);
}
return label;
}
/**
* Search for starred items in the inbox that are older than 14 days
* apply a label called "DEAL WITH ME"
*/
function addNaggingLabels() {
var label = _getNaggingLabel();
var threads = GmailApp.search('in:inbox label:Starred older_than:14d');
for (var i = 0; i < threads.length; i++) {
label.addToThread(threads[i]);
}
}
/**
* SCAN THROUGH THE "DEAL WITH ME" label and unlabel any items that aren't currently in the inbox
*/
function removeNaggingLabels() {
var label = _getNaggingLabel();
var threads = GmailApp.search('label:DEAL-WITH-ME');
for (var i = 0; i < threads.length; i++) {
if (threads[i].isInInbox() == false || threads[i].hasStarredMessages() == false) {
label.removeFromThread(threads[i]);
}
}
}
@robby1066
Copy link
Author

Updated 4/23/13 to make the remove function more flexible. You should be able to un-star OR archive a thread to trigger the label removal. Previously only archiving would remove the label.

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