Append Text to End of First Line

Hi,

I’m still getting my head around scripting in Drafts and would appreciate a few pointers.

I’m trying to duplicate an existing draft and append some text to the end of the first line / title. I’ve been experimenting with editor.getSelectedLineRange and editor.setSelectedRange and have come up with the following but not sure if there’s a better way?

editor.setSelectedRange(0,0)
let st, len;
[st, len] = editor.getSelectedLineRange();
editor.setSelectedRange(len-1, 0)
editor.setSelectedText(" - foo")
editor.focus();

Could anyone tell me the ‘best’ way to duplicate a draft (complete with tags) and let me know if there’s a ‘better’ way to do the text insertion at the end of the first line, please?

You might start with the “New Draft with Template” example and workflow. It is designed to allow you to setup templates with tags, etc., and create new draft based on them - essential via duplication.

Not sure what your “- foo” is, but if it’s inserting a formatted date or similar, this workflow can handle that as well via template tags.

Thanks Greg, that’s prefect. I can definitely use that to guide me in the creation of my script. " - foo" is the date (in yyyy-DD format).

It would be great if I could use template script directly (although I’d learn less) but the draft I want to duplicate isn’t a static template but rather one which I add to during a month (via Shortcuts, so I need a static draft UUID). I then want to duplicate that draft as a backup with a date appended to the title and then empty all but the title from the original draft so I can start adding to it again in the new month.

Although, it’s just occurred to me that I could use the UUID of the main draft in place of the template selection section… I’ll carry on experimenting!

One (hopefully final) question: am I misunderstanding the [[selection]] template tag? I assumed that if I created a template draft with that template tag in the body, then selected some text in an existing draft and invoked the New Draft With Template action, it would replace the tag with the selected text, but instead it’s replaced with nothing?

Template tags are evaluated in the context of a specific draft. When you run and action from the app, the draft loaded in the editor is considered to be the current draft in context…but templates can also be evaluated against other drafts not loaded in the editor.

In the case of the “New Draft with Template” action, the script was written not to use the current draft in the editor, because in most people’s workflow that wouldn’t make a lot of sense. You are creating a new draft, and in typical use templates would likely only contain date-related template tags. The line that evaluated template tags in the template is this one, where the variable d is the new draft that’s been created and content is the content of the template:

d.content = d.processTemplate(content);

[[selection]] in this case would return the last selection in the template draft, which could be blank, or could not be depending on your last editing session with the template.

If you would prefer the action evaluate based on the draft in context for the action, so the [[selection]] would return the selection that was active in the editor, change that line to:

// not second `d` is now `draft`, which is the global pointer to the current draft
d.content = draft.processTemplate(content);

Be sure you note that other tags you might use which are specific to a draft would also return values from that edited draft as well.

Ah yes, that makes perfect sense (both why the code is looking at a selection within the template, rather than draft loaded in the editor, and why it makes sense for that to be the case), thank you for taking the time to point that out.
I’m tempted to tweak the action to switch between the two contexts, either manually as part of the prompt or automatically based on the template selected. I guess I could just swap to a [[clipboard]] tag in the template and then cut the text from the original draft but that feels like cheating, even if it does achieve both of my aims (emptying the original draft and inserting the text in the duplicate).
Thanks again for the guidance.