-
Hello, I am trying to make a suggester that prompts all the author in "Authors" folder and creates a link to the selected author. If the author doesn't exist, then you would be able to create one. I have figured out how to get a suggester that prompts all the authors in a folder, but I'm not sure how to add the "create author" option when there is no match. Is there a way I could create a file from a suggester prompt without breaking the flow of the template (eg. other prompts etc.)? <%*
const files = app.vault.getMarkdownFiles();
const authors = new Array();
files.forEach((file) => {
//console.log("file.path: " + `${file.path}`)
if (file.path.includes("221 Creators")) {
authors.push(file.basename)
};
})
-%>
[[<% tp.system.suggester(authors, authors) %>]] Any help would be appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
There is no direct way with templater's The Templater API on Theoretically, however, if you really really want to retain a one-prompt-only workflow, there is a hack to implement what you want. But I honestly think it is much easier to just add another prompt. If you want the hacky way, I can make a script on Sunday. (Busy until then). ~Welp |
Beta Was this translation helpful? Give feedback.
-
Ok, it didn't take too long. Also, I realized that even if I were to somehow get the value of the suggester prompt, there is no way I can pass it out. The only way the suggester closes is if you click an option or click outside and exit. The ladder, doesn't allow me to assign the value of what you typed out. I think an enhancement issue could be made to ask that the suggester return the value last typed in the suggester input field. But anyway, the following implementation works fine. Albeit, there's another prompt that asks you to enter the new author. But it's not too bad at all. script
<%*
const authorFolderName = "221 Creators"
var newAuthorTFolder
var newAuthorTemplateContent = ""; //template content for new Author //tp.file.find_tfile("template_name")
const files = this.app.vault.getAllLoadedFiles()
const authors = new Array();
files.forEach((file) => {
if (file.path.includes(authorFolderName) && file.children === undefined) {
authors.push(file.basename);
};
if (file.name == authorFolderName) {newAuthorTFolder=file;}; // need TFolder to create new file
})
if (authors.length == 0 ){ // authors.pushed nothing in forEach()
new Notice("Couldn't find Folder: " + authorFolderName);
} else {
authors.push("Create New Author") // Add last option in suggester
var author = await tp.system.suggester(authors, authors);
if (author == null) {
// suggester exited with no input
new Notice("Exited Script", 5000)
} else if (author == "Create New Author") {
// create author
if (title = await tp.system.prompt("New Author")) {
var newfile = await this.app.vault.create(newAuthorTFolder.path+ "/" + title + ".md", newAuthorTemplateContent)
tR += "[[" + title + "]]";
} else { new Notice("No Author Input", 5000)}
} else {
tR += "[[" + author + "]]";
}
}
%> Notes:
|
Beta Was this translation helpful? Give feedback.
Ok, it didn't take too long. Also, I realized that even if I were to somehow get the value of the suggester prompt, there is no way I can pass it out. The only way the suggester closes is if you click an option or click outside and exit. The ladder, doesn't allow me to assign the value of what you typed out.
I think an enhancement issue could be made to ask that the suggester return the value last typed in the suggester input field.
But anyway, the following implementation works fine. Albeit, there's another prompt that asks you to enter the new author. But it's not too bad at all.
Demo
script