Templater script to create folder-file pair not working?

Hello,

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);
%>
1 Like

thank you. I will try. :slightly_smiling_face:

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.

1 Like

Thanks gino_m. Fabulous :smile:. I added it to periodic notes daily template. I click the button and it prompts. Working.

<%*
let qcFileName = await tp.system.prompt("Note Title")
let titleName = qcFileName
await tp.file.rename('index')
let baseFolder = "/content/post/"
let newFolder = `${baseFolder}/${titleName}/` 
await tp.file.move(newFolder + `index`);
let title = titleName
let description = ""
let slug = ""
let date = ""
let image = ""
let categories = ""
let tags = ""
let weight = ""
setTimeout(() => { app.fileManager.processFrontMatter(tp.config.target_file, frontmatter =>{ frontmatter["title"] = title; 
frontmatter["description"] = description; 
frontmatter["slug"] = slug; 
frontmatter["date"] = date; 
frontmatter["image"] = image; 
frontmatter["categories"] = categories; 
frontmatter["tags"] = tags; 
frontmatter["weight"] = 1; 
}) }, 200)
-%>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.