Have templater run an obsidian command that is in the command palette (like “New Tab” or “Toggle Pin”)

What I’m trying to do

I’m using the Image Inserter plugin and Templater. I’ll often find a note with no front matter and win a template to add my front matter. I’d like when the template runs, to run a command that is in the command palette - specifically Image Inserter: Insert Image in Frontmatter.

Things I have tried

Searched this forum and online in general. There’s some talk of using Advanced URI, but I can’t make sense of how that would work in this case, and it seems like there must be a simpler way.

1 Like

In your Templater template, run:

app.commands.executeCommandById('editor:toggle-fold')

and just use whichever command you need instead of toggle fold. You can find the list of commands by looking at the app.commands object in the console.

4 Likes

Thanks! It took me a minute, but this was helpful.

My frontmatter template with the Image Inserter now looks like this:

---
<% app.commands.executeCommandById('insert-unsplash-image:insert-in-frontmatter') %>
creation date: <% tp.file.creation_date() %>
tags: <% tp.file.creation_date("YYYY") %> <% tp.system.prompt("Tags?") %>
Aliases:
- <% tp.file.title %>
banner: "{image-url}"
---
<% tp.file.cursor(1) %>

And the result looks like this:

---
true
creation date: 2023-07-26 18:09
tags: 2023 tag1 tag2
Aliases:
- name of the file when template was run
banner: "https://images.unsplash.com/photo-1582034438152-77bc94ffa6ae?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wzNjAwOTd8MHwxfHNlYXJjaHwxNHx8aGlzdG9yeXxlbnwwfDB8fHwxNjkyMDI1MTQyfDA&ixlib=rb-4.0.3&q=80&w=1080"
---

The Image Inserter leaves that true at the top of the front matter, and although it doesn’t really make a difference, I do want to figure out how to prevent it or get rid of it eventually. If there’s some obvious thing, let me know but otherwise I will come back to this in the future…

I think you need to use <%* app... %> with the asterisk for your first command.

Yup! That got rid of the “true” result showing up. Thanks!

So for the record and all the folks in the future looking to solve this very thing, the final form is:

---
<%+ app.commands.executeCommandById('insert-unsplash-image:insert-in-frontmatter') %>
creation date: <% tp.file.creation_date() %>
tags: <% tp.file.creation_date("YYYY") %> <% tp.system.prompt("Tags?") %>
Aliases:
- <% tp.file.title %>
banner: "{image-url}"
---
<% tp.file.cursor(1) %>
1 Like

The + you have added would change it to a dynamic command, which I don’t think is what you want.

You would probably do better with <%* app.commands.executeCommandById('insert-unsplash-image:insert-in-frontmatter') -%>.

Notice the asterisk to make it an execution command (and not output anything), and the trailing - to remove the whitespace it leaves

2 Likes

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