I’m new to Scriptable and iOS programming. I’ve created the following script that should copy every event in my calendar to reminder.
This code fetches calendar events within the next week and copies them to reminders while handling duplicates based on title and time intervals. It also deals with missing or invalid event data and logs relevant information throughout the process.
// Fetch Calendar events
let now = new Date();
let oneWeekLater = new Date();
oneWeekLater.setDate(oneWeekLater.getDate() + 1);
let events = await CalendarEvent.between(now, oneWeekLater);
let existingReminderTitles = []; // Fetch existing Reminder titles;
let existingReminderTimeIntervals = []; // Fetch existing Reminder time intervals;
// Copy events to Reminders
if (events && Array.isArray(events)) {
for (const event of events) {
if (event instanceof CalendarEvent) {
let title = event.title || "No Title";
console.log(title);
let notes = event.notes;
console.log(notes);
let startDate = event.startDate;
console.log(startDate);
let endDate = event.endDate;
console.log(endDate);
// Check if event with the same title and time interval already exists
let eventTimeInterval = [startDate.getTime(), endDate.getTime()];
if (
existingReminderTitles.includes(title) &&
existingReminderTimeIntervals.some(interval =>
interval[0] === eventTimeInterval[0] && interval[1] === eventTimeInterval[1])
) {
console.log(`Event "${title}" with the same time interval already copied. Skipping.`);
continue; // Skip this event and proceed to the next one
}
// Check if notes is a valid string or set it to an empty string
if (typeof notes !== "string") {
notes = "void";
}
// Check if event has already been copied
if (existingReminderTitles.includes(title)) {
console.log(`Event "${title}" already copied. Skipping.`);
continue; // Skip this event and proceed to the next one
}
let reminder = new Reminder();
reminder.dueDateIncludesTime = true;
reminder.title = title;
reminder.notes = notes;
reminder.dueDate = endDate;
console.log(reminder.identifier + " " + reminder.notes + " " + reminder.dueDate);
try {
reminder.save();
console.log("Reminder saved successfully.");
existingReminderTitles.push(title);
existingReminderTimeIntervals.push(eventTimeInterval);
} catch (error) {
console.log("Error saving Reminder:", error);
}
}
}
// Display success message
let successMessage = `Copied ${events.length} events to Reminders.`;
console.log(successMessage);
} else {
console.log("Error fetching events or events data is invalid.");
}
But when run I get the following error:
2023-08-23 10:15:42: Error on line 53:30: The operation couldn’t be completed. (EKErrorDomain error 29.)
What can it be?