Skip to content

Folder templates with quick switcher

Posted on:December 13, 2022

This setup simulates the folder templates feature but in a way that allows me to type the name of the note without the folder name in the quick switcher, and then be prompted with the folder using tp.system.suggester.

For example, I can hit CMD/CTRL O to open the quick switcher, type the name of the note in the quick switcher dialog, hit SHIFT ENTER to create the note, and be prompted with which folder I want the note to go in and have that note’s folder template be applied.

Creating notes in the file explorer will also work with this setup, if you switch between creating files using the quick switcher and the file explorer.

Table of contents

Open Table of contents

Templater settings

In Templater settings, setup the following settings.

Obsidian settings

In Files & Links settings, set “Default location for new notes” to be either “vault folder” or “In the folder specified below”. If you choose the second option, be sure to pick a folder.

This setting needs to be set in order for the script to know if it should automatically apply a folder template based on it’s path, or if it should prompt the user for where to put the file.

Templates

Following is a list of the templates that are needed for this setup to work. Place these templates in your template folder location (“00 Meta/01 Templates/” for example).

Picker template

The picker template when run will check if the note you’ve created was created in the default location. If it was, then the template will ask you what type of note this new note is, and then place the note in that folder and apply it’s relevant template that you setup in Templater’s folder template settings earlier.

<%*
// Fallback template if no type is selected
let template = 'default-template';
// Get list of folder templates (setup in Templater settings)
const folderTemplates = app.plugins.plugins['templater-obsidian'].settings.folder_templates;
// Get default path for new files
const newFileLocation = app.vault.getConfig("newFileLocation");
const defaultNewFilePath = newFileLocation === "folder"
  ? app.vault.getConfig("newFileFolderPath")
  : "";
// If note is in default note location (likely from quick switcher)`
if (tp.file.folder() === defaultNewFilePath) {
  // Prompt for folder template
  const selectedTemplate = await tp.system.suggester(folderTemplate => folderTemplate.folder, folderTemplates);
  // If folder template is selected and is not the picker template,
  const pickerTemplateFolder = '/';
  if (selectedTemplate && selectedTemplate.folder !== pickerTemplateFolder) {
    // Move file to folder template and set template to apply
    template = selectedTemplate.template;
    await tp.file.move(`${selectedTemplate.folder}/${tp.file.title}`);
  }
}
// Apply template
tR += await tp.file.include(`[[${template}]]`)
_%>

Default template

The default template is the fallback template for if you create a note and you don’t know what type it should be yet. If it’s created from the file explorer, then the template will ask for a note name. If it’s created from the quick switcher, then we already know the file name so it will not ask for a note name.

It will also place a heading at the top of the note with the note’s title and place the cursor a few lines after the heading.

<%*
// Check if note already has title
const hasTitle = !tp.file.title.startsWith("Untitled");
let title;

// If note does not have title, prompt for title and rename file
if (!hasTitle) {
  title = await tp.system.prompt("Title");
  await tp.file.rename(title);
} else {
  title = tp.file.title;
}
_%>

# <% title %>

<% tp.file.cursor() %>

Running the picker template on new file creation

To get the picker template to run on new file creation, we need to go back into our Templater settings and add the picker template to the list of folder templates. The “folder” should be the root folder (/) and the template should be the picker template (“00 Meta/01 Templates/picker-template.md”).

And that’s it! Now you can either create new notes using the quick switcher and be prompted for a note type, or when creating notes from the file explorer the template will be applied without needing to be prompted if creating the note in a folder with a defined type in the picker template.