How to modify frontmatter after note creation

I really hope someone can help, as there is likely a super simple solution for this.

Structure
So my structure, is I have individual Areas which each has Subareas, with a main note.

What works
To create these Subareas I have made a Template with Templater that is trigger with Buttons.
The Template prompts me for the name of the Area and what the new Subarea and Main Note should be.

All this works like a charm :slight_smile:
Quite proud of that, having no coding skills :smiley:
But could properly be done much cleaner, so I’m all ears.

The problem
Anyway, the problem or what I can’t figure out is after creating the Subarea (folder) and Main Note, I would like to also update the frontmatter.
Here I have a field for which area and subarea it belongs to.

I have tried using MetaEdit and Metadata Menu, but simply can’t figure it out.

Really hope someone can help

Script

<%* 
//Set new names
let currentareaName = await tp.system.prompt("Current Area")
let folderName = await tp.system.prompt("New Subarea Title")
let titleName = await tp.system.prompt("New Main Note Title")
await tp.file.rename(titleName)

// Define new path
let baseFolder = "Areas"
let subFolder = "Areas - Private"
//let currentareaFolder = "Vacation"
let newFolder = `${baseFolder}/${subFolder}/${currentareaName}/${folderName}/`

// Create new folder and note, if they doesn't exist
if (!tp.file.exists(newFolder)) {
	await this.app.vault.createFolder(newFolder)
	}
	await tp.file.move(newFolder + titleName)
%>

No idea how to do it programatically, but recently I had good success using the YAML section of the Linter community plugin. You can specify keys to be added and in what order and whether you want your arrays multiline, etc.

And of course you can specify anything at file creation with Templater.

1 Like

Really simple solution:

<%* 
//Set new names
const currentareaName = await tp.system.prompt("Current Area")
const folderName = await tp.system.prompt("New Subarea Title")
const titleName = await tp.system.prompt("New Main Note Title")

// Define new path
const baseFolder = "Areas"
const subFolder = "Areas - Private"
// const currentareaFolder = "Vacation"
const newFolder = `${baseFolder}/${subFolder}/${currentareaName}/${folderName}/`

// Create new folder and note, if they doesn't exist
if (!tp.file.exists(newFolder)) {
	await this.app.vault.createFolder(newFolder)
}
await tp.file.move(newFolder + titleName)
%>
---
area: <% currentareaName %>
subarea: <% folderName %>
---

(Also, you don’t need tp.file.rename if you are using tp.file.move, which handles both moving and renaming.)

1 Like

Really that simple, my dumb ass thought that you couldn’t write anything before the frontmatter. :see_no_evil:
I did know the problem was the order - but thought it would make the YAML invalid if anything was written before. As the 3 dashes become a solid line in the template - so simply didn’t test it :upside_down_face:

Embarrassing how much time I spend on this.

Thank you both for the answers and for being so kind about it.

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