Getting the output of a Shortcuts into Drafts using scripting

I’m trying to call a Shortcut, get the result, find a string in the Draft, replace the string with the Shortcuts result.

I’m using the Run Shortcut step to call the Shortcut, then I want to use the Shortcut output in the script, but I’m not sure what the syntax is for calling the Shortcut result. Using the Template Tag double-brackets simply results in a verbatim replacement, of [[shortcut_result]] for $StartEndTime, while omitting the quotes results in an error.

The very simple script I’ve come up with is:

const find = ‘$StartEndTime'

const replaceWith = '[[shortcut_result]]’

draft.content = draft.content.replace(find, replaceWith);
draft.update();

I’m redoing this and other actions because the originals date back to the Workflows days, when using the clipboard was one of the only ways to pass information back and forth between apps. Under iOS 16, using those actions results in a request for permission to paste every. single. [explective deleted]. time, which basically breaks the “one tap and done” desired behavior. There are other places I want to be able to use Shortcuts output in scripting actions, so if there is a general way to do that knowing how to do it properly would be extremely helpful.

try changing the replaceWith variable to:

const replaceWith = draft.processTemplate("[[shortcut_result]]")

This tip also covers the flow to insert information from Shortcuts:

1 Like

And this one covers the convenience shortcut that can help you set these up without any additional coding:

1 Like

Thanks! This did the trick. So it looks like I’ve got to use the .processTemplate method to get Drafts to expand this template tag, and then I’ve got the value in a form that’s usable elsewhere?

I did hit that article first — which is good by the way, and helpful for other situations — but it didn’t cover how you can grab the output with scripting.

I could actually probably do what I was trying to do in Shortcuts by opening the Draft and doing the search-and-replace there, but I wanted to avoid that because:

  1. I want the original Shortcut to do one thing: grab the values from two different places and combine them in a particular format. I call that same Shortcut as a kind of subroutine in a couple of other related Shortcuts so I don’t have to duplicate the steps in each Shortcut.
  2. I wanted to figure out a bit more about scripting, since I have next to no knowledge other than copying and very lightly modifying other people’s scripts.
  3. I wanted to just mash a button and have something find the text anywhere, not have to worry about the selection/insertion point when I trigger it.

(Edited to add: a verb, 3 )