|
<% |
|
// Helper functions |
|
function getFancyTime(date) { |
|
const options = { hour: '2-digit', minute: '2-digit', timeZoneName: 'short' }; |
|
return new Intl.DateTimeFormat('en-US', options).format(date); |
|
} |
|
|
|
function getDayOfWeek(date) { |
|
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
|
return days[date.getDay()]; |
|
} |
|
|
|
// Variables |
|
const today = new Date(); |
|
const todayTimestamp = Math.floor(today.getTime() / 1000); // Convert to Unix timestamp |
|
const tomorrow = new Date(today); |
|
tomorrow.setDate(today.getDate() + 1); |
|
const tomorrowTimestamp = Math.floor(tomorrow.getTime() / 1000); // Convert to Unix timestamp |
|
|
|
const dayString = getDayOfWeek(today); |
|
const timeString = getFancyTime(today); |
|
|
|
// AppleScript to get tasks from OmniFocus |
|
const omnifocusScript = ` |
|
use framework "Foundation" |
|
use scripting additions |
|
|
|
on convertToJson(recordsList) |
|
set theList to current application's NSArray's arrayWithArray:recordsList |
|
set jsonData to (current application's NSJSONSerialization's dataWithJSONObject:theList options:0 |error|:(missing value)) |
|
set jsonString to (current application's NSString's alloc()'s initWithData:jsonData encoding:(current application's NSUTF8StringEncoding)) |
|
return jsonString as text |
|
end convertToJson |
|
|
|
tell application "OmniFocus" |
|
tell default document |
|
set currentDate to current date |
|
set beginningOfDay to (currentDate - (time of currentDate)) |
|
set endOfDay to (beginningOfDay + 86399) -- This is one second less than a full day. |
|
set theTasks to every flattened task where its due date is greater than or equal to beginningOfDay and its due date is less than or equal to endOfDay |
|
set taskList to {} |
|
repeat with aTask in theTasks |
|
set taskTags to tags of aTask |
|
set tagNameList to {} |
|
repeat with aTag in taskTags |
|
set end of tagNameList to {name:name of aTag, id:id of aTag} |
|
end repeat |
|
set taskDueDate to due date of aTask |
|
set dateString to taskDueDate as «class isot» as string -- Convert the date to an ISO 8601 format string |
|
set taskInfo to {name:name of aTask, id:id of aTask, dueDate:dateString, completed:completed of aTask, projectName:name of containing project of aTask, tags:tagNameList} |
|
set end of taskList to taskInfo |
|
end repeat |
|
return my convertToJson(taskList) |
|
end tell |
|
end tell |
|
`.trim(); |
|
|
|
// GraphQL query for events |
|
const gqlQuery = `query CalendarTemplateQuery($start: Int!, $end: Int!){ |
|
me { |
|
name |
|
calendar_meetings(start: $start, end: $end) { |
|
nodes { |
|
title |
|
start_time |
|
end_time |
|
is_all_day_event |
|
category |
|
} |
|
} |
|
} |
|
} |
|
} |
|
}`; |
|
|
|
// Fetch data simultaneously |
|
const [omnifocusTasksData, eventsData] = await Promise.all([ |
|
runAppleScript(omnifocusScript), |
|
graphql(gqlQuery, { "start": todayTimestamp, "end": tomorrowTimestamp }) |
|
]); |
|
|
|
console.log(dayString); |
|
console.log(eventsData); |
|
console.log(omnifocusTasksData) |
|
|
|
const tasks = []; |
|
const events = eventsData.me.calendar_meetings.nodes |
|
%> |
|
|
|
# <%# dayString %> <%= today.toISOString().split('T')[0] %> |
|
[[<%= today.toISOString().split('T')[0].slice(0,8) + "W" + (Math.floor(today.getDate() / 7) + 1) %>]] |
|
|
|
## Schedule (generated at <%= timeString %>) |
|
|
|
<% events.forEach((event) => { %> |
|
- [ ] <%= getFancyTime(new Date(event.start_time)) %> <%= event.title %> |
|
<% }); %> |
|
|
|
## Omnifocus tasks (generated at <%= timeString %>) |
|
|
|
<% tasks.forEach((task) => { |
|
const taskCompleted = task.completed ? 'x' : ''; |
|
const dueDateString = new Date(task.dueDate).toLocaleDateString('en-US', { month: 'short', day: '2-digit' }); |
|
const dueTimeString = getFancyTime(new Date(task.dueDate)); |
|
const taskTags = task.tags.length > 0 ? task.tags.map(tag => `[[#${tag.name}]]([#${tag.name} on OmniFocus](omnifocus:///perspective/Tag?name=${encodeURIComponent(tag.name)}))`).join(' ') : ''; |
|
%> |
|
- [${taskCompleted}] [<%= task.name %>](omnifocus://task/<%= task.id %>) Due: <%= dueDateString %> at <%= dueTimeString %> <%= task.projectName %> <%= taskTags %> |
|
<% }); %> |
|
|
|
⬅ [[<%= new Date(today.setDate(today.getDate() - 1)).toISOString().split('T')[0] %>]] 🗓 [[<%= new Date(today.setDate(today.getDate() + 2)).toISOString().split('T')[0] %>]] ➡ |