Drafts ios :syntax to insert text at the cursor?

Drafts iOS: What would be the syntax to insert text (in this case a simple arrow → ) at the cursor?
Thanks very much for your time and help

Firstly, the easy way to do that is with the Insert Text action step, which does exactly that - insert text at the cursor.

If you want/need to script it because of some other operations you are scripting, you can do…

editor.setSelectedText("→");
1 Like

Thank you very much both for the answer and for your efforts and expense in favor of st Jude

The insert text action step works perfectly.

Just to let you know that the script approach
editor.setSelectedText(“→”);
inserts the text and in addition selects it which considerably shows down workflow.

Thank you again

I’m pretty sure Greg know’s that’s what happens :wink:

Look at the name of the function ‘set selected text’. It’s working with the text selection, replacing that selection, and maintaining that selection. It is doing exactly as you asked, and exactly as you requested. It is just that this is doing it at a lower level and from a more literal point of view in some respects.

But the need to insert and position the cursor within scripting is not an uncommon requirement. When I need to incorporate insertions in a script, I use something very similar to the script below. As well as the insertion it also takes into account the subsequent selection and jumps the cursor to the end and zeros the selection length. I have variations on this that jump to the start of the selection, the end of the word, the line, the start, … etc.

function insertTextPosAtEnd(p_strText)
{
	editor.setSelectedText(p_strText);
	editor.setSelectedRange(editor.getSelectedRange()[0] + editor.getSelectedRange()[1], 0);
	editor.activate();
}

It works by carrying out the initial selection replacement, then taking the current cursor position, adding the length of the selection, setting that total as the new selection start and setting the length to zero, that moves the cursor ahead to the end of the insertion.

That function can be called like this to insert the word ‘foo’:

insertTextPosAtEnd("foo");

Hope that makes sense.

1 Like

Thank you very much for the answer and explanation.

Yes I mindlessly copied the syntax and did not mentally click on selected text

Thank you also for the action which I will install and play around with

As usual your explanations are didactic and help me better understand drafts

Have a nice evening