Change a property depending on the value of another property

Good evening!
I would like to ask for help from the program experts.
How can I change one property of a note depending on the value of another when saving it?
So, for example, we have a property “status”. If the “start” property is not specified then “status” is “planning” and if it is specified then it is “processing”
If I write a script and plug it into the Templater plugin, it can only be executed once.
How can this be solved, if possible?
Thank you all

Good evening!
I guess you can’t do that after all

It can be done, but it requires some coding and the use of dataviewjs to both pick up the existing value, and then storing it back again using app.fileManager.processFrontMatter(). Examples have been given in this forum recently on how this function can be used.

It could also be possible to use some of the metadata plugins to possible set the value of a property based upon another property. I’ve seen that it seems possible, but I’ve not done that variant myself.

Yes, I have seen implementations of the function. But I haven’t seen the implementation exactly when changing or saving a note

My understanding, from the information I’ve been able to find, is that it can’t be done in such a way that it works the way I see it. That is, when saving or editing a note, or rather its meta tags. So it would be manually running a script or template. Am I right?

Obsidian is doing the save operation more or less continously as you edit so it’s not as easy as tapping into the save function. But I do believe it’s possible to attach to some event that triggers when the file has changed, but since you also want to change the file a loop could be triggered if you don’t take care to avoid it.

So there are two questions which can affect which path to choose:

  • Do you need for the value to be updated before the next time you edit the file?
    • In other words, would it be sufficient for a script in the file to detect the value of the origin field has changed, so then we need to update the other field?
  • Is the calculated field used in many other places/queries, or can it be calculated just in time (that is, in the query using it) for when you need it?

It could be sufficient in your case to use dataviewjs to calculate the new value, and check if it differs from the stored value, and if and only if its changed store the new value.


Another way to deal with it, would be to use some other means to change the original field which would allow you to piggyback another script to update the other field. But I’d try the previous suggestion first.

The logic of the script’s behavior seemed to me to be

function status_text(tp) {
    const status = tp.frontmatter.status;
    const startDate = tp.frontmatter.start;
    const endDate = tp.frontmatter.end;

    /*
    if (startDate === "" && endDate === "") {
        status = "planning";
    } else if (startDate !== "" && endDate !== "") {
        status = "done";
    } else if (startDate !== "" && endDate === "") {
        status = "processing";
    }
    */

    return status;
}

module.exports = status_text;

That is, the presence of one date or another determines the status. And yes, both status and dates are still used in the queries in the note

The issue is solved, using the runjs plugin and the written script