Obsidian Templater Scripts
AI-Generated Content
Obsidian Templater Scripts
Obsidian transforms static note-taking into a dynamic network of thought, but manually formatting repetitive notes can stifle productivity. The Templater community plugin supercharges Obsidian's basic templates by injecting the power of JavaScript, allowing you to automate note creation, enforce consistent structures, and process information on the fly. Mastering Templater scripts moves you from simply storing information to building a responsive, intelligent knowledge system that works for you.
From Static Templates to Dynamic Scripts
At its core, Templater replaces Obsidian's native template system with one that can execute JavaScript code directly within your template files. Where a standard template might have a placeholder like {{date}}, Templater allows you to write code that calculates dates, manipulates text, or even prompts you for input before the note is created. This turns your templates from simple stencils into powerful, context-aware generators.
To begin, you must install the Templater plugin via Community Plugins and enable it. The critical next step is to set the "Template folder location" in Templater's settings. This designated folder will hold all your .md template files. Unlike core templates, Templater scripts are triggered by a command palette command, a dedicated hotkey, or automatically through folder templates. This separation gives you precise control over when and how your automation runs.
The simplest demonstration is dynamic date insertion. In a Templater file, you wouldn't use a static tag. Instead, you use a JavaScript function call enclosed in Templater's special tags, which by default are <% and %>. For example, <% tp.date.now("YYYY-MM-DD") %> will insert the current date in your specified format the moment you create the note. This is your first step beyond static text into dynamic, context-sensitive note creation.
Core Scripting Functions: User Input and Note Manipulation
The true power of Templater is unlocked through its tp object, which provides a suite of utility functions for interacting with Obsidian and your content. Two of the most transformative are tp.system.prompt and tp.file.cursor.
The tp.system.prompt function allows your template to ask you questions. When you run the template, a dialog box will appear, and your input will be inserted into the note. This is perfect for creating structured notes that require unique information each time, such as a book review template that asks for the title and author.
<%*
const bookTitle = await tp.system.prompt("Book Title");
const author = await tp.system.prompt("Author");
%>
# Summary of "<%= bookTitle %>"
**Author:** <%= author %>
**Date Finished:** <% tp.date.now("YYYY-MM-DD") %>Notice the use of <%= to output the value of a variable. This script prompts for two pieces of information, then uses them to build the note's title and metadata automatically. The await keyword is necessary because the script must pause and wait for your response.
Equally important is cursor placement with tp.file.cursor(). When you insert <% tp.file.cursor() %> into your template, Templater will place the text cursor at that exact location after the note is generated. You can specify multiple cursor positions; pressing Tab will jump between them. This expertly guides your immediate focus to the fields that need manual input, streamlining the note creation process.
Automating Workflows with Folder Templates and User Scripts
Templater's automation extends to the moment a note is created through folder templates. In the plugin settings, you can define a specific template to be applied automatically to any new note created within a designated folder. This is ideal for standardizing note types like daily journals, meeting minutes, or project logs. Once configured, creating a note in that folder instantly formats it with your predefined structure, prompts, and dynamic data, requiring zero manual intervention.
Beyond the built-in tp functions, you can create reusable template-triggered scripts by writing your own JavaScript functions. These user scripts, stored in a separate folder you specify, can perform complex operations. For instance, you could write a function that fetches the weather, generates a unique ID, or performs a calculation based on other notes in your vault. You then call this function from within a template file.
<%*
const myFunctions = tp.user.myFunctions;
const weeklyGoal = myFunctions.getWeeklyGoal(tp);
%>
## Weekly Priority
This week's focus: **<%= weeklyGoal %>**This modular approach lets you build a library of functions that can be mixed and matched across different templates, promoting code reuse and more sophisticated automation.
Building Your Template Library for Standardized Notes
The ultimate goal is to build a template library that standardizes your note-taking processes across different note types. Each template should be designed to eliminate repetitive manual formatting and cognitive load. Start by identifying the repetitive structures in your vault: daily notes, literature notes, project status updates, or client meeting records.
For each type, design a template that balances automation with flexibility. A robust project note template might automatically populate a creation date, prompt for the project name and status, insert a standardized header structure with links to related notes, and place the cursor in the "Today's Actions" section. By applying these templates via hotkeys or folder templates, you ensure that every note of a given type starts with a consistent, useful structure, making your vault more organized and navigable.
Common Pitfalls
- Over-engineering Simple Notes: It's easy to get carried away and write complex scripts for notes that would benefit from simplicity. Correction: Start with the manual process, automate only the truly repetitive parts, and iterate. A template with just a date, title, and cursor placement is often more usable than one with five prompts.
- Misunderstanding Execution Context: Templater scripts run at the moment of note creation. They cannot react to later changes in the note or the vault unless triggered again. Correction: Use Templater for initial note generation and formatting. For ongoing note manipulation, consider plugins like Dataview or periodic manual reviews.
- Ignoring Error Handling in User Scripts: If a custom JavaScript function in your user script folder has an error, it can cause the entire template to fail silently or produce unexpected output. Correction: Test your user scripts thoroughly in isolation. Use simple
try...catchblocks orconsole.logstatements (viewable in Obsidian's Developer Console) to debug issues.
- Forgetting the
awaitKeyword: Functions liketp.system.promptandtp.system.suggesterare asynchronous. Calling them withoutawaitwill not pause for input and will instead insert aPromiseobject into your note. Correction: Always useawaitwhen calling these interactive functions, and ensure the enclosing script tag uses<%*(the asterisk denotes an asynchronous execution context).
Summary
- Templater replaces static templates with JavaScript-powered scripts, enabling dynamic note creation through functions like
tp.date.now()andtp.system.prompt(). - Key functions automate interaction: Use
tp.system.prompt()to gather input andtp.file.cursor()to guide focus, transforming templates into interactive forms. - Folder templates enable full automation by applying a specific template to any new note created within a designated folder, perfect for standardizing journals, meeting notes, or project logs.
- Build reusable user scripts for complex operations and call them from your templates, creating a modular and powerful automation library.
- A well-designed template library targets specific note types to eliminate manual formatting, enforce consistency, and drastically reduce the friction of capturing and structuring information in your vault.