// Schedule: */5 * * * *
// Name: Reminders to Todoist
// Description: Move tasks from Apple Reminders to Todoist every 5 minutes
import '@johnlindquist/kit'
import { TodoistApi } from '@doist/todoist-api-typescript'
const todoistToken = await env('TODOIST_API_TOKEN')
const todoist = new TodoistApi(todoistToken)
interface AppleReminder {
name: string;
completed: boolean;
dueDate: Date | null;
notes: string | null;
}
interface AppleList {
listName: string;
reminders: AppleReminder[];
}
const getRemindersJXA = `
(() => {
const Reminders = Application('Reminders');
Reminders.includeStandardAdditions = true;
try {
const allLists = Reminders.lists();
const remindersData = allLists.map(list => {
const reminders = list.reminders().map(reminder => ({
name: reminder.name(),
completed: reminder.completed(),
dueDate: reminder.dueDate() ? reminder.dueDate().toISOString() : null,
notes: reminder.body() || null
}));
return {
listName: list.name(),
reminders: reminders
};
});
return JSON.stringify(remindersData);
} catch (error) {
console.error(\`An error occurred: \${error}\`);
return "[]";
}
})();
`
const remindersJson = await applescript(getRemindersJXA)
const remindersData: AppleList[] = JSON.parse(remindersJson);
for (const list of remindersData) {
for (const reminder of list.reminders) { if (!reminder.completed) {
try {
await todoist.tasks.create({
content: reminder.name,
description: reminder.notes || '',
due_date_utc: reminder.dueDate ? reminder.dueDate.toISOString() : undefined,
}) // Mark as completed in Reminders after successful Todoist creation
const completeReminderJXA = `
(() => {
const Reminders = Application('Reminders');
Reminders.includeStandardAdditions = true;
const lists = Reminders.lists.whose({ name: "${list.listName}" });
if (lists.length > 0) {
const currentList = lists[0];
const remindersToComplete = currentList.reminders.whose({ name: "${reminder.name}" });
if (remindersToComplete.length > 0) {
remindersToComplete.forEach(r => r.completed = true);
return true;
}
}
return false;
})();
`;
await applescript(completeReminderJXA);
console.log(`Moved task "${reminder.name}" from Reminders list "${list.listName}" to Todoist.`);
} catch (error) {
console.error(`Failed to move task "${reminder.name}":`, error);
}
}
}
}
console.log("Reminder sync completed.");