-
I made a script with the help of chatGPT to run the templater select template modal but modified to display the folders of the Utilities/Templates folder first, and allow the user to select the template once navigated to the base folder, and then the file is created, the user is asked for a title and the file is moved to it's default folder. Basically a script to automate creating a naming a file, maybe there was an easier way. The reason why I made a script for selecting a template is because I was able to add creating a naming the new file more seamlessly, whereas before I had to create a file, name it, open the templater modal then select the template. I have another script that I modified from a helpful user on my last discussion post, and it moves the file depending on some conditions (if it belongs to a project or course folder, it will be placed there, otherwise its default folder is kept. It allows me to have standalone lecture notes inside my Input Notes folder while having course-specific lecture notes kept in the course folder. Here is the desired workflow:
If you notice any problems, don't be shy!
<%*
const templatesFolder = "Utilities/Templates";
const ttemplatesFolder = app.vault.getAbstractFileByPath(templatesFolder);
// Perform checks to ensure it's a folder and has subfolders
if (!("children" in ttemplatesFolder)) {
throw new Error("Templates folder is not a folder.");
}
const templatesSubfolders = ttemplatesFolder.children.filter(subfolder => "children" in subfolder);
if (templatesSubfolders.length === 0) {
throw new Error("Templates folder doesn't have any subfolders.");
}
let templatesCurrentFolder = ttemplatesFolder;
let selectedTemplatesFolder = "";
// Prompt user to select subfolder or go back
while (true) {
const templatesSubfolders = templatesCurrentFolder.children.filter(subfolder => "children" in subfolder);
const templatesFolderOptions = templatesSubfolders.map(subfolder => subfolder.name);
// Check if it's the lowest subfolder
if (templatesSubfolders.length === 0) {
break;
}
const templatesOptions = ["Go Back", ...templatesFolderOptions];
// Prompt user to select subfolder or go back
const templatesSelectedOption = await tp.system.suggester(option => option, templatesOptions);
if (templatesSelectedOption === "Go Back") {
if (templatesCurrentFolder.parent) {
templatesCurrentFolder = templatesCurrentFolder.parent;
selectedTemplatesFolder = templatesCurrentFolder.path;
}
} else {
const selectedSubfolder = templatesSubfolders.find(subfolder => subfolder.name === templatesSelectedOption);
if (selectedSubfolder) {
templatesCurrentFolder = selectedSubfolder;
selectedTemplatesFolder = templatesCurrentFolder.path;
break; // Break out of the loop once a subfolder is selected
}
}
}
// Get the selected template folder
const selectedTemplateFolder = app.vault.getAbstractFileByPath(selectedTemplatesFolder);
// Get the template files from the selected template folder
const templateFiles = selectedTemplateFolder.children.filter(file => !("children" in file));
const templateOptions = templateFiles.map(file => file.name);
// Prompt user to select template file
const selectedTemplateFile = await tp.system.suggester(option => option, templateOptions, true, "Select a template file:");
// Get the selected template file
const selectedTemplate = await tp.file.find_tfile(selectedTemplateFile);
// Prompt the user to enter a new title for the note
const newNoteTitle = await tp.system.prompt("Note title:");
// Create a new note with the selected template content and the entered title
await tp.file.create_new(selectedTemplate, newNoteTitle, true);
-%>
<%*
// Prompt user if note belongs to a collection
const belongsToCollection = await tp.system.suggester(
["YES", "NO"],
["YES", "NO"],
false,
"Does this note belong to a collection (i.e., course/project-specific material)?"
);
if (belongsToCollection === "YES") {
// Get details about the "Collections" folder
const collectionsFolder = "Collections";
const tCollectionsFolder = app.vault.getAbstractFileByPath(collectionsFolder);
// Perform checks to ensure it's a folder and has subfolders
if (!("children" in tCollectionsFolder)) {
throw new Error("Collections folder is not a folder.");
}
const collectionSubfolders = tCollectionsFolder.children.filter(subfolder => "children" in subfolder);
if (collectionSubfolders.length === 0) {
throw new Error("Collections folder doesn't have any collections.");
}
let currentFolder = tCollectionsFolder;
let selectedFolder = "";
while (true) {
const subfolders = currentFolder.children.filter(subfolder => "children" in subfolder);
const folderOptions = subfolders.map(subfolder => subfolder.name);
// Check if it's the lowest subfolder
if (subfolders.length === 0) {
break;
}
const options = ["Go Back", folderOptions, "Current Folder"];
// Prompt user to select subfolder or go back
const selectedOption = await tp.system.suggester(option => option, options);
if (selectedOption === "Go Back") {
if (currentFolder.parent) {
currentFolder = currentFolder.parent;
}
continue;
}
selectedFolder = subfolders.find(subfolder => subfolder.name === selectedOption).path;
currentFolder = app.vault.getAbstractFileByPath(selectedFolder);
}
// Move the note to the selected subfolder or current folder
const targetFolder = selectedFolder || currentFolder.path;
await tp.file.move(`${targetFolder}/${tp.file.title}`);
}
-%> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You can run another template from within your template using <% tp.file.include("[[Template1]]") %> |
Beta Was this translation helpful? Give feedback.
You can have
tp.file.include
work in an execution command like this