Possible timing issues when renaming, moving a newly created note and then modifying frontmatter

What I’m trying to do

The aim is to:

  • create a meeting note from a template,
  • rename it to the date + title - here 2024-04-26 My Meeting,
  • move it to the correct destination folder( here: Meetings/MyMeeting) and
  • then modify the frontmatter .

Things I have tried

The strange thing is the first steps work, but the frontmatter does not get modified, unlike running it without the first steps.

I have searched the forum, and many related post came up, but this seems to be a combination of issues, I do not have sufficient knowlegde to solve.

Here is the code

---
Created: 
Title: My Meeting
Subject: Text
---
<%*
let title = tp.file.title;
let shortTitle = "My Meeting";
let subject = "";
// get the relative path - includes the filename
let relPath = await tp.file.path(true);
console.log("relative path", relPath);
// if newly created file
if (title.startsWith("Untitled")) {
  title = tp.date.now("YYYY-MM-DD") + " " + shortTitle; 
  console.log("title", title);
  console.log("shortTitle", shortTitle);
  subject = await tp.system.prompt("Subject?");
} 
// rename the file
await tp.file.rename(title);
// get the correct folder
const folder = await app.vault.getAbstractFileByPath("Meetings/MyMeeting");
console.log("folder", folder);
// get the file's tfile instance
const fileName = await tp.file.find_tfile(title);
// make the destination - folder/filename
const dest = folder.path + "/" + title;
console.log("destination", dest);
// move the file to the destination folder
await tp.file.move(dest, fileName);
// modify the frontmatter
tp.hooks.on_all_templates_executed(async () => {
	const filePos = await tp.file.find_tfile(tp.file.path(true));
	//const filePos = app.workspace.getActiveFile();
	console.log("file position", filePos);
	// get the creation date
	const createDate = await tp.date.now("YYYY-MM-DDTHH:mm:ss");
	// get the fileMetadata
	let fileCache = app.metadataCache.getFileCache(filePos).frontmatter;
	console.log("frontmatter cached", fileCache);
	// change the frontmatter 
	const properties = [
		{key:'Title',  value: shortTitle, treatment: 'update'},
		{key: 'Subject', value: subject, treatment: 'update'},
		{key: 'Created', value: createDate, treatment: 'update'},
		{key: 'Tags', value: null, treatment: 'add'}
	];
	await tp.user.merge_frontmatter_function2(filePos, properties);
});
-%>
## Attendees
- John
- Hendrik

The user function - merge_frontmatter_function2 is used courtesy of Feralflora posted before in New Properties and templater prompts and commands? - #59 by Feralflora

I have received very useful help from Feralflora so far, but as the previous post is getting too long, I have been advised to start a new topic.
Any assistance or guidance would be appreciated.

here’s “any” (not an expert on this, sorry):

---
Created: 
Title: My Meeting
Subject: Text
---

may not be necessary as the user script will create the yaml fence

i also move my files before using feralflora’s hook method and it now works (sometimes it didn’t) – don’t ask me the difference

one thing i found out though is that when you are changing stuff in the script, sometimes you need to restart obsidian because a perfectly working script (that the chat robot cannot find fault with and messes it up even more) may not fire the hook…

so patience until…someone will come and sort you out…

Thanks Yurcee

I will try and see. I think I have given up on trying to understand everything in Obsidian, as long as I can get it to work, well, lets say it will suffice.

if you think you are out of luck and want to give up
try a different method
check out Pamela Wang’s utube video on meta template from sratch and you can set individual meeting, book, etc. notes with her method

here’s what can get you started:

from here you can copy the code she’s typing in in the video so no need to type it yourself

then if you are done, you can use the default note template with feralflora’s method as well, provided that in the meantime you sort out the kinks

Thanks again Yurcee. I will definitely check that out!

For those who are interested, although not the elegant Feralflora modification solution, taking out the frontmatter completely and modifying the solution as below works for me. I can add frontmatter with the prompted subject now.

## Attendees
- Pierre
- Hendrik

## Agenda
- My Meeting  

## Notes
- 

## Attachments


## Next meeting
- [ ] 

## Action Items

<% tp.file.cursor() %>


<%*
let shortTitle = "My Meeting";
let subject = "";
let title = tp.file.title;
// get the relative path - includes the filename
let relPath = await tp.file.path(true);
console.log("relative path", relPath);
// if newly created file
if (title.startsWith("Untitled")) {
  // read the new title
  // title = await tp.system.prompt("Title");
  title = tp.date.now("YYYY-MM-DD") + " " + shortTitle; 
  console.log("title", title);
  console.log("shortTitle", shortTitle);
  subject = await tp.system.prompt("Subject?");
} 
// rename the file
await tp.file.rename(title);
// get the correct folder
const folder = await app.vault.getAbstractFileByPath("Meetings/MyMeeting");
console.log("folder", folder);
// get the file's tfile instance
const fileName = await tp.file.find_tfile(title);
// make the destination - folder/filename
const dest = folder.path + "/" + title;
console.log("destination", dest);
// move the file to destination folderñ
await tp.file.move(dest, fileName);
// modify the frontmatter
tp.hooks.on_all_templates_executed(async () => {
	const filePos = await tp.file.find_tfile(tp.file.path(true));
	console.log("file position", filePos);
	// get the creation date
	const createDate = await tp.date.now("YYYY-MM-DDTHH:mm:ss");
	await app.fileManager.processFrontMatter(filePos, (frontmatter) => {
	frontmatter["Title"] = shortTitle; // create a new property or update an existing one by name
	frontmatter["Subject"] = subject;
	frontmatter["Created"] = createDate;
	frontmatter["Tags"] = "";
	});
});
-%>

Glad you found a solution! So removing the “hardcoded” frontmatter was the key? It makes sense to stick to one frontmatter addition technique in the template.

By the way, as you know, there’s been some issues with using tp.hooks.on_all_templates_executed along with processFrontmatter on new notes, under certain circumstances. However, a fix was released in version 2.3.2 of Templater, so hopefully, things will work more reliably now.

And just FYI, using processFrontmatter is not a prerequisite for adding prompt values to the frontmatter. The main reason I use it is to be able to apply templates with frontmatter to existing notes.

You can also do it like so, which is much simpler:

<%*
// Rename the file if untitled
let title = tp.file.title;
const shortTitle = "My Meeting"; // Maybe prompt for the shortTitle?
if (title.startsWith("Untitled")) {
  title = tp.date.now("YYYY-MM-DD") + " " + shortTitle;
  await tp.file.rename(title);
}
// Move the file
const folder = await app.vault.getAbstractFileByPath("Meetings/MyMeeting");
const fileName = await tp.file.find_tfile(title);
const dest = folder.path + "/" + title;
await tp.file.move(dest, fileName);

// Prompt for subject
const subject = await tp.system.prompt("Subject?");
const createDate = await tp.date.now("YYYY-MM-DDTHH:mm:ss");
_%>
---
Title: <% title %>
Subject: <% subject %>
Created: <% createDate %>
Tags:
---

## Attendees

- Pierre
- Hendrik

## Agenda

- My Meeting  

## Notes

- 

## Attachments


## Next meeting

- [ ] 

## Action Items

<% tp.file.cursor() %>

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