Shortcut Learning

#1

Here is a recent learning. Please jump in and share observations corrections etc.

I wanted to pass both the title and the body tags to a Shortcut instead of passing the whole draft and parsing it there into title and body

This was my template

{“Title”:"[[title]]",“Body”:"[[body]]"}

I could not get it to be recognized as a dictionary object. I had already made a solution that used the same format to pass in the uuid and selection_only tags that worked fine.

It turns out that Shortcuts dictionaries (JSON) cannot accept line feeds. When I replace linefeeds (in Shortcuts) with angle brackets br it works fine.

1 Like
#2

Maybe I’m missing something, but it looks like whatever you’re replacing linefeeds with got turned into a linefeed when your post was rendered. Was it \n?

Also, is there any value to URL-encoding the body in the template and then decoding it in the Shortcut?

#3

Just doing this purely with templates is likely to run into some problems, because there are additional characters that might need encoding to create valid JSON - like if you had quotes in your draft.

I would recommend a script to make sure you get valid JSON. Like:

// create the values you want to send
let title = draft.processTemplate("[[title]]");
let body = draft.processTemplate("[[body]]");

// make a JS object holding those keys-values
let obj = {
    "title": title,
    "body": body
}

// create a template tag for use in later action steps in this action
// JSON.stringify ensures proper conversion to JSON
draft.setTemplateTag("json", JSON.stringify(obj)); 

// in later action steps, like a Run Shortcut step,
// use new [[json]] tag

Note this also has the advantage of supporting passing dates, numbers or nesting objects, etc. - not just strings.

#5

I had entered br in angle brackets as a substitute line feed. Agiletortoise has a really good solution.

#6

Excellent solution Thanks!!

#7

Tested and it is beautiful. I will generalize your script so that it passes “all” the tags for a draft. It will be my standard for interfacing with Shortcuts.

The story of my learning is to propose a just adequate muddled out solution for a problem and then let others improve it. The suggestions you made are not incremental they are quanta and not just in the result but in what I learned.

Thank you!

#8

Your proposed solution is excellent and I have extended it to include most of the template tags.

I have found the Shortcut step

Markdown to Rich Text

is very weak.

I want to pass rich text/html from Drafts in my JSON string.

I naively tried adding

let mdbody = draft.processTemplate("%%[[body]]%%");

And

“mdbody”: = mdbody

to the object definition with no success. Does Drafts have a JS markdown to html or rich text converter that is accessible from a script??

#9

I figured it out

Added

var mmd = MultiMarkdown.create();
var mdbody = mmd.render(body);

Before the object definition.

I am finding my way around the scripting reference.

#10

That should have worked to get HTML conversion from Markdown.

#11

Sometimes I don’t hold everything constant and just change one thing at a time. I will try again. Shortcuts and Evernote are two other variables here. Also I think my expectations are an issue.

Thanks for a quick response. I feel like I am being handheld. It is great!

#12

You are, of course, correct. I originally tried to use unconverted body (which is markdown with links and footnotes) in my shortcut with Make Rich Text from Markdown.

That could not resolve footnotes.

Then I did the conversion in Drafts with %%%% delimiters. I expected that to look like Markdown to Email in Drafts but it is actually HTML not rich text (I guess the conversion to rich text is handled by the mail program). In the meantime I added the explicit conversion but also changed the Shortcut step to make Rich Text from HTML. That worked.

I have now removed the explicit change and returned to the %%%% delimiters.

Tedious explanation but maybe someone will learn from my mistake.