Parts of my notes contain a Log-Section where I write down things I did at a specific point in time.
I tried to create a Templater “Script” that adds a Logsection whenever I want to add one. The following Code works fine. I miss only one thing. I would like to place the cursor under the newly created headline (With date and Time).
How can I move the cursor there? Any Ideas or suggestions?
Thanks a lot
<%* var file = app.workspace.getActiveFile()
var t = await app.vault.read(file)
var s = t.replace(“## Log\n”, “## Log\n\n### " + tp.date.now(“YYYY-MM-DD – HH:mm”) +”\n\n")
app.vault.modify(file, s)
%>
Thanks for the Tip. However, I use the template in an already existing file. The Template ist just the container for the script.
I could not find a method to position to place the cursor in JavaScript (editor Object)
<%*
// Get the active file
var file = app.workspace.getActiveFile();
// Read the content of the file
var t = await app.vault.read(file);
// Define the new log entry
var newLog = "## Log\n\n### " + tp.date.now("YYYY-MM-DD – HH:mm") + "\n\n";
// Split the content by lines
var lines = t.split("\n");
// Find the index of the "## Log" line
var logIndex = lines.findIndex(line => line === "## Log");
// Insert the new log entry after the "## Log" line
if (logIndex !== -1) {
lines.splice(logIndex + 1, 0, newLog);
}
// Join the lines back into a single string
var s = lines.join("\n");
// Modify the file with the new content
await app.vault.modify(file, s);
// Set the cursor to the new position after all templates are executed
tp.hooks.on_all_templates_executed(async () => {
const activeLeaf = app.workspace.activeLeaf;
if (activeLeaf) {
const editor = activeLeaf.view.editor;
editor.setCursor({line: logIndex + 4, ch: 0}); // Adjust the line number as needed
editor.focus();
}
});
%>