Skip to content

Instantly share code, notes, and snippets.

@wolfposd
Created October 30, 2023 11:45
Show Gist options
  • Save wolfposd/837ace186211543c1ee86c64ce7b5f1b to your computer and use it in GitHub Desktop.
Save wolfposd/837ace186211543c1ee86c64ce7b5f1b to your computer and use it in GitHub Desktop.
Scriptable: Clock In Time
// Scriptable script
// Execute Widget to create a one-time reminder for your sheduled workshift
// change the amounts of minutes to your work-duration
// 8 hours = 480
// 8 hours work + 30 min break = 510
// etc
let minutes = 460
let titleFontSize = 22
let reminderCategory = "Worktime"
let reminderTitle = "Go Home"
manageInfoWidget()
let hasreminder = await hasReminderToday();
if(!config.runsInWidget && !hasreminder) {
console.log("Creating reminder");
createReminder()
}
Script.setWidget(await manageHomeWidget())
Script.complete()
async function manageInfoWidget() {
let remDate = await getReminderToday();
let date = await getReminderDate();
var headerText = "CLOCK IN";
var text = "Out Time: "+ formatDate(date);
if(remDate) {
text = "Out Time: " + formatDate(remDate.dueDate);
headerText = "CLOCKED ✅";
}
let widget = createWidget(headerText, text);
widget.presentSmall()
}
async function manageHomeWidget() {
let reminder = await getReminderToday();
var headerText = "CLOCK IN";
var text = "Clock in Time";
if (reminder) {
text = "Outtime: "+ formatDate(reminder.dueDate);
headerText = "CLOCKED ✅";
}
return createWidget(headerText, text);
}
function createWidget(headerText, displayText) {
let widget = new ListWidget()
widget.backgroundColor = Color.gray()
let title = widget.addText(headerText)
title.font = Font.boldRoundedSystemFont(titleFontSize)
widget.addSpacer()
let description = widget.addText(displayText)
description.font = Font.mediumSystemFont(14)
title.textColor = Color.white()
description.textColor = Color.white()
return widget;
}
function formatDate(date) {
let minutes = date.getMinutes() < 10 ? "0"+date.getMinutes() : date.getMinutes();
return date.getHours() + ":" + minutes;
}
async function getReminderToday() {
let reminders = await Reminder.all([await getCalendar()]);
if(reminders.length >0)
return reminders[0];
else
return null;
}
async function hasReminderToday() {
let reminder = await getReminderToday();
return reminder != null;
}
async function createReminder() {
let reminder = new Reminder();
reminder.calendar = await getCalendar();
reminder.title = reminderTitle;
reminder.dueDate = await getReminderDate();
reminder.save();
}
async function getCalendar() {
let c = Calendar.findOrCreateForReminders(reminderCategory)
return c
}
async function getReminderDate() {
let d = new Date(Date.now() + (1000 * 60 * minutes))
return d
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment