How to streamline placeholder handling using Shortcuts' Dictionary action

If you have one or two placeholders in a note, and want to replace them with values using the Insert Text At Placeholder In Note action (or its Daily Note sibling), you’d use one action block per placeholder.

But what if you have 3 placeholders, or 10? Let’s consider this is note:

## My note

Today is %%date%%.

I feel %%mood%%.

The file path of this note is %%filepath%%.

Here, we’d have to use 3 separate Insert Text At Placeholder In Note actions, all with the same configuration.

There’s a better technique: Use Shortcuts’ built-in Dictionary action to declare our placeholder/replacement pairs, and a loop to iterate through them one at a time. (A “dictionary” refers to a data structure holding key/value pairs.)

Here’s a quick demo workflow which creates the note, sets up a list of key/value pairs (= placeholder/replacement pairs), then loops over that dictionary and inserts them one by one.

  1. Set up note body as described above.

  2. Create Note in my vault (its name is “Testbed”, change it your vault name), and use the note body as initial content.

  3. Set up a dictionary with a key/value pair for each replacement:

  • Key: %%date%%, value: the built-in “Current Date” variable.
  • Key: %%mood%%, value: “fine”
  • Key: %%filepath%%, value: the result of the “Create Note” action
  1. Repeat with Each, using the dictionary’s keys. For that, insert the dictionary variable (result of the previous action), click it, and pick the “Keys” property. Now inside this loop construct, in every iteration the variable “Repeat Item” will contain a single dictionary key.

    4.1. For readability, Set Variable “Placeholder” to the repeat item, i.e. the dictionary key.

    4.2. A Get Dictionary Value action reads the value for the current key out of the dictionary.

    4.3. For readability, Set Variable “Replacement” to the dictionary value from (4.2).

    4.4. And finally, the Insert Text At Placeholder In Note action replaces the placeholder in the note with the actual value.


That’s it! The Dictionary handling might feel a bit weird at first, but iterating over the list of keys (4) and then separately looking up the values (4.2) works well.

Also, you could easily get rid of the “Set Variable” blocks (4.1 & 4.3) if you want to boil it down even more — I’ve added them for readability only.

2 Likes

Thanks for another outstanding technique with sample shortcut. I hadn’t thought about how we might create a note with placeholders and then use a Dictionary to replace them in that note. This shows so many useful ways to combine shortcut actions. Actually, at first I didn’t understand how to get the File Path value in step 3; then I realized that I was supposed to insert the variable for the Note we just created, and select it’s File Path.

Also, I couldn’t find Another note, until I discovered that it was in my vault root directory; I was expecting that it would go in my default folder for new notes.

Then I wanted to do something like this for a section in my Daily Note, which had subheadings where I wanted to insert prompted text in the right place. So I included several Ask for Text for each prompt, and then set a variable for each from Provided Input. Since I wanted to use Append Text to Daily Note after headline, at first I found I could use the Replace Text action to replace the placeholders. However, then I realized I could eliminate the placeholder Dictionary and simply use my prompt variables where I previously had the comment placeholders. This simplified this shortcut even further, and worked nicely. This provides another way to create notes or append text using an embedded “template” in the shortcut; this might not even need a special Templater template in simple cases, or I could use this customized text together with Templater.

Doug, I like how you drill down and just refine, refine, refine! :100:

This provides another way to create notes or append text using an embedded “template” in the shortcut; this might not even need a special Templater template in simple cases, or I could use this customized text together with Templater.

Also, you could fetch a template using Get Note instead of setting up a note body in your Shortcuts workflow.

Thanks for pointing out how there are several ways to use “templates” with Actions for Obsidian.

Obviously for most situations, I would tend to use the Templater template scenario since that provides access to all my Obsidian templates from Shortcuts using Actions for Obsidian. This is extremely powerful. Although you provide specific actions for Daily Note, I can use the Note actions to generate Weekly, Monthly, Quarterly, and Yearly periodic notes using my templates for each of them; actually, I might even leverage the Templater option for Folder Templates that are automatically triggered when an empty note is created in specific folders.

I had been using my Daily Note template for any new empty file created in the Calendar/Daily folder; of course, this was before I started using Actions for Obsidian, which provides specific Daily Note actions, including with templates. However, there are no specific actions for Periodic notes (yet), but Templater Folder Templates could be used for corresponding periods just by have specific folder templates for each period like Weekly, etc.

I like the idea of using Shortcut Text action as a “template” sometimes, especially for content with simple notes or embedded text within a more complex note using a Templater template. You can embed Shortcut variables after receiving Shortcut input, Ask for Input prompts, List with Choose from List and Get Item from List, or Dictionary with Get Dictionary from Input and Get Dictionary Value. For example, when you run an embedded Shortcut as a “subroutine” in a workflow, you might use a Dictionary for its parameters and return values, with corresponding preparation before invocation and processing after completion (they’re just JSON objects with key-value pairs so that’s quite flexible).

Although, I understand that Get Note could be used to fetch a template, I’m not really sure how practical that would be, but now that I think about it a little more, perhaps I want to embed some text or Shortcut variables within a section of that template. Normally, Templater templates only have access to Templater, Dataview, DataviewJS, and JavaScript in the templates, so this provides a way to include Shortcut variables, objects, or generated text.

Yeah, the question of practicability is valid. You can certainly turn all of that stuff up to 11 but you’d definitely add a level of complexity that might be undesirable. Personally, for smaller tasks I use simple Shortcuts text blocks as “templates”, e.g. when jotting down quick notes via Shortcuts workflow, or when adding a task to my DN using a workflow. It’s more direct, less abstraction, which often enough translates into better maintainability.

(20-year old Carlo from the past would probably vehemently disagree but he’s not here right now.)