How can I use Templater to get a prompt for text, append it as a link in today's daily note, then create a page with that as the title? #1200
-
I want to make a command that does the following:
I spent a bunch of time looking stuff up to do this but couldn't figure this out. I think this could be done with a Templater script. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Give the following a try. If your daily note doesn't exist when you run the template, it will throw an error. If your daily notes template takes a long time to execute or requires user input, then this is the option you should go with. <%*
// Find if todays daily note exists
const { folder, format } = app.internalPlugins.getEnabledPluginById("daily-notes").options;
const todaysDailyNote = tp.file.find_tfile(`${folder}/${tp.date.now(format)}`);
if (!todaysDailyNote) {
throw new Error("Daily note for today is required to run this template.")
}
// Prompt for task
const task = await tp.system.prompt("Task name");
// Append task to daily note (\n means line break)
const content = await app.vault.read(todaysDailyNote);
const newContent = `${content}\n![[${task}]]`;
await app.vault.modify(todaysDailyNote, newContent);
// Create task file in tasks folder
const tasksFolder = app.vault.getAbstractFileByPath("Tasks");
const newTaskFile = await tp.file.create_new("", task, false, tasksFolder);
// Open task file in new tab
app.workspace.getLeaf(true).openFile(newTaskFile);
-%> If your daily note isn't setup with a template, or the template runs very quickly and doesn't require any user input, then you could create the daily note if it doesn't exist, likeso. <%*
// Sleep function to wait for set amount of time
const sleep = ms => new Promise(r => setTimeout(r, ms));
// Prompt for task
const task = await tp.system.prompt("Task name");
// Find if todays daily note exists
const { folder, format } = app.internalPlugins.getEnabledPluginById("daily-notes").options;
let todaysDailyNote = tp.file.find_tfile(`${folder}/${tp.date.now(format)}`);
if (!todaysDailyNote) {
// If daily note doesn't exist, create it
app.commands.executeCommandById("daily-notes");
// Creating the daily note will take up the active tab, go back after 100 ms (probably has finished creating by now)
await sleep(100);
app.workspace.activeLeaf.history.back();
todaysDailyNote = tp.file.find_tfile(`${folder}/${tp.date.now(format)}`);
}
// Append task to daily note (\n means line break)
const content = await app.vault.read(todaysDailyNote);
const newContent = `${content}\n![[${task}]]`;
await app.vault.modify(todaysDailyNote, newContent);
// Create task file in tasks folder
const tasksFolder = app.vault.getAbstractFileByPath("Tasks");
const newTaskFile = await tp.file.create_new("", task, false, tasksFolder);
// Open task file in new tab
app.workspace.getLeaf(true).openFile(newTaskFile);
-%> |
Beta Was this translation helpful? Give feedback.
-
I had asked Phind and Perplexity.ai how to solve this and they didn't give good answers, so I gave one web search plugin for ChatGPT Plus: Webpilot a shot. I did a bunch of back and forth with it for JS snippets, giving it the link https://docs.obsidian.md to look through. I did a bunch of editing and made this Templater template, which I added a hotkey for:
|
Beta Was this translation helpful? Give feedback.
I had asked Phind and Perplexity.ai how to solve this and they didn't give good answers, so I gave one web search plugin for ChatGPT Plus: Webpilot a shot. I did a bunch of back and forth with it for JS snippets, giving it the link https://docs.obsidian.md to look through. I did a bunch of editing and made this Templater template, which I added a hotkey for: