Changing frontmatter value

I can’t change frontmatter “week” to a number.

What I’m trying to do

<%*
async function getNoteInfo() {
// Ask the user for input
const noteName = await tp.system.prompt(“Enter the name for the new note:”);
const weekInput = await tp.system.prompt(“Enter the week number:”);

// Convert the input to a number
const weekNumber = parseInt(weekInput);

// Check if the weekNumber is a valid number
if (!isNaN(weekNumber) && weekNumber >= 1 && weekNumber <= 12) {
    // Set the frontmatter values
    tp.file.rename(noteName);
    tp.frontmatter.week = weekNumber;
} else {
    // Show an error message if the week number is not valid
    alert("Invalid week number. Please enter a number between 1 and 12.");
}

}

// Call the function
getNoteInfo();
%>

Things I have tried

Setting property to number.
Consulting ChatGPT.

Have you tried to use :

// ...
const weekNumber = Number(weekInput);
// ...

Using this and checking the console with:

console.log(typeof weekInput);

The console answered with number :blush:

I’m not sure if the whole template will do what’s expected though :thinking:
I mean, when testing, it renames the note but nothing happens to tp.frontmatter.week

On the other hand, I don’t think tp.frontmatter can be used to update a frontmatter key/Property … but I’m not sure if this is what you’re trying to do :no_mouth: .

Edit to add (:innocent:):
This is the method I’ve used to manipulate Properties within a note (Credits to Zachatoo :blush:):

<%*
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(file, (frontmatter) => {
  frontmatter["status"] = "In progress"; // create a new property or update an existing one by name
  frontmatter["review count"] += 1; // increment the value of an existing property by one
  delete frontmatter["ignored"]; // delete a property
});
-%>
1 Like

After running some tests, this could potentially work :innocent: :

<%*
// Get TFile of the note where the Template is applied
const file = tp.file.find_tfile(tp.file.path(true));  
// Ask the user for input  
const noteName = await tp.system.prompt("Enter the name for the new note:");  
const weekInput = Number(await tp.system.prompt("Enter the week number:"));

if (!Number.isNaN(weekInput) && weekInput >= 1 && weekInput <= 12) {
    // Set the frontmatter value
    await app.fileManager.processFrontMatter(file, (frontmatter) => {
	  frontmatter["week"] = weekInput;
	  });
	// Rename note 
	await tp.file.rename(noteName);
} else {
    // Show an error message if the week number is not valid
    alert("Invalid week number. Please enter a number between 1 and 12.");
}
-%>

The tp.frontmatter is only for reading what was defined in the frontmatter from before the template was executed. It can’t be used for setting any new value, nor reading values you’ve set earlier on in the same template.

For changing the frontmatter, besides actually setting the value directly through text insertion, the proper way is indeed to use the app.fileManager.processFrontMatter() method. Do however be forewarned that using this on a newly created file (which could be the case using a template), can potentially have some issues as mentioned further down in the web page linked before from Zachatoo.

I’ve tried all these suggestions and the number does not change. It still stays blank. However, I’ve had luck with this, but the number is never the one I chose:

<%*
async function getNoteInfo() {
    // Prompt for noteName
    const noteName = await tp.system.prompt("Enter the name for the new note:");
    
	// Use suggester for weekInput
    const weekInput = await tp.system.suggester(["Week 1", "Week 2", "Week 3", "Week 4", "Week 5", "Week 6", "Week 7", "Week 8", "Week 9", "Week 10", "Week 11", "Week 12"], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
    
    // Convert the input to a number
    const weekNumber = Number(weekInput);
    
    // Check if the weekNumber is a valid number
    if (!isNaN(weekNumber) && weekNumber >= 1 && weekNumber <= 12) {
        // Set the frontmatter values
		tp.file.rename(noteName);
		tp.hooks.on_all_templates_executed(async () => {
		  const file = tp.file.find_tfile(tp.file.path(true));
		  await app.fileManager.processFrontMatter(file, (frontmatter) => {
		    frontmatter["week"] = weekNumber;
		  });
		});
    } else {
    
        // Show an error message if the week number is not valid
        alert("Invalid week number. Please enter a number between 1 and 12.");
    }
}

// Call the function
getNoteInfo();
%>

You’re using multiple async functions, but you don’t await them when calling them. That will often cause strange outcomes.

I can’t build up some test files before tomorrow, but most likely it’ll be very similar to your current code with await in front of some of the functions calls.

You are a live saver man. I think what you said made me figure it out:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.