How to define a JavaScript variable for the URL action

Hello Drafts community!

I happen to be a non-headings person. I just type something which could become a long sentence or even a paragraph. So first-line-as-title more often than not does not work for me.

In Drafts 4 I had a simple script (which in addition removed Markdown characters) that just worked fine for my purposes:

var maxLength = 50;
var firstLine = draft.content.split(’\n’)[0];
var trimmedTitle = firstLine.replace(/[#*]/gi,"").trim();
trimmedTitle = trimmedTitle.substr(0, maxLength);
draft.defineTag(“trimmedTitle”, trimmedTitle);

And in the URL action I set title=[[trimmedTitle]].

In Drafts 5 this does not work anymore. The last line of the script causes an error. I suspect the introduction of tags (hooray!) caused a change in the nomenclature. Or is there a better way of handing over variables to URL schemes?

A little PS: While I was just mentioning tags (hooray!)—why isn’t there a little tags bubble in the URL scheme?


You’re right, in Drafts 5 the equivalent function is:

setTemplateTag(tagName, value)

You can find more details here:


1 Like

Related note, just based on what your script is doing, there is also a new built-in template tag [[safe_title]] which does something similar for getting a filename safe version of the title. Docs

Thank you. Next time I should read the documentation more thoroughly.

So is setTemplateTag the recommended way for exporting script results for use elsewhere?