Last active
December 19, 2016 20:44
-
-
Save bencharb/7baa8dd669fa146f4bd230d527477206 to your computer and use it in GitHub Desktop.
Kill Outlook OOO/WFH Alerts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Utilities | |
var td_mill = { | |
second: 1000 | |
} | |
td_mill.minute = td_mill.second * 60; | |
td_mill.hour = td_mill.minute * 60; | |
td_mill.day = td_mill.hour * 24; | |
var roundTime = function (adate, roundto) { | |
// roundto examples: 'hours, minutes, seconds' or 'minutes, seconds' or 'seconds' | |
// TODO: doc in JS. | |
var dt = new Date(adate); | |
var round_hour = false; | |
var round_min = false; | |
var round_sec = false; | |
var round_day = false; | |
if (typeof(roundto) !== 'undefined') { | |
round_hour = roundto.indexOf('hour') >= 0 || roundto.indexOf('hr') >= 0; | |
round_min = roundto.indexOf('min') >= 0; | |
round_sec = roundto.indexOf('sec') >= 0; | |
round_day = roundto.indexOf('day') >= 0; | |
}; | |
if (round_sec) { | |
dt = new Date(dt.valueOf() - (dt % td_mill.second)) | |
}; | |
if (round_min) { | |
dt = new Date(dt.valueOf() - (dt % td_mill.minute)) | |
}; | |
if (round_hour) { | |
dt = new Date(dt.valueOf() - (dt % td_mill.hour)) | |
}; | |
if (round_day) { | |
dt = new Date(dt); | |
dt.setHours(0); | |
dt.setMinutes(0); | |
dt.setSeconds(0); | |
}; | |
return dt; | |
}; | |
var config = { | |
dryrun: true | |
} | |
var OutlookCalendarService = function() { | |
this.getCalendar = function(criteria, cal_index) { | |
var Outlook = Application("Outlook"); | |
cal_index = cal_index || 0; | |
var projectCalendars = Outlook.calendars.whose(criteria); | |
return projectCalendars.at(cal_index); | |
}; | |
this.getEvents = function(cal, criteria) { | |
var query = cal.calendarEvents.whose(criteria); | |
return query | |
}; | |
return this | |
}; | |
var mysteryValue = function (value) { | |
if (typeof(value) === 'function') { | |
return value(); | |
} | |
else { | |
return value | |
}; | |
} | |
var Event = function (event_, index) { | |
this.index = index; | |
this.event = event_; | |
this.props = this.event.properties(); | |
this.hasreminder = mysteryValue(this.props.hasReminder); | |
this.name = mysteryValue(this.props.subject); | |
this.startTime = this.event.startTime(); | |
this.endTime = this.event.endTime() | |
this.reprDate = function () { | |
return [this.startTime, this.endTime].join(' - ') | |
}; | |
this.repr = function () { | |
return this.name + ', ' + this.reprDate(); | |
}; | |
this.deleteEvent = function () { | |
if (!config.dryrun) { | |
try { | |
this.event.delete(); | |
return 1 | |
} | |
catch (e) { | |
console.log(e); | |
return 0 | |
} | |
} | |
else { | |
return 1 | |
} | |
} | |
this.removeAlarms = function () { | |
if (this.hasreminder) { | |
if (!config.dryrun) { | |
try { | |
this.event.hasReminder = false; | |
return 1 | |
} | |
catch (e) { | |
console.log(e); | |
this.event.props.hasReminder = false; | |
return 1 | |
} | |
} | |
else { | |
return 1 | |
} | |
} | |
else { | |
return 0 | |
} | |
} | |
return this | |
} | |
var AlarmKiller = function () { | |
this.killBadAlarms = function (events, showlog) { | |
var event_len = events.length; | |
if (showlog !== false) { | |
showlog = true; | |
}; | |
if (showlog) { | |
console.log('Total events found: ' + event_len); | |
}; | |
var total_alarms_removed_count = 0; | |
for (var i=0; i < events.length; i++) { | |
var event_ = events[i]; | |
var alarms_removed = event_.removeAlarms(); | |
if (showlog && config.dryrun) { | |
console.log('Dry run, did not actually remove alarm') | |
} | |
if (showlog && alarms_removed) { | |
console.log('\nRemoved ' + alarms_removed + ' alarms at index ' + event_.index + '. Event\n from event ' + event_.repr()); | |
} | |
total_alarms_removed_count+=alarms_removed; | |
console.log('\n') | |
}; | |
if (showlog) { | |
console.log('Removed ' + total_alarms_removed_count + ' alarms from ' + event_len + ' events'); | |
}; | |
}; | |
return this | |
}; | |
var EventKiller = function () { | |
this.killEvents = function (events, showlog) { | |
var event_len = events.length; | |
if (showlog !== false) { | |
showlog = true; | |
}; | |
if (showlog) { | |
console.log('Total events found: ' + event_len); | |
}; | |
var total_events_removed_count = 0; | |
for (var i=0; i < events.length; i++) { | |
var event_ = events[i]; | |
if (!config.dryrun) { | |
var events_removed = event_.deleteEvent(); | |
if (showlog && events_removed) { | |
console.log('\nRemoved event at index ' + event_.index + ' \n' + event_.repr()); | |
} | |
} | |
else { | |
if (showlog) { | |
console.log('Dry run, did not actually delete event') | |
} | |
} | |
total_events_removed_count +=events_removed; | |
console.log('\n') | |
}; | |
if (showlog) { | |
console.log('Removed ' + total_events_removed_count + ' events from ' + event_len + ' events'); | |
}; | |
}; | |
return this | |
}; | |
var dateBetweenCriteriaOutlook = function (start, end) { | |
var data = {_and: | |
[{startTime: {_greaterThanEquals: start}}, | |
{endTime: {_lessThanEquals: end}}] | |
}; | |
return data; | |
}; | |
var app = Application.currentApplication(); | |
app.includeStandardAdditions = true; | |
var addDays = function(dt, days) { | |
return new Date(dt.valueOf() + td_mill.day*days); | |
} | |
var createDateCriteria = function (startdate, enddate, shouldroundtime) { | |
if (typeof(startdate) === 'undefined') { | |
startdate = new Date(); | |
}; | |
if (typeof(shouldroundtime) !== false) { | |
shouldroundtime = true | |
}; | |
if (shouldroundtime) { | |
startdate = roundTime(startdate, 'day') | |
} | |
if (typeof(enddate) === 'undefined') { | |
enddate = new Date(startdate.valueOf() + td_mill.day*14); | |
}; | |
if (shouldroundtime) { | |
enddate = roundTime(enddate, 'day'); | |
}; | |
console.log('Removing events between dates: ' + startdate + ' and ' + enddate); | |
return dateBetweenCriteriaOutlook(startdate, enddate) | |
}; | |
var CalendarCriteria = function (cal_criteria, cal_index) { | |
this.criteria = cal_criteria; | |
this.cal_index = cal_index; | |
return this | |
}; | |
var collectEvents = function (mycalcriteria, days_out, event_criteria) { | |
var startdate = app.currentDate(); | |
var enddate = addDays(startdate, days_out) | |
var date_criteria = createDateCriteria(startdate, enddate, true); | |
if (typeof(event_criteria) === 'undefined') { | |
event_criteria = {}; | |
throw('event criteria not defined'); | |
}; | |
var calservice = OutlookCalendarService(); | |
var Outlook = Application("Outlook"); | |
var cal_index = mycalcriteria.cal_index; | |
if (typeof(cal_index) == 'undefined') { | |
throw('Cal index is undefined'); | |
} | |
var projectCalendars = Outlook.calendars.whose(mycalcriteria.criteria); | |
var calendar = projectCalendars.at(cal_index); | |
var events = calendar.calendarEvents.whose(event_criteria); | |
var events_array = []; | |
var event_len = events.length; | |
for (var i=0; i < event_len; i++) { | |
var event_ = events.at(i).get(); | |
events_array.push(new Event(event_, i)); | |
}; | |
return events_array | |
}; | |
var killMyAlarms = function (mycalcriteria, days_out, event_criteria) { | |
var events_array = collectEvents(mycalcriteria, days_out, event_criteria); | |
var alarm_killer = new AlarmKiller(); | |
return alarm_killer.killBadAlarms(events_array, true) | |
}; | |
var killMyEvents = function (mycalcriteria, days_out, event_criteria) { | |
var events_array = collectEvents(mycalcriteria, days_out, event_criteria); | |
var event_killer = new EventKiller(); | |
return event_killer.killEvents(events_array, true) | |
}; | |
// Customize these fields based on your outlook calendars | |
var cal_criteria = { | |
criteria: { | |
name:'Calendar' // Meh, probably that's your calendar name. | |
}, | |
cal_index: 1, // And, you may have two calendars. Mine is at index 1. Take a guess! :) | |
}; | |
var mycalcriteria = new CalendarCriteria(cal_criteria.criteria, cal_criteria.cal_index) | |
// var defaultIf(val, default_val, criteria_func) { | |
// if (typeof(case) !== true) { | |
// case = false | |
// } | |
// } | |
// var CriteriaItem = function (func, val, case) { | |
// if (typeof(case) !== true) { | |
// case = false | |
// } | |
// }; | |
// var generateSubjectContains = function () | |
var event_criteria = {_or: [{subject: {_contains: 'OOO'}}, {subject: {_contains: 'ooo'}}, {subject: {_contains: 'WFH'}}, {subject: {_contains: 'wfh'}}]}; | |
var daystokill = 10; | |
var killAlarms = function(data) { | |
if (data.i_know_what_im_doing) { | |
var criteria = {_and:[{hasReminder: true}, event_criteria]}; | |
killMyAlarms(mycalcriteria, daystokill, criteria) | |
} | |
else { | |
console.log('Hi, you need to modify this script a bit before using it. Please edit some criteria above, test it, and then you can uncomment the main command.') | |
} | |
} | |
var killEvents = function(data) { | |
if (data.i_know_what_im_doing) { | |
var criteria = event_criteria; | |
killMyEvents(mycalcriteria, daystokill, criteria) | |
} | |
else { | |
console.log('Hi, you need to modify this script a bit before using it. Please edit some criteria above, test it, and then you can uncomment the main command.') | |
} | |
} | |
config.dryrun = true; | |
// killAlarms({i_know_what_im_doing:false}); | |
killEvents({i_know_what_im_doing:false}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment