Make capture and template part of API #771
Replies: 1 comment
|
Sorry for the very slow reply here. This is the same ask as #655 ( The good news: both things are doable today from a script in your repo, without a registered Template or Capture choice. Template to a new note: module.exports = async ({ app, quickAddApi }) => {
const folder = `Meetings/${quickAddApi.date.now("YYYY")}`;
const title = `${quickAddApi.date.now("YYYY-MM-DD")} Project sync`;
const template = app.vault.getFileByPath("Templates/Meeting.md");
const content = await quickAddApi.format(await app.vault.read(template), {
title,
attendees: "Ana, Ben",
});
if (!app.vault.getFolderByPath(folder)) await app.vault.createFolder(folder);
const note = await app.vault.create(`${folder}/${title}.md`, content);
await app.workspace.getLeaf(true).openFile(note);
};Capture to the end of a section: module.exports = async ({ app, quickAddApi }) => {
const file = app.vault.getFileByPath("Journal.md");
const line = await quickAddApi.format("- {{DATE:HH:mm}} {{VALUE:entry}}", {
entry: "Answered discussion 771",
});
await app.vault.process(file, (text) => {
const lines = text.split("\n");
const start = lines.indexOf("## Log");
let end = lines.findIndex((l, i) => i > start && /^#{1,2} /.test(l));
if (end === -1) end = lines.length;
while (end > start + 1 && lines[end - 1].trim() === "") end--;
lines.splice(end, 0, line);
return lines.join("\n");
});
};I tested both on the current release. The first creates To apply a template into the open note instead, there's If you need the full Capture/Template engine (insert-after options, create-if-not-found, file-exists handling, Templater, task formatting), register the choice once and call If there's a specific capture behavior you can't get this way, tell me which one. I'm open to a small, curated API for a real gap, just not one that exposes our internal choice types. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I mostly manage my automations via js macros, as it is easier to version control them in a common repo.
But when it comes to capture and creating template the only alternative available is via
executeChoicewhich forces me to use the UI to create such and then it doesn't fit in my automation repo. Alternatively, I have to go directly to the obsidian api and have to deal with theworkspaceorvaultobjects and then replacing/adding text feels like writing a plugin on my own.Would it be possible to expose such quickadd actions (capture and template) to the "public" api, or maybe allow a way to define them in a programatically way?
Thanks !
All reactions