Templater Input Phase: Is It Possible to Stop with ESC Without Creating a Note?

I need help with Templater. The goal is to halt the creation of a note by pressing ESC (while filling out the input boxes) without actually creating it. This is my Templater script. With this script, if I press ESC, it stops the script without creating the note. The issue is that Templater moves a copy of the template to the root directory, and it remains there. If I try to initiate the creation of another note, I encounter a parsing error (the typical error when the file already exists).

<%*
const now = tp.date.now("YYYY-MM-DD")
const fmplant = ["CVB","AGI","AGF","AGU","FSP","YAF"]
const fmmplant = await tp.system.suggester(fmplant, fmplant, false, "Agrati production Plant: ")

// Check if ESC was pressed or the input was cancelled for each prompt
const fmpn_plant = await tp.system.prompt("PN Plant: ")
if (fmpn_plant === null) return;

const fmdesc_product = await tp.system.prompt("Product description: ")
if (fmdesc_product === null) return;

const fmcustomer = await tp.system.prompt("Customer: ")
if (fmcustomer === null) return;

const fmpncustomer = await tp.system.prompt("PN Customer: ")
if (fmpncustomer === null) return;

// If all inputs are provided, move the file
await tp.file.move("001-XXX_PRM/Prodotti/" + fmpn_plant + "/" + fmpn_plant);
-%>

In the picture below, you can see the root directory and the file named “Prodotto” that was copied during the process.

image

Last point: if I press ESC and therefore interrupt the process, I would like to return to the original position, that is, to the starting note.

Could you assist me?

Thank you.

What I’m trying to do

Things I have tried

Usually when a Templater script is issued the file has already been created, so the question then becomes how did you create the file?

Or possibly would it be safe to delete the file instead of moving it?

The solution could also possibly be done using another plugin like QuickAdd to execute the logic, delaying the file creation until one is certain that the file should be created.

1 Like

First of all, thank you for your answer.
The file is created with a button, which calls the script below the button

button
name New product 
type note(prodotto) template
action prodotto
color 
templater true

As for file deletion, it was also my idea, meaning that in case ESC is pressed during input, locate the template saved in the root and delete it. This is the code I tried:

const fmpncustomer = await tp.system.prompt("PN Customer: ");
if (fmpncustomer === null) {
  await tp.file.delete(tp.file.path);
  return;

Unfortunately, the ‘tp.file.delete’ command does not exist …

If you think that the solution might be QuickAdd, I’ll give that a try, but it seemed straightforward to do it in JavaScript.

Thank you for supporting me.

In the end, I found a solution.
I discovered the function to delete the file created and parked by Templater in the root directory.
Below is the source code, which might be useful to someone :slight_smile:

<%*
// path: and  name of file will automatic generate from templater after the call a the template 
const file = app.vault.getAbstractFileByPath("prodotto.md");
const now = tp.date.now("YYYY-MM-DD");
const fmplant = ["CVB","AGI","AGF","AGU","FSP","YAF"];
const fmmplant = await tp.system.suggester(fmplant, fmplant, false, "Agrati production Plant: ");
if (fmmplant === null) {
   await app.vault.trash(file, true);
  return;
}

const fmpn_plant = await tp.system.prompt("PN Plant: ");
if (fmpn_plant === null) {
  await app.vault.trash(file, true);
  return;
}

const fmdesc_product = await tp.system.prompt("Product description: ");
if (fmdesc_product === null) {
await app.vault.trash(file, true);
  return;
}

const fmcustomer = await tp.system.prompt("Customer: ");
if (fmcustomer === null) {
 await app.vault.trash(file, true);
  return;
}

const fmpncustomer = await tp.system.prompt("PN Customer: ");
if (fmpncustomer === null) {
  await app.vault.trash(file, true);
  return;
}

// If all inputs are provided, move the file
await tp.file.move("001-Agrati_PRM/Prodotti/" + fmpn_plant + "/" + fmpn_plant);
-%>

About the second question i asked before :
I would like to return to the original position, that is, to the starting note
I can confirm that the proposed solution also addresses this requirement. After the file is deleted, the system automatically returns you to the original starting point.

Good!

The corrected sentence would be: "Now, one last minor question. In my country, we say something like ‘to put the tie on the pig…’ …
When I press the button and activate Templater, the system initiates the form compilation process before creating the note. Unfortunately, this process displays the source code of the note as a background, which is aesthetically unpleasing. I would like to see a blank page or remain on the initial screen when I press the button.
Before pressing the button :

After I have press the button :

Thank you!

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