A problem with using scripts in templater plugin

hey guys,

i have a script that i use with QuickAdd plugin that fetches metadata from the current note, but when i use it with in templater it gives me error. can someone with some javascript knowledge help me. I don’t know much about javascript and i just trying to make some automation functional.

here is the code:

(params) => {
	const activeFile =
params.app.workspace.getActiveFile();
	if (!activeFile) {
		new Notice("No active file");
		throw new Error("No active file");
	}
	const myvar01 = app.metadataCache.getFileCache(activeFile);
	const myOutput = myvar01.frontmatter.citshort
	if (!myOutput) {
	new Notice("ٍError");
	throw new Error("Error");
	}
	
	return myOutput;
}

and here is the error:

What is the templater command? The script isn’t finding params which should come from the command.

I don’t think it needs to though. You should be able to just do: app.workspace.getActiveFile()

1 Like

i have no idea what params do here, can you modify the above code to what you suggest? I don’t know who should I define it. this exact code work very well on Quickadd tho, so i am very confused because in both of these plugins it supposed to return a value when its called

Just change params.app.workspace.getActiveFile() to app.workspace.getActiveFile()

params is the parameter that the function accepts. Parameters are passed to a function in order to be used inside the function declaration.

function double(number) {
    return number + number
}
double(2) // returns 4

Here number is a parameter. It is added to itself and returns the result.

In the case of templater, you pass in arguments* when calling the command:

<% tp.user.double(2) %>

Because you aren’t passing in params it is undefined. Which is why you see an error: “can’t read app of undefined” when try to run params.app....

As to why this works in QuickAdd, my guess would be that QuickAdd is automatically passing the correct argument as params into the function.

Since app is globally scoped, you shouldn’t need to pass it in. Which is why app.metadataCache.getFileCache(activeFile) works later in the code execution.

For more info on functions see: Functions - JavaScript | MDN

* fun fact: when creating the function you define parameters, but when calling the function you pass in arguments. Many people use these words interchangeably and you can essentially think of them as defining the same thing (stuff used inside a function).

1 Like

Thanks, its very interesting. i know these inputs called arguments but i had no idea what params is.

i changed the code to this:

function test(){
	const activeFile = app.workspace.getActiveFile();
	if (!activeFile) {
		new Notice("No active file");
		throw new Error("No active file");
	}
	const myvar01 = app.metadataCache.getFileCache(activeFile);
	const myOutput = myvar01.frontmatter.bookchapter
	if (!myOutput) {
	new Notice("ٍError");
	throw new Error("Error");
	}
	
	return myOutput;
}

and i put it in test.js . when i execute <% tp.user.test() %> via templater it says default export is not a function. i suppose i should define export

yep, at the end of the file add module.exports = test

1 Like

thank you. it works now, it is going to save me so much time. javascript is so cool. i have to learn it when i find a bit of free time.

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