make a template that takes the filename and puts it in the front-matter. it also needs to be able to split the name, preferably using <%tp.file.title.split%>,as that it what I already have. it also needs to be able to work on mobile, as that is my main way of using obsidian. it would be ideal if it would also work on desktop, but that isn’t required.
Things I have tried
i have no idea where to even begin here, i have looked at other posts running into similar issues, but it’s unclear to me whether that would work with the front-matter or not. I have the Quick Add plugin, which could be a workaround, but I’d much rather use the new note functionality.
i would also like to add that, if possible, it would be amazing if i could have a set of queries to fill in some of the front matter properties, though that seems a bit out there for what i can do currently
Where’s the filename coming from? Just normal renaming using the Obsidian interface?
You could try renaming upon note creation with this Templater prompt instead, and have the same title entered there (or a split value) inserted into the frontmatter, like so:
<%*
const title = tp.file.title;
if (title.startsWith("Untitled")) {
const newTitle = await tp.system.prompt("Enter note title");
// You can split the newTitle here and save it in another variable or variables
tp.file.rename(newTitle);
}
-%>
---
title: <% newTitle %>
---
I had a similar issue. The solution was to use “tp.config.target_file.basename” instead of “tp.file.title”. I remember seeing a discussion about this somewhere in the forum, but I don’t remember where. Someone thought it was a bug in templater (the script was executing before tp.file had being totally formed). Hopefully, this works for you.
i used the Create new note from template command, I just copied and pasted what you put in the code block into a new note called “Test template”. it also didn’t like the front matter, I think it has to be the first line of the document to render properly. it rendered the first --- as it would in source mode, and it rendered the second as a horizontal rule.
edit: I checked on desktop, and it still gives the same error, it turns out that the lack of any template was just because, on my phone, I have it set to make new notes in the same folder as the current file, so it never activated any template. on desktop though, it’s set to make them in a specific folder, which activated the template for that folder.
I assume that when you’re saying “on desktop though, it’s set to make them in a specific folder, which activated the template for that folder.” you are referring to folder templates? I was recently informed by @Zachatoo (the current Templater maintainer) that folder templates aren’t meant to be applied concurrently with Templater commands like “Create new note from template”. Only the template chosen by the command ought to be applied.
What I shared was put together a bit too fast from another template of mine. After testing, it appears that the Templater error was due to newTitle not being a global variable, so it was only available within the scope of the conditional. This should fix that:
<%*
const title = tp.file.title;
let newTitle;
if (title.startsWith("Untitled")) {
newTitle = await tp.system.prompt("Enter note title");
// You can split the newTitle here and save it in another variable or variables
await tp.file.rename(newTitle);
}
-%>
---
title: <% newTitle %>
---
However, although this should work, I’m getting some really strange behavior, as documented here in the Discord server: Discord
EDIT: The strange behavior was due to a missing await in front of tp.file.rename. With the included, everything works as expected. The code above is fixed accordingly.
Re the above, did you put the code I gave you at the very top of the document? If not, you should.
Also, you have to make sure you are formatting the content appropriately for the frontmatter, which I can’t tell if you are, since I don’t know what type of titles you are writing and how you are splitting them. For example, you have to surround the title with quotation marks, if it contains any colons.
You can check the validity of your YAML for the frontmatter using:
When I was saying “on desktop”, I was refering to the folder templates, sorry if that wasn’t clear. I don’t know why it triggered the folder template if it was supposed to trigger the one selected by the command, seems weird. I tried the new script you gave, and while it does work, I don’t know JavaScript, so I have some questions about how to split it, would this work?
<%*
const title = tp.file.title;
let newTitle;
if (title.startsWith("Untitled")) {
newTitle = await tp.system.prompt("Enter note title");
tp.newTitle.split(`-`)[1]
await tp.file.rename(newTitle);
}
-%>
---
title: <% newTitle %>
---
I can confirm that the front matter issue was due to an unseen new line at the start of the document, it is fixed now. I haven’t yet implemented this into my original template due to not knowing how to split things properly, though I believe the YAML is formatted correctly, as obsidian renders it fine in live preview mode.
As for how to split, it depends on what you want to achieve and what you are splitting. I can’t really help you without that information. But in any case, you shouldn’t put tp in this line: tp.newTitle.split(`-`)[1]. Also, you need to save it to a new variable, so that you don’t split the newTitle that you want to rename the file to. So it would be something like:
<%*
const title = tp.file.title;
let splitTitle;
if (title.startsWith("Untitled")) {
newTitle = await tp.system.prompt("Enter note title");
splitTitle = newTitle.split(`-`)[1].trim();
await tp.file.rename(newTitle);
}
-%>
---
splitTitle: <% splitTitle %>
---
If you are looking to save the second part after the -, then this looks right. I added a .trim() to remove unwanted whitespace.
If you don’t want to be prompted for a title, but were actually using tp.file.rename in folder templates before with predetermined titles, and that’s what you wanted, you could try fetching the title using tp.config.target_file.basename, like @TheTao recommended. However, in this case, you wouldn’t need to split anything, since it’s already predetermined, and you could just put that info directly in the templates frontmatter.
Thanks for the help, I’ll be implementing this into my template shortly! One more question though, why does the script have an asterisk in <%*? Thank you for the help with this, I would have been completely lost