I want to rename the new file from within the used template instead of giving a name.
Example:
New Note command → select template “random number file” → inside that template file generate random number and rename.
Things I have tried
The problem is in the selected templater file:
<%*
let fileName = '12345';
await tp.file.rename(fileName);
-%>
Then the code in “New Note.md” where the frontmatter is updated is never called.
Changes in “New Note.md”
New option to skip asking for filename.
In the templater file to select:
...
let title = 'TempUntitled';
if(chosen.meta.template_options_skip_ask_for_filename && chosen.meta.template_options_skip_ask_for_filename == true) {
// change later in included template
} else {
title = (await tp.system.prompt('Name for the new file. Will use the current date/time if no value given.', chosen.title || '')) || moment().format('YYYY-MM-DD HH꞉mm꞉ss')
}
...
Ok, I was digging deeper and came up with the following solution:
New Note.md
async function createFromTemplate (template, newNoteName, destinationFolder, openNewNote = false) {
const templatePath = template.templatePath;
destinationFolder = destinationFolder || tp.file.folder(true)
let fullPath = app.vault.getAbstractFileByPath(destinationFolder);
const filenameNewFile = fullPath.path + "/" + newNoteName;
const newFile = await tp.file.create_new(tp.file.find_tfile(templatePath), newNoteName, openNewNote, fullPath)
const newCreatedFile = await tp.file.find_tfile(filenameNewFile);
// rename file, handle frontmatter. wait until new note is fully created.
setTimeout(async () => {
let text = await this.app.vault.read(newCreatedFile);
const renameToValue = extractTemplateRenameTo(text, 'template_rename_to');
if (renameToValue !== null) {
await tp.file.rename(renameToValue);
new Notice("Renamed new file to " + renameToValue + ".", 2500);
}
console.log("get meta data for new file:" + newCreatedFile.path);
const metaDataNewFile = await app.metadataCache.getFileCache(newCreatedFile.path)?.frontmatter || {}
console.log(metaDataNewFile);
// Remove the template properties from the new file and add default data
app.fileManager.processFrontMatter(newCreatedFile, (frontmatter) => {
const ctime = moment(newCreatedFile.stat.ctime);
if(template.meta.template_options_banner) {
frontmatter['banner'] = '"[[' + template.meta.template_options_banner + ']]"'
}
if(template.meta.template_frontmatter_type) {
frontmatter['type'] = template.meta.template_frontmatter_type
}
// add default
frontmatter['uuid'] = ctime.format("YYYYMMDDHHmmssSSS");
frontmatter['created-on'] = ctime.format("YYYY-MM-DD\TH:mm:ssZ");
frontmatter['last-modified-on'] = ctime.format("YYYY-MM-DD\TH:mm:ssZ");
// remove config and options for this template
delete frontmatter.template_destination_folder
delete frontmatter.template_title
delete frontmatter.template_rename_to
delete frontmatter.template_frontmatter_type
delete frontmatter.template_options_create_folder
delete frontmatter.template_options_banner
delete frontmatter.template_options_skip_ask_for_filename
})
return openNewNote ? '' : `[[${newNoteName}]]`
}, 500);
}
And in template selected:
---
template_options_skip_ask_for_filename: true
template_rename_to:
---
<%*
let fileName = '12345'
// rename in New Note.md, see template_rename_to
-%>
...
<%*
setTimeout(() => {
app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {
frontmatter['template_rename_to'] = fileName;
})
}, 200) // the reason for the timeout is to let the template complete first
-%>
This is a bit complicated. The solution is to rename in “New Note.md”, not in the selected template.
But I couldn’t get the actual frontmatter from the newly created file with const metaDataNewFile = await app.metadataCache.getFileCache(newCreatedFile.path)?.frontmatter so I’m reading the content (which is read directly without cache?) and extract the frontmatter with a regex in extractTemplateRenameTo.
Is there a way to get the frontmatter from a new created file?
Similar questions came up recently at least on two occasions.
One of them:
Another:
To be honest, I’m so out of Templater and templates now (and never mastered it), and while I’m pretty sure 6-12 months ago some answers were or might have been given, I cannot find them and actually, I haven’t even tried looking for them…
I did go on here to search for ‘inherit’, but came up with nothing.