Skip to content

Instantly share code, notes, and snippets.

@cliedelt
Last active May 11, 2021 16:14
Show Gist options
  • Save cliedelt/a2a45b2d9529b019171b88fa6055f2a6 to your computer and use it in GitHub Desktop.
Save cliedelt/a2a45b2d9529b019171b88fa6055f2a6 to your computer and use it in GitHub Desktop.
Google Calendar - Next Meetings
const manager = FileManager.local();
const configPath = manager.joinPath(manager.libraryDirectory(), 'config.json');
function addDate(date, days) {
const copy = new Date(Number(date));
copy.setDate(date.getDate() + days);
return copy;
}
const isToday = (someDate) => {
const today = new Date()
return someDate.getDate() == today.getDate() &&
someDate.getMonth() == today.getMonth() &&
someDate.getFullYear() == today.getFullYear()
}
const isTomorrow = (someDate) => {
const tomorrow = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 1)
return someDate.getDate() == tomorrow.getDate() &&
someDate.getMonth() == tomorrow.getMonth() &&
someDate.getFullYear() == tomorrow.getFullYear()
}
function formatDate(date) {
if(isToday(date)) {
return 'Heute';
} else if(isTomorrow(date)) {
return 'Morgen';
} else {
return ('0' + date.getDate()).slice(-2) + '.'
+ ('0' + (date.getMonth()+1)).slice(-2);
}
}
function formatTime(date) {
return ('0' + (date.getHours())).slice(-2)
+ ':'
+ ('0' + (date.getMinutes())).slice(-2);
}
function loadConfig() {
if(manager.fileExists(configPath)) {
let readData = manager.readString(configPath);
console.log(readData)
return readData;
} else {
return undefined;
}
}
function saveConfig(data) {
console.log("SAVING")
manager.writeString(configPath, data);
console.log("SAVED");
return;
}
async function getCalendarData(identifier) {
let events = await CalendarEvent.between(new Date(), addDate(new Date(), 99));
console.log(events[0].calendar.identifier, identifier)
events = events.filter((event) => event.calendar.identifier === identifier);
console.log(events.length);
events.sort((a,b) => new Date(b.date) - new Date(a.date));
events = events.splice(0,5);
return events;
}
async function createWidget(data) {
let widget = new ListWidget();
widget.spacing = 5;
widget.backgroundColor = Color.dynamic(new Color('#ffffff', 0.8), new Color('#000000', 0.8));
data.forEach(function(rowData) {
let row = widget.addStack();
let stockSymbol = row.addText(rowData.title);
stockSymbol.textColor = Color.dynamic(new Color('#515150'), new Color('#FFFFFF'));
stockSymbol.font = Font.boldSystemFont(16);
row.addSpacer();
let dateRow = row.addStack();
dateRow.size = new Size(120, 20);
let symbolDate = dateRow.addText(formatDate(rowData.startDate));
symbolDate.textColor = Color.dynamic(new Color('#515150'), new Color('#FFFFFF'));
symbolDate.font = Font.mediumMonospacedSystemFont(16);
dateRow.addSpacer();
let symbolTime = dateRow.addText(formatTime(rowData.startDate));
symbolTime.textColor = Color.dynamic(new Color('#515150'), new Color('#FFFFFF'));
symbolTime.font = Font.mediumMonospacedSystemFont(16);
})
return widget;
}
async function init() {
let identifier = loadConfig();
let eventData = await getCalendarData(identifier);
let widget = await createWidget(eventData);
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
widget.presentMedium();
}
Script.complete();
}
if(loadConfig() !== undefined) {
init();
} else {
let calendars = await Calendar.forEvents();
let alert = new Alert();
alert.title = "Select Calendar";
calendars.forEach((calendar) => {
alert.addAction(calendar.title);
})
alert.presentSheet().then(async (selectedElementIndex) => {
let selectedCalender = calendars[selectedElementIndex];
saveConfig(selectedCalender.identifier);
init();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment