Templater triggering before choosing file's title

What I’m trying to do

I am trying to automatically add and apply templater templates to files created on especific folders, by right clicking the folder and choosing “New note”, but the issue I am having is that it creates a file titled “Untitled” first, and then asks for the file name, so if I use the templater option “Folder Templates” which requires “Trigger Templater on new file creation” to be enable, it automatically runs templater on the file with title “Untitled”, instead of waiting for my input on the name.

I have been trying to find a workaround, and I managed to create a templater command to input for the file name, then rename it and apply it to where it uses the title, but this way when I create a file I am asked twice for it’s name, once by Obsidian, and once by my command, which is not that big of an issue but bothers me a little.

<%*
let title = await tp.system.prompt("File name: ");
tp.file.rename(title);
tR += "# " + title;
%>

Another thing that would work is creating the file using cmd + O, and typing the full path and title of the file, like folder/folder2/File Name, but I would rather be able to right click the folder and click “New note”.

It is a very specific issue and probably there is no perfect solution, but if someone knows any way to solve this I would really appreciate it. I was even considering learning how to create a plugin to make a plugin that disables obsidian asking for a title when creating a file, but that would be too much work.

I have been trying to find a solution for a while and couldn’t find anything, so if there are already threads about this I am sorry, I couldn’t find them.

Files created by the system, will always have the Untitled name first (unless you come from a preset link with the name or similar), but the trick is to detect this within your template for the file, and ask for a filename if it’s not set. (If a file is already present with the Untitled name exists, it add a counter to it like in Untitled 1 and so on, so the check below checks only for the start of the string to catch these cases, as well)

So my templates usually start with something like this:

<%*
let filename = tp.file.title
if ( filename.startsWith("Untitled") ) {
  filename = await tp.system.prompt("File name: ")
  await tp.file.rename(filename)
} 
... Rest of template ...
%>
4 Likes

The problem with that is when I create a file, the same thing will happen that happens on my code, Obsidian will ask for the name, and Templater will ask as well at the same time. I wanted to only need 1 title prompt either from Obsidian or Templater, but not both.
If there was a way to disable Obsidian’s title promp after creating a file, that would solve it.

Which prompt are you talking about? With the setup I described there is only one prompt, and that is the one from Templater. I’ve tested with both creating from the right click in the file explorer, the new daily note button, and using links which don’t trigger the prompt.

Do you have any other plugins presenting the prompt?

Every time I create a new note with right clicking → new note, Obsidian prompts me for a title:

And with the script I get both prompts like this:

But if you are saying you only get one, maybe it is a plugin asking for the first one and I didn’t know. I will see if I can find anything.

I’m trying to do a very similar thing with Templater (pre-populating based on the file’s title) and having the same problem. Fixes or workarounds would be much appreciated.

The only thing I came up with is that script I wrote on the initial post. But it is annoying that you get 2 prompts for file title every time you create a new file.
I am considering learning how to create plugins to create one that asks for the file title before creating the file but I have no idea where to start and even if that is possible.

You shouldn’t get two prompts, what is the template you’re using now? Give us the whole thing

I using a very similar templator script as theofbonin and I’m also getting two prompts. One of them is from the templator script, but the other is built-in to obsidian. The reason not everyone gets the built-in (second) prompt is because it only appears when “Show inline title” is disabled in the Appearance settings (in obsidian 1.0+)

1 Like

I do usually have “Show inline title” enabled, but I do not get the double prompt even with that disabled. And I’m on 1.1.9 with 1.0.0 installer. I then get to edit in the tab header of the new file. Strange stuff is happening

1 Like

If both Show inline title and Show tab title bar are toggled off, Obsidian prompts you for a title. Maybe that?

2 Likes

There we go! Now I got it, as well. But this will then also indicate one way forward, and that is to enable “Show tab title bar”, and use the templater script as suggested. It’ll then only have one prompt, rename the file properly. The only downside is that it leaves the cursor at the file name in the tab header.

And if you add a <% tp.file.cursor() %> where you want the cursor to end, you’ll end up with this kind of workflow:

  • Right-click and select new note
  • Enter the name in the one-and-only prompt
  • See the file be created, and have the cursor at the tab header
  • Hit Enter, and it moves to the place where you placed the cursor
2 Likes

Wow I spent so much time trying to figure out why I was getting two prompts, I checked every setting except for the ones in the appearance tab lol.
Thank you so much for everyone that helped!

For me even with “Show tab title bar” enabled it prompts for a name, but if I turn on “Show inline title” it stops and that is good enough.

I spent the entire day yesterday figuring out how to create plugins and managed to create one that adds a new context menu option for creating files without prompting for the title, now it is useless but at least I learned how to create basic plugins. :sweat_smile:

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