Save each tag with # as prefix

I’m modifying the “Save to Files As…” with the intention to append tags at the end of the text before saving.

The current Export steps look like this:

I inserted #[[tags]]

This works fine if there’s only one tag in the drafts. The output is #work, for example.

If I have two tags, #work and #project, the output is #work,project

I’d like to have the output as #work #project - is there an easier way to do this or do I need some extensive scripting to parse through the the tags?

I would say some it is just scripting rather than “extensive” scripting as it is quite short and uses only common run of the mill, JavaScript and Drafts scripting constructs. The premise is you have an array of tag names (strings), so you iterate over them (no parsing required because parsing is breaking down into the relevant component parts, and we already have the tag names parsed into an array that meets that requirement), prefix each element with a hash tag to create a new array, and then join them together into a string. The resulting string you then assign as the value of a custom template tag, which can then be referenced in the export action step.

The line below line sets a custom template tag for the current draft to be a string of comma space separated entries, where the entries are the tags of the current draft, prefixed with a hash symbol.

draft.setTemplateTag("taglist", => "#" + strTag).join(", "));

You can then just reference it in the export action following a script action, like this:

In the action above I’ve set it to use the safe_title template tag for the file name, with a Markdown file extension.

Presumably you have come custom code to set up your use of the file template tag as that is not listed as a standard template tag. Which means you should just be able to add the line of code above to your own script action step.

Hope that helps.

1 Like

Yes, this helps. Thank you very much. I have tried it with up to four tags and it exported fine. Your code above and the detailed guidance helped. I appreciate it.