Templater: Create new file from template syntax error

What I’m trying to do

Using the following the code, I’ve got a 3 step process of:

  1. Create a new file
  2. Insert the template (that has this code)
  3. Replace templates (to rename the file, move it to the folder it needs to be in, and drop the cursor where I want it).
<% await tp.file.move("04 AREAS/FAIM/Program Logs/" + tp.file.creation_date("MM-DD-YYYY, dddd")) %>
**Client:** <% tp.file.cursor() %>
**Activities:** <% tp.date.now("MM-DD-YYYY, dddd") %>:  - Lawrence C

As the 3 step process, it works fine but the I figured I should be able to use Templater’s “Create new file from template” command and the Trigger Templater on new file creation setting to get it done in one command.

No go. Syntax error.

I’ve been just cutting and pasting stuff from the web as I know nothing of Java/TypeScript.

Any help will be greatly appreciated.

Please post a screenshot of the developer console (press CMD or CTRL+SHIF+I in the desktop version) when the syntax errors occur.

It will be difficult to try to help you without knowing the specific error.

1 Like

I believe this is caused because the file doesn’t exist yet, so when it tries to move it an error occurs.

What I have found in the past works is to put the movement part into a delayed function that only triggers a second or two after file creation.

Try adding this section to the bottom of your template

<%_*
async function move() {
	await new Promise(r => setTimeout(r, 2000)); // 2 second delay
	await tp.file.move("04 AREAS/FAIM/Program Logs/" + tp.file.creation_date("MM-DD-YYYY, dddd"))
}
move()
%>

It is important not to await the move function, as we want the file to be created before the move is triggered.

1 Like

@mr_abomination you might find it a bit simpler if you do the timeout like this, rather than needing the Promise and function:

...Your template here...

<%*
// Move the file to the destination location after 2 seconds
setTimeout(() => {
  tp.file.move("04 AREAS/FAIM/Program Logs/" + tp.file.creation_date("MM-DD-YYYY, dddd"))
}, 2000)
%>
3 Likes

This topic has had alot of discussion and the below script is the only one that has never failed for me (explitives deleted):

let newfilename = "Thousand Words " + tp.date.now("YYYY-MM-DD ddd")
await tp.file.rename(newfilename)
await tp.file.move("/ThousandWords/" + newfilename)
1 Like

Thank you! I didn’t know how to show the console. When I did and tested, I believe the problem was that there was already a file with that name in the directory. :blush:

1 Like

I seen other solutions that break it down into steps like this but then people commented that it was better to make it all one line. I like this solution of your’s as it’s easier for me to understand what’s going on.

Interesting… I added the setTimeout and now it’s adding some numbers in. Here’s the output once run:

Program Log

3971
Client:
Activities: 08-02-2023, Wednesday: - Lawrence C

I’m not too sure what the 3971 is. Any idea?

Here’s the updated template:

<% setTimeout(() => { tp.file.move("04 AREAS/FAIM/Program Logs/" + tp.file.creation_date("MM-DD-YYYY, dddd") + " (FAIM)") }, 2000) %>
**Client:** <% tp.file.cursor() %>
**Activities:** <% tp.date.now("MM-DD-YYYY, dddd") %>:  - Lawrence C

It’s because you changed the code - you are using <% rather than <%* at the start of the timeout section.

See the docs here for what the <%* mode is:

https://silentvoid13.github.io/Templater/commands/execution-command.html

Excellent! Thanks for noticing that AND giving me the reference in the docs to read over. That will help in my learning this stuff.

For future reference for those that may come across this post, I did not select a solution for this problem, because the problem (getting a syntax error when when running the Templater command “Templater: Create new note with template” was that I had successfully created a new note the first time but then forgot to delete it, made some changes, tested and got an error because I already had a file with the same name. I didn’t know how to access the developer console which was prompted by @stef.rausch in this thread and clarified for Mac by @JonathanBuchh in this thread.

In a side win, @AlanG helped me fix up template to do what I wanted it to from the start!

So the template now looks like this:

## Program Log
<%* setTimeout(() => { tp.file.move("04 AREAS/FAIM/Program Logs/" + tp.file.creation_date("MM-DD-YYYY, dddd") + " (FAIM)") }, 2000) %>
**Client:** <% tp.file.cursor() %>
**Activities:** <% tp.date.now("MM-DD-YYYY, dddd") %>:  - Lawrence C

Once run with Templater: Create new note from template my new note is moved to the folder I’ve specified and it’s ready for the day looking like this:

08-09-2023, Wednesday (FAIM)

Program Log

Client:
Activities: 08-09-2023, Wednesday: - Lawrence C

It works every time with no problems! Well… as long as I don’t have a file with the same in the destination folder anyway…

Thanks everyone for helping me get this sorted out! I appreciate it!

Lawrence