When I use the new app.fileManager.processFrontMatter, the console shows an updated frontmatter, but the file itself is not modified.
I am probably missing something…
How do you trigger the template? Are you running “Templater: Replace templates in the active file”? Are you doing an “Templater: Open Insert Template modal”? Are you triggering it through a button, or other means?
The thing is I suspect your code is actually working, but when the template concludes it’s work it also updates the file, but now based upon the previous version, hence the revert of the last changes done by your script.
I’m not sure how to counter that effect, but I do have some ideas I want to test out (tomorrow, not tonight). However, to get the best test setup it would be nice to know how you trigger your template.
One other thing you could test, was to change from <%* in the project note, into <%*+, and then switch to reading mode. Does the change then happen, and stay changed?
Logic flaw in your first version
In you first version you say:
If status is active, change to closed
Afterwards, if status is closed then change to active
So, what’s the net effect of that? A change from active to closed to active again… (It does depend a little on other logic, but it doesn’t look good like it is now)
You should change the middle section to:
} else if ( status === "closed" ) {
This should be done, but there might be other issues, as well
I realize the following side steps the matter of getting the API to modify the frontmatter and it’s not a generalized solution, but in my testing it does toggle the values.
<%*
const status = tp.frontmatter.status;
let change = false;
let oldStat = '';
let newStat = '';
if (status === 'active')
{
change = true;
oldStat = 'status: active\n';
newStat = 'status: closed\n';
}
if (status === 'closed')
{
change = true;
oldStat = 'status: closed\n';
newStat = 'status: active\n';
}
if (change)
{
const content = tp.file.content.split(oldStat).join(newStat);
await app.vault.modify(app.workspace.getActiveFile(),content);
}
-%>
This could have also been done more concisely with some minor code redundancy.
The final goal is to attach the template to a button in order to trigger the code.
Thanks for your comment on the logic issue. I was probably tired yesterday night . Behaviour is the same though.
So far, I tried these triggers which all produce the same behaviour:
Button triggering a template
Templater: Replace templates in the active file
Your assumption is probably correct as the file is getting change when the code is triggered and then imeediately changed back.
So to summarize, when I trigger the code:
The value flickers once and then shows its initial state
The template disapears (expected)
When I click ctrl + z
the correct value is shown
the template comes back
This is so weird…
I tried deactivating all plugins except templater. Same result.
Oh I also tried an example available in chhoumann/MetaEdit: MetaEdit for Obsidian. And it works. But it is displaying a table listing tasks and I don’t want a table…
Evaluation Error: TypeError: Cannot read properties of undefined (reading 'path')
at eval (eval at <anonymous> (plugin:dataview), <anonymous>:4:130)
at DataviewInlineApi.eval (plugin:dataview:18370:16)
at evalInContext (plugin:dataview:18371:7)
at asyncEvalInContext (plugin:dataview:18381:32)
at DataviewJSRenderer.render (plugin:dataview:18402:19)
at DataviewJSRenderer.onload (plugin:dataview:17986:14)
at e.load (app://obsidian.md/app.js:1:864580)
at DataviewApi.executeJs (plugin:dataview:18921:18)
at DataviewPlugin.dataviewjs (plugin:dataview:19423:18)
at eval (plugin:dataview:19344:124)
However, this is two buttons, and so far I’ve not found how to toggle the value more than once. And that has to do with the dataviewjs script and caching, and I’m still on mobile so debugging is a little tricky.
Part of it is that the dataviewjs query is by nature presenting a dynamic result, but the code in the source file never changes (until you manually edit , of course). So dataview doesn’t change the source file, nor has the need by itself to save the file.
However, Templater by nature is changing the source file, and will always try to save the new and updated file. And this creates, from my point of view, a race condition when the templater code actually changes the source file outside of the stuff the templater code knows it’s changing.
I’ve not tested whether it’s possible to circumvent this somehow, but it kind of reminds me of sitting on a branch you’re trying to saw off…