62 lines
2.6 KiB
JavaScript
62 lines
2.6 KiB
JavaScript
/**
|
|||
|
|
* Message Notes - saves the sermon notes from email into Google Drive.
|
||
|
|
*
|
||
|
|
* Runs every 10 minutes in the Google account it is installed in (media@waterschurch.org).
|
||
|
|
* When a "Notes/Slides - ..." email arrives with notes attached, it saves the attachment(s)
|
||
|
|
* plus the email text (used to know who is preaching) into the Drive folder "Message Notes".
|
||
|
|
* Google Drive on the ProPresenter Mac syncs that folder, and the Message Notes tool picks
|
||
|
|
* up the newest file from it.
|
||
|
|
*
|
||
|
|
* Setup: paste this into a new project at https://script.google.com (signed in as media@),
|
||
|
|
* save, choose the function "install" and click Run, then click Allow.
|
||
|
|
*/
|
||
|
|
var FOLDER_NAME = 'Message Notes';
|
||
|
|
var QUERY = '(subject:"Notes/Slides" OR to:sermonslides@waterschurch.org) has:attachment newer_than:21d';
|
||
|
|
var KEEP = 300; // how many processed message ids to remember
|
||
|
|
|
||
|
|
function saveSermonNotes() {
|
||
|
|
var props = PropertiesService.getScriptProperties();
|
||
|
|
var done = JSON.parse(props.getProperty('done') || '[]');
|
||
|
|
var folder = getFolder_();
|
||
|
|
var threads = GmailApp.search(QUERY, 0, 30);
|
||
|
|
threads.forEach(function (thread) {
|
||
|
|
thread.getMessages().forEach(function (msg) {
|
||
|
|
var id = msg.getId();
|
||
|
|
if (done.indexOf(id) >= 0) return;
|
||
|
|
var saved = false;
|
||
|
|
msg.getAttachments({includeInlineImages: false}).forEach(function (att) {
|
||
|
|
var name = att.getName();
|
||
|
|
if (!/\.(rtf|docx?)$/i.test(name)) return;
|
||
|
|
replaceFile_(folder, name, att.copyBlob());
|
||
|
|
var base = name.replace(/\.[^.]+$/, '');
|
||
|
|
var info = 'Subject: ' + msg.getSubject() + '\nFrom: ' + msg.getFrom() + '\nDate: ' +
|
||
|
|
msg.getDate().toISOString() + '\n\n' + msg.getPlainBody();
|
||
|
|
replaceFile_(folder, base + '.email.txt', Utilities.newBlob(info, 'text/plain', base + '.email.txt'));
|
||
|
|
saved = true;
|
||
|
|
});
|
||
|
|
done.push(id);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
props.setProperty('done', JSON.stringify(done.slice(-KEEP)));
|
||
|
|
}
|
||
|
|
|
||
|
|
function getFolder_() {
|
||
|
|
var it = DriveApp.getRootFolder().getFoldersByName(FOLDER_NAME);
|
||
|
|
return it.hasNext() ? it.next() : DriveApp.getRootFolder().createFolder(FOLDER_NAME);
|
||
|
|
}
|
||
|
|
|
||
|
|
function replaceFile_(folder, name, blob) {
|
||
|
|
var old = folder.getFilesByName(name);
|
||
|
|
while (old.hasNext()) old.next().setTrashed(true);
|
||
|
|
folder.createFile(blob.setName(name));
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Run once: saves anything recent now and checks every 10 minutes from then on. */
|
||
|
|
function install() {
|
||
|
|
ScriptApp.getProjectTriggers().forEach(function (t) {
|
||
|
|
if (t.getHandlerFunction() === 'saveSermonNotes') ScriptApp.deleteTrigger(t);
|
||
|
|
});
|
||
|
|
ScriptApp.newTrigger('saveSermonNotes').timeBased().everyMinutes(10).create();
|
||
|
|
saveSermonNotes();
|
||
|
|
}
|