[SOLVED] How can I get the current filename in quickadd module?

For those interested, I used this user script:

async function fetchFrontmatter() {
const activeFile = app.workspace.getActiveFile();
if (!activeFile) {
    new Notice("No active file.");
    return;
}

const fileContent = await app.vault.read(activeFile);
const frontmatterRegex = /^---\n([\s\S]*?)\n---/;
const match = frontmatterRegex.exec(fileContent);

if (!match) {
    new Notice("No frontmatter found in the active file.");
    return;
}

const frontmatter = match[1];
const frontmatterLines = frontmatter.split("\n");

const frontmatterData = {};
frontmatterLines.forEach(line => {
    const [key, value] = line.split(":").map(part => part.trim());
    frontmatterData[key] = value;
});

return frontmatterData;
}

// Main function to be called by QuickAdd
 module.exports = async (params) => {
const frontmatterData = await fetchFrontmatter();
if (!frontmatterData) {
    return;
 }

// Pass the frontmatter values as QuickAdd variables
params.variables = {
    ...params.variables,
    author: frontmatterData.author || '',
    title: frontmatterData.title || ''
    // Add other frontmatter fields as needed
};

and make a property that contains the file title with <%tp.file.title%> and pass the name of the property in the script as the title in the script, then load the script in a quickadd macro and use the name of the property that contains the filename like title as a quickadd variable {{value:title}} and the script will replace the variable with the filename, In any case, thanks to @josephtribulat for this script, of course, there are other uses for this wonderful script.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.