What I’m trying to do
I’m trying to write Templater scripts that read Frontmatter information and based on that write other Frontmatter data. The use case is that I insert these template scripts in existing files (or add them to a button) and run them to quickly update the Frontmatter information. However, after the scripts run, the updated data is not saved in the current file.
Below is an example. This script grabs a link to another file linked in the Frontmatter field ‘Project’. This linked file has Frontmatter information that includes a ‘Projectnr’ field. The script copies the information and saves this in the Frontmatter of the active file.
When I run this script (e.g. using the command ‘Templater: Replace templates in the active file’), something weird happens…I see the Projectnr is changed to the right value, but almost immediately the value is reversed again. And if after that I hit cmd-z, the new, correct value reappears (and the inserted Templater code block reappears too). How do I fix this?
<%*
// Get TFile of the note where the Template is applied
const currentFile = tp.file.find_tfile(tp.file.path(true));
// Read the frontmatter of the current file to get the project link
const currentFrontMatter = await app.metadataCache.getFileCache(currentFile)?.frontmatter;
const projectLinkMarkdown = currentFrontMatter[‘Project’];
if (projectLinkMarkdown) {
// Extract the filename from the Markdown link format [[filename]]
const projectName = projectLinkMarkdown.slice(2, -2);
// Find the TFile of the linked project
const projectFilePath = app.metadataCache.getFirstLinkpathDest(projectName, currentFile.path);
if (projectFilePath) {
// Access the frontmatter of the linked file
const projectFileFrontMatter = await app.metadataCache.getFileCache(projectFilePath)?.frontmatter;
const projectNumber = projectFileFrontMatter['Projectnr'];
if (projectNumber) {
// Update the current file's frontmatter with the project number from the linked file
await app.fileManager.processFrontMatter(currentFile, (frontmatter) => {
frontmatter["Projectnr"] = projectNumber;
});
} else {
alert("Project number not found in linked file.");
}
} else {
alert("Linked project file not found.");
}
} else {
alert(“No project linked in the frontmatter.”);
}
%>
Things I have tried
I’ve tried adding the following code to force save and refresh, but that didn’t help.
// Save the file explicitly
await app.vault.modify(currentFile, await app.vault.read(currentFile));
// Force a refresh of the current file await
app.workspace.activeLeaf.openFile(currentFile, { eState:app.workspace.activeLeaf.getViewState() });
The problem I encounter is very similar as described in this forum post:
but the solution in here doesn’t help me forward in writing my own scripts that access and modify the Frontmatter data.