Renaming files with text prompts in Templater

Hey everyone,
Thanks for taking the time helping a newbie :slight_smile: I do not have a lot of coding experience, but have tried multiple commands in Templater trying to fix my problem.

What I would like to achieve:

  1. Upon triggering the Templater template hotkey, create a new note from specific template - this works fine.
  2. Trigger a text prompt to do two things:
    2.1. Rename the file with the text entered in the prompt.
    2.2. Create a header in the note copying the file name.
  3. Move the file into a specific folder (here named β€œProjects”).
    … not necessarily in this order :slight_smile:

Things I have tried

My (relevant) setup:

  • Obsidian 1.0.3
  • Templater 1.16.0

I have tried (and achieved) various components of my target state, but not the combination. For example, my current version (copied from here ) gives me a text prompt and shifts the note, but only a creates a YAML header and note header with the entered text, but does not rename the file!

<%*
  let title = tp.file.title
  if (title.startsWith("Untitled")) {
    title = await tp.system.prompt("Title");
    await tp.file.rename(title);
  } 
  
  tR += "---"
%>
title:  <%* tR += title %>
created: <% tp.date.now("dddd Do MMMM YYYY HH:mm") %>
Last modified: <% tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm") %>
Aliases: 
Tags:

---
# <%* tR += title %>

### Summary
<% await tp.file.move("/Projects/" + tp.file.title) %>

### Notes
- 

### Flashcards
- 

### References
**Source / author**::
**Link**::  
<div class=iframe-container>
	## Place the embedded link from YouTube in the indented section here.
</div>

Any help would be appreciated!

I think I read somewhere that there is a delay before the tp.file.title gets updated, so one alternative is to use title in the move command. Another option, is to just skip the rename command and use the move command to also rename the file.

This is one I use.

<%*
    const title_prefix = await tp.system.suggester(
        ["πŸŽ₯ ",
        "🐦 Tweet",
        "πŸ’­ Thought",
        "🎧 Podcast",
        "πŸ‘€ Person",
        "πŸ“œ Paper",
        "🌱 Seedling",
        "πŸ“š Book",
        "πŸ“° Article"], 
        ["πŸŽ₯ Video",
        "🐦 Tweet",
        "πŸ’­ Thought",
        "🎧 Podcast",
        "πŸ‘€ Person",
        "πŸ“œ Paper",
        "🌱 Seedling",
        "πŸ“š Book",
        "πŸ“° Article"], 
        false,
        "Type of Note"

    )
    let title = await tp.system.prompt("What is the name of your new note?")
    await tp.file.rename(title_prefix + title)

%>

==<%tp.file.creation_date("YYYY-MM-DD")%>==
1 Like

Hey @holroy ! Thanks for your answer!
Could you tell me how to modify the code in the way you described?

Hey @notralph - thanks for your answer! I replaced the upper part of the code (kept the header), and got the two prompts but it did not seem to work. The file is still β€œUntitled” in its name. This is what I got:



==2022-11-27==

# Test

### Summary


### Notes
- 

### Flashcards
- 

### References
**Source / author**::
**Link**::  
<div class=iframe-container>
	## Place the embedded link from YouTube in the indented section here.
</div>

I’ve not tested, but try removing the first rename line, and change the move command parameters, so that it reads "/Projects/" + title

Hopefully that should do the trick.

That did the trick! The file now renames after the prompt, sets up a corresponding header and is moved to the β€œProjects” folder! Thank you :partying_face: !

Just for reference, I am adding the final code here:

<%*
  let title = tp.file.title
  if (title.startsWith("Untitled")) {
    title = await tp.system.prompt("Title");
  } 
  
  tR += "---"
%>
title:  <%* tR += title %>
created: <% tp.date.now("dddd Do MMMM YYYY HH:mm") %>
last modified: <% tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm") %>
aliases: 
tags:
status:

---
# <%* tR += title %>

### Summary
<% await tp.file.move("/Projects/" + title) %>

### Notes
- 

### Flashcards
- 

### References
**Source / author**::
**Link**::  
<div class=iframe-container>
	## Place the embedded link from YouTube in the indented section here.
</div>
1 Like

Yeah. I only know that it works for me. You probably know more than I do about it. I copied it from someone else. I’ll try and find it. Meanwhile look at this https://iwannabemewhenigrowup.medium.com/part-4-other-things-you-can-automate-using-templater-commands-and-functions-and-other-resources-749cbaf862f7

It might help.

Great! That is a good example for others, like me. :grinning:

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