I want to run a dataview script for new note. First, it should ask for a prompt for giving the note title, then it should create a new directory (by that title), then create an "index.md" file inside it.
I have written templater code it is not working. Whats wrong ?
<% let title = await tp.system.prompt("Enter Blog Post Title:")
// Handle blank titles
if (title === "") {
tp.system.showMessage("Please enter a valid title.", { type: "error" });
return;
}
// Construct folder path
const blogFolder = "/content/post/all_posts";
// Check if folder exists
if (!await tp.file.exists(blogFolder)) {
tp.system.showMessage("The specified folder path doesn't exist.", { type: "error" });
return;
}
// Create directory
const dirPath = `${blogFolder}/${title}`;
if (!await tp.file.exists(dirPath)) {
await tp.file.createFolder(dirPath);
}
// Create index.md file and populate basic content
const filePath = `${dirPath}/index.md`;
await tp.file.create(filePath, `---
title: ${title}
date: ${tp.date.now("YYYY-MM-DD")}
tags: []
---
Write your blog post content here.
`);
tp.file.open(filePath);
%>
When you try a template and it doesn’t work, you can open the console with Ctrl+Shift+I, and look at the error message in the console. Here, a parenthesis is missing.
Well, that’s a bit of a stretch.
The first problem is that for templater codes such as these you have to use <%* %> and not <% %>.
Secondly, the tp.system.showMessage function doesn’t exist. Having looked into what the obsidian api had to offer, I think it’s better to use tp.obsidian.notice().
Finally, the tp.file.createFolder function doesn’t exist either. I haven’t found a way yet, but you’ll have to do things differently.
This may be possible with the Obsidian api (tp.obsidian.FileManager.prototype.createNewFolder comes close, but I haven’t yet found out if it’s a usable function and how to use it.
There’s also an error in the functions tp.file.create (tp.file.create_new) and tp.file.open doesn’t exist either.
So, the part that creates the folder still needs to be made functional, but this code comes pretty close:
<%*
let title = await tp.system.prompt("Enter Blog Post Title:");
// Handle blank titles
if (title === "") {
tp.obsidian.Notice("Please enter a valid title.");
return;
}
// Construct folder path
const blogFolder = "/content/post/all_posts";
// Check if folder exists
if (!await tp.file.exists(blogFolder)) {
tp.obsidian.Notice("The specified folder path doesn't exist.");
return;
}
// Create directory
const dirPath = `${blogFolder}/${title}`;
if (!await tp.file.exists(dirPath)) {
// await tp.obsidian.??;
}
// Create index.md file and populate basic content
const filePath = `${dirPath}/index.md`;
await tp.file.create_new(filePath, `---
title: ${title}
date: ${tp.date.now("YYYY-MM-DD")}
tags: []
---
Write your blog post content here.
`);
// tp.file.open(filePath);
%>
Just chirping in about folder creation.
Perusing the Templater snippets kindly shared by Zachatoo, I seem to have found the solution to be tp.file.move as used in this thread/post, but I’d be surprised if that’s the only way. Not being a coder – I’d still say there must be another way that doesn’t require the fs module.
Maybe someone can offer the Obsidian API solution.