I am using the Templater plugin for Obsidian. I am clicking a link to a Note that does…
- User
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 itself doesn't have a built-in mechanism to track the originating note when creating a new note from a link.
Here are some workarounds you could consider:
-
Custom Templater Script:
- You could create a custom Templater script that captures the current note's path before creating the new note.
- This script could then store the originating note's path as metadata within the newly created note.
-
Dataview Queries:
- After creating the new note, you could use Dataview queries to find all notes linking to it.
- This would allow you to identify the originating note(s) indirectly.
-
External Scripting:
- You could use external scripting languages like Python or JavaScript to interact with Obsidian's API.
-
- User
Is there a way to find from
app.workspacewhat the last viewed file was? Or through some other ObsidianappAPI? Assistant
You're on the right track! While
app.workspacedoesn't directly store the "last viewed file," you can achieve this using theapp.workspace.getLastOpenedFile()method.Here's how you can use it:
javascriptconst lastViewedFile = app.workspace.getLastOpenedFile(); if (lastViewedFile) { console.log("Last viewed file:", lastViewedFile.path); } else { console.log("No recently opened file found."); }Explanation:
app.workspace.getLastOpenedFile(): This method returns theFileobject representing the last file opened in the current workspace.lastViewedFile.path: Access thepathproperty of theFileobject to get the file's path.
Important Notes:
- This method retrieves the last file opened, not necessarily the last file viewed. If a file was opened but not actively viewed (e.g., opened in the background), it might not be considered the "last viewed" file.
- The behavior might vary slightly depending on Obsidian's version and configuration.
- 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" andgetPathDomainreturns: "8 - NEL-C". Assistant
javascriptfunction getPathDomain(sourceFilePath, domainFolderPath) { const pathParts = sourceFilePath.split('/'); const domainIndex = pathParts.indexOf(domainFolderPath) + 1; if (domainIndex > 0 && domainIndex < pathParts.length) { return pathParts[domainIndex]; } else { return null; // Or handle the case where the domain folder is not found } } // Example usage: const sourceFilePath = "1 - Domains/8 - NEL-C/0 - Terms, Concepts & Ideas/Terms/Untitled item.md"; const domainFolderPath = "1 - Domains"; const domain = getPathDomain(sourceFilePath, domainFolderPath); console.log(domain); // Output: "8 - NEL-C"- User
In JS, how do you check if a value is Null?
Assistant
In JavaScript, you use the strict equality operator (
===) to check if a value is exactlynull.Here's how:
javascriptlet myValue = null; if (myValue === null) { console.log("The value is null"); } else { console.log("The value is not null"); }Explanation:
===(Strict Equality): This operator checks for both value and type equality. It ensures that the value being compared is exactlynull.
Important Notes:
==(Loose Equality): Avoid using the loose equality operator (==) for null checks. It can lead to unexpected results because it performs type coercion, which might treat other falsy values (likeundefined,0, or an empty string) asnull.typeofOperator: While you can usetypeof myValue === 'object' && myValue === nullto check fornull, it's generally considered less readable and more verbose than the direct=== nullcomparison.
- 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
javascriptfunction getAcronym(str) { return str .split(' ') // Split the string into words .map(word => word[0].toUpperCase()) // Get the first letter of each word and uppercase it .join(''); // Join the letters together into a string } const sentence = "this is It"; const acronym = getAcronym(sentence); 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. -
.map(word => word[0].toUpperCase()):- Uses the
.map()method to iterate over each word in the array. - For each
word, it extracts the first character (word[0]) and converts it to uppercase using .
- Uses the
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.