Help with Templater and Modal Form Plugins - Preventing Untitled File Creation on ESC

Hi everyone,

I’m integrating Templater and Modal Form plugins in Obsidian but facing an issue. When pressing ESC during form input, it still creates an “Untitled” file.

Here’s what my script looks like:

<%*
const file = app.vault.getAbstractFileByPath("New_Contact-Int1.md");
const now = tp.date.now("YYYY-MM-DD");
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('Newcontactint');
if (result.asFrontmatterString() === null) {
    await app.vault.trash(file, true);
    return;
}
await tp.file.move("000-Agrati_CRM/Agenda/Nominativi-Interni/" + result.asString('{{name}}_{{family_name}}'));
-%>

I need the form to exit without creating a file when ESC is pressed. Any tips or suggestions would be greatly appreciated.

Thanks!

When your script is executing the file has already been created, so in order to make the file disappear you need to delete it from “under your feet”.

Hello holroy,

I’m encountering a persistent issue with the Modal Form plugin in Obsidian where an empty “Untitled” file remains in the root directory after canceling a form. Despite several attempts to programmatically handle this through Templater scripts, the file still appears every time the form is canceled.

Here’s a summary of what I’m trying to achieve and what I’ve tried:

Goal:

  • I am using the Modal Form plugin to input data into a form.
  • If the form input is confirmed, everything works fine—the data is processed correctly.
  • However, if the form is canceled (e.g., pressing ESC), Obsidian automatically creates an empty “Untitled” file in the root directory, which I want to prevent or automatically delete.

Approach Tried:

  • I’ve written a Templater script that checks if the form result is null or the input is canceled.
  • On detecting a cancellation, the script attempts to find and delete any “Untitled” files in the root.
  • I’ve even introduced a delay before the cleanup process to handle any timing issues that might cause the file to be created after the script runs.

Script Example:

<%*
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('Newcontactint');

if (result === null || result.asFrontmatterString() === null) {
    setTimeout(async () => {
        const files = app.vault.getFiles();
        files.forEach(async (file) => {
            if (file.name.startsWith("Untitled")) {
                await app.vault.trash(file, true);
            }
        });
    }, 5000); // Delay to ensure all processes have completed
}

-%>

Despite these efforts, the “Untitled” file still appears. This issue persists even after ensuring there are no race conditions or timing issues with the creation and deletion of the file.

Questions:

  1. Has anyone else encountered this issue with the Modal Form plugin creating unwanted “Untitled” files after cancellation?
  2. Are there any known configurations or settings in Obsidian or the plugin that might be causing this?
  3. Any suggestions on how to modify the script or settings to successfully prevent or automatically delete the “Untitled” file upon form cancellation?

Thank you in advance for any insights or advice you can provide!

As I tried to say before it’s not the _Modal form, which creates the untitled file. It’s the Templater plugin, and you should have the full file name of this available in tp.file.title.

This file should be possible to delete through a delayed execution.

Hello Holroy, I have modified the source in this way but the problem isn’t t solved .

<%*
	const now = tp.date.now("YYYY-MM-DD");
	const modalForm = app.plugins.plugins.modalforms.api;
	const result = await modalForm.openForm('Newcontactint');
	
	// Procedura se il form è confermato
	if (result !== null && result.asFrontmatterString() !== null) {
	    const newName = result.asString('{{name}}_{{family_name}}') + ".md";
	    const destinationPath = "000-Agrati_CRM/Agenda/Nominativi-Interni/" + newName;
	
	    // Rinomina o crea il file nel percorso specificato
	    await app.vault.rename(app.vault.getAbstractFileByPath(tp.file.title + ".md"), destinationPath);
	} else {
	    // Esecuzione ritardata per eliminare il file "Untitled" creato da Templater
	    setTimeout(async () => {
	        const untitledFile = app.vault.getAbstractFileByPath(tp.file.title + ".md");
	        if (untitledFile) {
	            await app.vault.trash(untitledFile, true);
	        }
	    }, 5000); // Ritardo di 5000 ms (5 secondi)
	}
		-%>

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