Skip to content

Instantly share code, notes, and snippets.

@Makiah
Last active November 13, 2017 03:31
Show Gist options
  • Save Makiah/01db76b7dd23a98ccba359dff9a4c598 to your computer and use it in GitHub Desktop.
Save Makiah/01db76b7dd23a98ccba359dff9a4c598 to your computer and use it in GitHub Desktop.
Used to generate school calendars for Apple Calendars (iCal whatever) for NHS schedules.
// Create the application.
var app = Application.currentApplication()
// Ensure that we have the Calendar application available for use.
app.includeStandardAdditions = true
// This function is used when we need to prompt the user for something.
function askUser(prompt) {
var response = app.displayDialog(prompt, {
defaultAnswer: "",
withIcon: "note",
buttons: ["Cancel", "Continue"],
defaultButton: "Continue"
})
// Result: {"buttonReturned":"Continue", "textReturned":"Jen"}
return response.textReturned
}
// Get the required info before we start.
var inDays = parseInt(askUser("Monday is how many days from now?"));
var mondayWas = parseInt(askUser("Monday will be 1: Red1, 2: Blue1, ..."));
var weeksToAdd = parseInt(askUser("How many weeks should be populated?"));
var toSkip = askUser("Which weekdays, starting on monday, aren't school days? (comma space separated)");
var toSkipDays = [];
if (toSkip !== "") {
var days = toSkip.split(", ");
for (var day of days) {
toSkipDays.push(parseInt(day));
}
}
// Tell user the data we just got, in case they want to cancel early (entered something wrong for instance).
app.displayDialog("Monday was " + inDays + " days ago and was a " + mondayWas + " day, populating " + weeksToAdd + " weeks");
// Create Calendar events.
app.includeStandardAdditions = true;
var CalendarApp = Application("Calendar");
var schoolCalendar = CalendarApp.calendars.whose({name: "School Calendar"})[0];
var myClassesRed1 = {
"Guidance Sem": "7:30 - 8:10",
"Rec Activities for Life": "8:15 - 9:35",
"AP Physics C": "9:40 - 11:0",
"AP Economics": "12:57 - 14:17"
};
var myClassesBlue1 = {
"AP Physics C Lab": "7:30 - 8:10",
"AP Psychology": "8:15 - 9:35",
"AP Spanish": "9:40 - 11:0",
"AP Literature Pre-Lunch": "11:05 - 11:22",
"Lunch": "11:27 - 11:52",
"AP Literature Post-Lunch": "11:57 - 12:52",
};
var myClassesRed2 = {
"Rec Activities for Life": "8:15 - 9:35",
"AP Physics C": "9:40 - 11:0",
"AP Economics": "12:57 - 14:17"
};
var myClassesBlue2 = {
"AP Psychology": "8:15 - 9:35",
"AP Spanish": "9:40 - 11:0",
"AP Literature Pre-Lunch": "11:05 - 11:22",
"Lunch": "11:27 - 11:52",
"AP Literature Post-Lunch": "11:57 - 12:52",
};
var toSkipIndex = 0;
var firstMonday = app.currentDate();
firstMonday.setDate(firstMonday.getDate() + inDays);
// The type of day
var scheduleForToday = mondayWas;
var daysPastMonday = 0;
for (var week = 1; week <= weeksToAdd; week++) {
for (var weekday = 1; weekday <= 5; weekday++) {
if (scheduleForToday === 5) {
scheduleForToday = 1; // Re-cycle schedules.
}
if (toSkipDays.length > 0 && daysPastMonday === toSkipDays[toSkipIndex]) {
toSkipIndex++;
daysPastMonday++;
// scheduleForToday++; // Skip a schedule (maybe).
continue;
}
var todaysClasses;
switch (scheduleForToday) {
case 1:
todaysClasses = myClassesRed1;
break;
case 2:
todaysClasses = myClassesBlue1;
break;
case 3:
todaysClasses = myClassesRed2;
break;
case 4:
todaysClasses = myClassesBlue2;
break;
}
// Add new events for today.
for (var newClass of Object.keys(todaysClasses)) {
// Figure out the class times for this period.
var timesForClassToday = todaysClasses[newClass].split(" - ");
// Start times.
var startStringTimes = timesForClassToday[0].split(":");
var startHour = parseInt(startStringTimes[0]);
var startMin = parseInt(startStringTimes[1]);
// Set event params.
var eventStart = new Date();
eventStart.setMonth(firstMonday.getMonth());
eventStart.setDate(firstMonday.getDate() + daysPastMonday);
eventStart.setHours(startHour);
eventStart.setMinutes(startMin);
eventStart.setSeconds(0);
// Start times.
var endStringTimes = timesForClassToday[1].split(":");
var endHour = parseInt(endStringTimes[0]);
var endMin = parseInt(endStringTimes[1]);
// Set event params.
var eventEnd = new Date();
eventEnd.setMonth(firstMonday.getMonth());
eventEnd.setDate(firstMonday.getDate() + daysPastMonday);
eventEnd.setHours(endHour);
eventEnd.setMinutes(endMin);
eventEnd.setSeconds(0);
var event = CalendarApp.Event(
{
summary: newClass,
startDate: eventStart,
endDate: eventEnd
}
);
schoolCalendar.events.push(event);
// app.displayDialog("Adding event " + newClass + " First was " + firstMonday.getDate() + "Start Date " + eventStart + " End Date " + eventEnd);
event
}
daysPastMonday++;
scheduleForToday++;
}
daysPastMonday += 2; // Skip weekend
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment