Skip to content

Instantly share code, notes, and snippets.

@dasevilla
Last active May 4, 2017 15:14
Show Gist options
  • Save dasevilla/5265703 to your computer and use it in GitHub Desktop.
Save dasevilla/5265703 to your computer and use it in GitHub Desktop.
Google Apps Script to bulk-import Gmail messages into Evernote.
/**
* For every thread with the `Evernote` label:
* - Forward each message in the thread to your Evernote email address
* - Remove the Evernote label
*
* TODO: Figure out a way to trigger this script when a new thread is tagged, right now I run it manually.
*/
/**
* Your evernote Gmail label. Where the emails will be pulled from.
*/
EVERNOTE_LABEL = 'Evernote';
/**
* Your evernote email address. Where the emails be forwarded to.
*/
EVERNOTE_IMPORT_EMAIL = 'something@m.evernote.com';
/**
*
* Return an Evernote formated subject.
*
* Appends the the thread's labels to the messages's
* subject. Each thread is appended as a hash tag.
*
*/
function getSubject(thread, message) {
var labels = thread.getLabels()
labels = labels.map(function(label) {
return '#' + label.getName().toLowerCase();
}).join(' ');
return message.getSubject() + ' ' + labels;
};
/**
*
* Forward each method in the thread to the Evernote
* email address.
*
*/
function handleThread(thread) {
Logger.log('Forwarding: %s', thread.getFirstMessageSubject());
var messages = thread.getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var emailOptions = {
subject: getSubject(thread, message)
};
message.forward(EVERNOTE_IMPORT_EMAIL, emailOptions)
}
};
/**
*
* Ideally this script would trigger when the Evernote label
* was applied to a thread.
*
* Currently, it removes the Evernote label when done.
*
*/
function processLabel(labelName) {
// get the label for given name
var label = GmailApp.getUserLabelByName(labelName);
var threads = label.getThreads();
Logger.log('Processing label "%s", found %s unread', label.getName(), threads.length);
for (var i = 0; i < threads.length; i++) {
handleThread(threads[i]);
threads[i].removeLabel(label);
}
};
function main() {
processLabel(EVERNOTE_LABEL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment