Best way to clear draft during multi-step action?

I’m working on an action that will allow me to type a simple concept or idea into a draft and then have the action;

  • copy that draft to the [[clipboard]]
  • replace the entire content of the draft with inserted text having the [[clipboard]] somewhere in the middle of all the new text

I’m having a problem figuring out how to script the ‘clearing out’ of the draft before I ‘Insert Text’.

Can anyone point me to an existing action that uses this type of script component?

Thanks in advance

Not sure if I have got the right picture, but perhaps assign a new value to ‘draft.content’ (built as a sandwich of three layers) ?

try {

    (() => {
        'use strict';

			const strThought = draft.content;

			draft.content = [
				'Prefixed material,',
				strThought,
				'and appended material.'
			].join('\n\n');

			draft.update();
    })()
   
} catch (e) {
	alert(
		['line', 'column', 'message']
		.reduce((a, k) => a + k + ': ' + e[k] + '\n', '')
	)
}

This would be a little simpler, I think:

let template = `
Text to insert before
[[draft]]
Text to insert after
`;
draft.content = draft.processTemplate(template);
draft.update();

This is using Drafts template and tags to build a new text with the contents of the current draft ([[draft]]) in the middle.

3 Likes

( and in this case, of course, JS’s own text expansion is also an option )

draft.content =`
Text to insert before
${draft.content}
Text to insert after
`;

draft.update();

I went with your suggestion for using the template method and it works like a charm.

I’ve posted a generic version of what will soon become one of my most used actions.

Thanks