Templater plugin: What's the syntax for adding a parameter to a system command user function

Things I have tried

I combed through Templater’s documentation, I asked in Obsidian’s Discord server in plugin general and templater’s thread and got zero responses, and I googled extensively. I found no answer to what I feel is a really basic syntax question. Experimenting with random syntax turned out to be a waste of time as well.

What I’m trying to do

I’m looking for a way to pass on a parameter to a user function “vidname” calling a system command that looks like so:

youtube-dl https://www.youtube.com/watch?v=QgbLb6QCK88 --get-title

where the link should be replaced by whatever the user inputs in this part of the template:

<%* const link = await tp.system.prompt("Link") -%>

The intended usage of the user function within the template would look like this (inserted below the previous template snippet ofc):

---
title: <% await tp.user.vidname(link) %>
---

The final result is intended to be a template that allows me to create a Youtube video note that auto populates the front matter with title and other information about the video based on a Youtube video link pasted into the user prompt popup.

2 Likes

Be aware they are stored as environment variables, so you must adhere to what’s allowed for your particular OS.

Yes, I did read that documentation entry and it doesn’t help me. I’m confused. What on earth do I put here within the user function instead of question marks? I’m using Windows.

youtube-dl ??? --get-title

Yeah, same experience. I’m not sure why the discord server kept getting recommended. The clique culture is super obvious. And questions get ignored except those from well-known members.

Anyways if you just want to get the youtube title, check how the Auto Link Title plugin does it.

2 Likes

Yeah, for all that folks are gushing about the server my personal experience has been of being mostly ignored.

I appreciate the suggestion, but I want to be able to later on add more analogous user functions using youtube-dl commands that will get me also other video information. Essentially I’d like to automatically populate the frontmatter of the note with things like the channel name, video duration, video upload date, etc.

Plus apparently the way the plugin you mentioned gets the site title means that the Youtube video title I’d get would look like “(actual video title) - Youtube” which means extra headache of deleting that last part, and that’s on top of the time and effort spend on actually delving into the plugin’s code and trying to understand it… Not worth it imo.

Basic rule:
Don’t scold others - learn for yourself! Then share.

That said, here’s a quick one I made just to get you started:

  • If you want to use multiple arguments (which makes sense), you must use a Javascript object ({key1: value1, key2: value2, …}) as a parameter for your user function.
  • This way, you can also use just one script and give it parameters like your --get-title.
  • After getting all you want (from scripts, user functions, templater calculations), you can use a Javascript template string to elegantly fill in the variables using ${var} syntax, like I show here.
  • You’ll also see a little trick to make a clickable thumbnail image in Obsidian.

Example template code:

<%*
/*
    Rather inefficient video template example, calling the same script thrice.
    Still shows how it can be done. Uses bash, don’t know about Windows.
    
    Needs user function "ytdl":
    youtube-dl ${opt} "${url}"
    
    On Windows this might look like this, but I have no idea. RTFM.
    youtube-dl %opt% "%url%"
*/
const url = await tp.system.prompt("Video URL")
const title = await tp.user.ytdl( { url: url, opt: '--get-title' } )
const duration = await tp.user.ytdl( { url: url, opt: '--get-duration' } )
const thumbnail = await tp.user.ytdl( { url: url, opt: '--get-thumbnail' } )

tR += `---
title: "${title}"
duration: "${duration}"
thumbnail: "${thumbnail}"
source: "${url}"
---
 
# ${title}

[![Thumbnail](${thumbnail})](<${url}>)  
Click image to watch on YouTube (${duration}).
`
%>

I have no Windows, so you must find out how and if environment variables work on that platform, probably using %variable% instead of my bash ${variable}.

Enjoy the example and expand on it.

3 Likes

Wat? No one was scolding anyone. Mistranslation?

Sorry to hear that you didn’t get an answer on the Discord server. My suggestion would be to also try the GitHub discussions on the Templater repository.

I do think it’s important to adjust expectations! It’s a bit unfair to judge the whole community because you didn’t get an answer for a question of a specific plugin that is considered somewhat advanced. I’d highly suggest asking again if it got buried among other discussions and adding more details about what you’ve tried (a starter script might get more help than just Googling). That being said, feel free to @ me (or any mod) or send me a DM if there is inappropriate behaviour on the Discord server towards you or any user.

Please also keep in mind that not all users have the same level of familiarity with programming, Obsidian and Templater in particular, so both questions and answers come from people with a variety of background knowledge and experience. Don’t take it personally.

Here is a reminder of the guidelines. Please help us by flagging posts instead of engaging in discussions on differences of opinion.

6 Likes

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