Skip to content

Instantly share code, notes, and snippets.

@alexflint
Created December 16, 2012 22:55
Show Gist options
  • Save alexflint/4313894 to your computer and use it in GitHub Desktop.
Save alexflint/4313894 to your computer and use it in GitHub Desktop.
A google apps script that reminds me that I have a package delivery. It schedules an SMS for the time I arrive home (about 6:30pm) on any day when I receive an email with a certain label.
/**
* Author: alex.flint@gmail.com
* Date: December 16, 2012
*
* A google apps script that reminds me that I have a package delivery. It
* schedules an SMS for the time I arrive home (about 6:30pm) on any day when
* I receive an email with a certain label.
*/
////////////////////////////////////////////////////////////////////////////////
// VARIABLES TO CONFIGURE
////////////////////////////////////////////////////////////////////////////////
// Time at which to send reminder. Default is 6:30pm.
var kReminderTimeHour = 18;
var kReminderTimeMin = 30;
// This script will look for emails with this label. If it finds one it will
// schedule a reminder and then remove the label (to avoid multiple reminders)
var kGmailLabel = 'Reminder Pending'
// The script will insert reminders into the calendar with the following name.
// Create a calendar with this name and then hide it in order to avoid cluttering
// your calendar. (To create a calendar, log into Google Calendar and click the
// down arrow next to "Calendars" on the left side of the screen.
var kCalendarName = 'Automated';
////////////////////////////////////////////////////////////////////////////////
// IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
function sendText() {
var label = GmailApp.getUserLabelByName(kGmailLabel);
var threads = label.getThreads();
Logger.log('There are '+threads.length+' threads with label "'+label.getName()+'"')
if (threads.length > 0) {
// Construct a date at XX:YY today (if before XX:YY) or XX:YYpm tomorrow (if after XX:YY)
var t = new Date(); // initialized to now
Logger.log("Current time is {0}:{1} on {2}/{3}/{4}".format(t.getHours(),
t.getMinutes(),
t.getDate(),
t.getMonth(),
t.getYear()));
min = t.getMinutes();
hr = t.getHours();
// Add a margin of 2 minutes
var waitUntilTomorrow = false;
if (hr > kReminderTimeHour || (hr == kReminderTimeHour && min > kReminderTimeMin-2)) {
waitUntilTomorrow = true;
Logger.log('Wait until tomorrow!');
}
// Set the reminder time
t.setHours(kReminderTimeHour);
t.setMinutes(kReminderTimeMin);
if (waitUntilTomorrow) {
// Add one full day
// This is better than setDate because it will wrap at the end of months and/or years
t.setTime(t.getTime() + 1000*60*60*24);
}
// Log the reminder time.
Logger.log("Will remind at {0}:{1} on {2}/{3}/{4}".format(t.getHours(),
t.getMinutes(),
t.getDate(),
t.getMonth(),
t.getYear()));
// Get the email address of the active user - that's you
var emailAddress = Session.getActiveUser().getEmail();
calendars = CalendarApp.getCalendarsByName(kCalendarName)
if (calendars.length != 1) {
Logger.log("There were %d calendars named 'Automated'");
} else {
Logger.log('Found the calendar: '+calendars[0].getName())
// Create the event
event = calendars[0].createEvent('You have a package delivery', t, t);
event.removeAllReminders();
event.addSmsReminder(0);
// Remove the label so that we do not send multiple messages
label.removeFromThreads(threads);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment