Skip to content

Instantly share code, notes, and snippets.

@merlinmann
Created October 28, 2025 19:12
Show Gist options
  • Save merlinmann/98fb8c2f4721f478bb33fd9661727037 to your computer and use it in GitHub Desktop.
Save merlinmann/98fb8c2f4721f478bb33fd9661727037 to your computer and use it in GitHub Desktop.
Find (or generate) a 'Today' file in Drafts.app
let now = new Date();
// Format date in Pacific Time
let options = {
timeZone: "America/Los_Angeles",
weekday: "long",
year: "numeric",
month: "2-digit",
day: "2-digit"
};
let formatter = new Intl.DateTimeFormat("en-US", options);
let parts = formatter.formatToParts(now);
let weekday = parts.find(p => p.type === "weekday").value;
let month = parts.find(p => p.type === "month").value;
let day = parts.find(p => p.type === "day").value;
let year = parts.find(p => p.type === "year").value;
let iso = `${year}-${month}-${day}`;
let title = `Today ${weekday} ${iso}`;
let marker = `<!-- GENERATED TODAY NOTE -->`;
let body = `- foo
-
- bar
- baz
- bat
-
`;
let existing = Draft.query("", "all", []).filter(d =>
d.title === title && d.content.includes(marker)
);
let targetDraft;
if (existing.length > 0) {
targetDraft = existing[0];
} else {
targetDraft = Draft.create();
targetDraft.content = `${title}\n\n${marker}\n\n${body}`;
targetDraft.update();
}
// Open the draft
editor.load(targetDraft);
editor.activate();
// Set cursor after the second "- "
let content = targetDraft.content;
let cursorPosition = content.indexOf("- \n") + 2;
if (cursorPosition > 1) editor.setSelectedRange(cursorPosition, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment