I am using the Templater plugin for Obsidian. I am clicking a link to a Note that does not exist yet in Obsidian. I have Obsidian configured to always create new notes in a specific folder. Is there a way for Templater to know from which note the link was clicked which created the new note?
Assistant
Unfortunately, Templater doesn't have a built-in way to know directly from which note a link was clicked to create a new note.
However, there are a few workarounds you can use to achieve this functionality:
1. Using Templater's [[...]] syntax:
Create a Templater template: You can create a template that automatically inserts the name of the current note into a specific field when a new note is created. For example, you could use the following template:
code
User
Is there a way to find from app.workspace what the last viewed file was? Or through some other Obsidian app API?
Assistant
You are right! Obsidian's API doesn't directly provide a way to track the last viewed file. There's no built-in property in app.workspace or other APIs that specifically stores this information.
However, here are a few approaches you could consider to achieve a similar outcome:
Using a custom plugin: You can develop your own Obsidian plugin that tracks the last viewed file manually. This would involve:
Observing file changes: Use Obsidian's app.workspace.on('file-open', ...) event to monitor when files are opened in the workspace.
User
Write a script to use in Templater with the following syntax: getPathDomain(sourceFilePath, domainFolderPath), where:
sourceFilePath = "1 - Domains/8 - NEL-C/0 - Terms, Concepts & Ideas/Terms/Untitled item.md"
domainFolderPath = "1 - Domains"
and getPathDomain returns: "8 - NEL-C".
Assistant
javascript
User
In JS, how do you check if a value is Null?
Assistant
In JavaScript, you can check if a value is null using the strict equality operator (===). Here's how:
javascript
if (value === null) {
// Value is null
} else {
// Value is not null
}
Explanation:
=== (Strict Equality): This operator checks for both value and type equality. It only returns true if the two operands are both null.
Why not use == (Loose Equality)?
While == can also sometimes work for checking null, it's generally not recommended due to its loose comparison behavior. It can lead to unexpected results when comparing with other values like 0, "", or undefined.
Example:
javascript
User
How do I get the first letter of every word in a string, and return it as an uppercase acronym in JS, such as: "this is It" = "TII"?
Assistant
javascript
function getAcronym(str) {
const words = str.split(' '); // Split the string into words
let acronym = '';
for (const word of words) {
acronym += word.charAt(0).toUpperCase(); // Get the first letter and uppercase it
}
return acronym;
}
const inputString = "this is It";
const acronym = getAcronym(inputString);
console.log(acronym); // Output: "TII"
Explanation:
getAcronym(str): This function takes a string (str) as input.
str.split(' '): Splits the input string into an array of words using spaces as delimiters.
for (const word of words): Iterates through each word in the array.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Use the template when creating new notes: When you click on a link to a non-existent note, Templater will automatically open the template and insert the current note's name into the "Created from" field.
2. Using JavaScript and Templater's file variable:
Create a Templater script: You can write a JavaScript script that accesses the file variable (which contains information about the current note) and inserts it into the new note. For example:
Call the script from Templater: You can call this script from a Templater command, ensuring it runs when a new note is created.
3. Using a plugin like "Note Refresher":
Install Note Refresher: This plugin automatically updates the content of notes when linked files are modified. It can be used to automatically update the "Created from" field in your new notes.
4. Using a custom plugin:
Develop your own plugin: You can create a custom Obsidian plugin that specifically handles this functionality. This would give you the most control over the process, allowing you to customize the behavior as needed.
Remember: These workarounds require some setup and configuration. Choose the method that best suits your workflow and preferences.
Storing the file information: Create a variable within your plugin to store the most recently opened file's path.
Accessing the stored information: You can then access this stored file information whenever needed, such as when creating a new note.
Using a third-party plugin: Some third-party plugins might already have features related to tracking file activity or history. You can explore the Obsidian community plugins for those that might provide the desired functionality.
Using a workaround with the app.workspace.getActiveFile(): While not ideal, you could potentially use the app.workspace.getActiveFile() function to get the currently active file, which might be the last viewed file in many cases. However, this approach could be unreliable if the user opens multiple files rapidly or switches between tabs frequently.
Using a browser extension: If you're comfortable with browser extensions, you could create one that interacts with Obsidian's webview to track file changes. This would allow you to monitor the file activity directly within the browser.
Ultimately, the best approach depends on your specific needs and how important it is to have a precise record of the last viewed file. If you need a reliable and accurate solution, creating a custom plugin is likely the most suitable option.
const getPathDomain = (sourceFilePath, domainFolderPath) => {
// Split the source file path into an array of segments.
const sourcePathSegments = sourceFilePath.split('/');
// Find the index of the domain folder in the source file path.
const domainIndex = sourcePathSegments.indexOf(domainFolderPath);
// Ensure the domain folder is present in the source file path.
if (domainIndex === -1) {
return null; // Or throw an error if you prefer
}
// Extract the domain folder name from the source file path.
const domainName = sourcePathSegments[domainIndex + 1];
return domainName;
};
Explanation:
getPathDomain(sourceFilePath, domainFolderPath): This function takes two arguments:
sourceFilePath: The full path to the source file.
domainFolderPath: The path to the domain folder.
sourceFilePath.split('/'): Splits the sourceFilePath into an array of segments, separated by forward slashes (/).
sourcePathSegments.indexOf(domainFolderPath): Finds the index of the domainFolderPath within the sourcePathSegments array. If the domain folder is not found, it returns -1.
if (domainIndex === -1): Checks if the domain folder was found. If not, it returns null (or throws an error if you prefer).
sourcePathSegments[domainIndex + 1]: Extracts the domain folder name from the array. Since the index of the domain folder is one position before the actual folder name, we add + 1.
return domainName: Returns the extracted domain folder name.
How to use it in Templater:
Create a Templater template: In your Templater settings, add a new template with the following code:
Use the template: When you click a link to a new note, Templater will automatically open the template. It will insert the note title and the domain name extracted from the Current File path using the getPathDomain function.
Example:
If the sourceFilePath is "1 - Domains/8 - NEL-C/0 - Terms, Concepts & Ideas/Terms/Untitled item.md" and domainFolderPath is "1 - Domains", the function will return "8 - NEL-C" as the domain name.
This code will successfully extract the first letters of each word in the input string and return the uppercase acronym "TII".
“I am using the Templater plugin for Obsidian. I am clicking a link to a Note that does….” Conversation with gemini-1.5-flash-api-0514, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850pt5zca6eereka5gcc0r1