Hi Gino,
Thanks a lot for testing and feedback! I’m glad you’re putting it to use in your workflow ![]()
Regarding 1, all the functionality you coded is already built into the script (unless I misunderstood something).
By default, missing keys are always added. It’s only in the case of existing keys that the treatments come into play. You can see how that’s handled here:
// Check if property exists, if not, add it
if (!fileCache || !fileCache.hasOwnProperty(prop.key)) {
console.log(`${target.basename} doesn't contain ${prop.key}`);
console.log(`Adding property: ${prop.key} to ${target.basename} with value: ${prop.value}`);
frontmatter[prop.key] = prop.value;
// If property exists, handle it according to treatment type
} else {
console.log(`${target.basename} contains property: ${prop.key}`);
if (treatments.hasOwnProperty(prop.treatment)) {
treatments[prop.treatment](frontmatter, prop, value);
} else {
// Default treatment
treatments['add'](frontmatter, prop, value);
}
}
So the following should do the same as what were trying to prep before letting the script handle it:
<%*
tp.hooks.on_all_templates_executed(async () => {
const formattedDateTime = tp.date.now("YYYY-MM-DDTHH:mm:ss");
const targetFile = tp.config.target_file;
const properties = [
{key:'dg-updated', value: formattedDateTime, treatment: 'update'},
{key:'dg-created', value: formattedDateTime},
{key:'dg_upload', value: 'VNC done', treatment: 'update'},
{key: 'status', value: 'dg_uploaded', treatment: 'update'},
{key: 'share', value: true, treatment: 'update'},
{key: 'dg-publish', value: true, treatment: 'update'},
{key: 'bool', value: true, treatment: 'update'}
];
await tp.user.merge_frontmatter_function2(targetFile, properties);
});
-%>
See demo:

Here, I run the script several times with the same parameters and treatments to showcase how the behavior changes, depending on the treatment, whether the property exists, and whether the values are different.
So if you want something added if it doesn’t exist, but not updated if it does, use add or omit treatment to get the default (same, same). update will add if it doesn’t exist, update if it does (unless the values are the same, in which case, it won’t do anything).
Please let me know if I misunderstood what you meant.
Btw, I noticed that tp is now redundant as a parameter since I changed the script to accept the TFile as a parameter. I’m updating the scripts above to reflect that. I also removed some minor redundancy from the update case.
Regarding point 2:
I couldn’t test this due to the insider bug in processFrontmatter. But thanks for sharing your solution!






