Replies: 3 comments 3 replies
-
Here's a snippet for opening a file in the current tab. app.workspace.getLeaf().openFile(tp.file.find_tfile("Workbox")); One additional consideration. Templater has already created the file by the time your code is run, so you may want to consider deleting the newly created file if you don't want to apply a template. await app.vault.delete(tp.file.find_tfile(tp.file.path(true))); |
Beta Was this translation helpful? Give feedback.
-
@MichaVDM I'm not 100% sure what it is that you're trying to do but I stumbled onto this thread because I had the same core question: how do switch to a file if it exists instead of trying to create it? I agree that there's not a ton of intermediate documentation w/r/t Templater and using it with Obsidian so it can be tricky to figure out how to do some things beyond what's in the introduction / guide docs. I think the answer to your question is "how do I prevent the creation of the new I have a generic <%*
// Omitted here: a bunch of JS code that builds up a list of `folders` from my vault.
// Basically I have hundreds of folders but am only interested in a sub-set of them so I have to list all the folders in my vault and then
// apply basic filter logic to eliminate some...
// Ask the user for the project or folder
const folder = await tp.system.suggester(folders, folders);
// Check if the user selected nothing (null)
if (folder === null) {
// Stop execution if no selection is made; this prevents the Untitled note from being created
return;
}
// omitted here: more code that's specific to my vault; basically it does some additional processing based on the user's
// selection to populate some variables that will be used in the note body.
// E.G: if the user selected X or Y from the list, put `kind/X` in the list of tags that will later make it into the front matter.
// Use Obsidian's API to check if the file already exists
const existingFile = tp.file.find_tfile(filePath);
if (existingFile) {
console.debug(`file exists already. existingFile: ${existingFile}`)
app.workspace.getLeaf().openFile(existingFile);
// Stop execution NOW to prevent a file from being created regardless.
return;
} else {
// If the file doesn't exist, proceed to create or move the file as originally intended
// Note: Since we're creating a new file, appending '.md' is necessary as Templater doesn't automatically add the extension
console.log(`file does not exist already. filePath: ${filePath}`)
await tp.file.move(filePath);
}
%>
// The rest of my note template here
For anybody else that stumbles on this thread, there's a few things I want to point out:
E.g.: Here is the code that chatGPT suggested I use to check if a note exists or not: // Check if the file already exists
this.app.vault.getFiles().forEach(file => {
if (file.path === `${filePath}.md`) {
existingFilePath = file.path;
}
}); While this probably works, told chatGPT this:
And it incorporated that into something that was more or less correct and did what I needed it to. Hope that helps! |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot to @kquinsland and @Zachatoo for your precious help! I think I can manage again from here. I don't know precisely how the GitHub "conversation" works, so this is not an answer in form of a solution, but the info shared by both can definitely be of some help to others! |
Beta Was this translation helpful? Give feedback.
-
Hello,
I'm trying to use a new prompt template as below. Everything is quite simple, a prompt appears and lets me select which template I want to use. What I'm trying to achieve, is when I don't want to use a specific template at the time of a new file creation, to just open an existing "Workbox.md" file at the root of my vault (the XXX after the else statement in the code):
Could anyone help, please? Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions