setSelectedText creates non-visible selection?

If I run a script with code like this, to append text to the end of the draft:

editor.setSelectedRange(10000000, 0)

I think the app still considers “abc” as being selected? Even though it’s not highlighted on the screen.

After I run an action with the script above, then run another action that processes [[selection]], only the “abc” part of the draft is used (rather than the whole draft that I expected, seeing/thinking that there was no selection).

I’m seeing backticks around “abc”. Is that significant?

No, that’s just defining a template literal in JavaScript. It allows you to do string interpolation, multiline content, etc. over and above that of a basic string defined by double or single quotes. Here, it should just be treating it as a string as there’s nothing to differentiate it from a simple text string.

1 Like

seems like a reasonable question - from the naming of the APIs the text should still be selected.

The docs just state that the selected text will be replaced - did not try it by myself but seems reasonable that the text should still be selected afterwards.

I guess you’re just appending text to a Draft with that high number - so you could also use the draft.append API: Draft | Drafts Script Reference

1 Like

The editor always has a selection. It’s only visible when the editor is focused. When the editor is not focused it’s the last active selection.

You can show that by adding an editor.activate() at the end of that script to refocus the editor.

This isn’t obvious in day to day use on iOS, because you are typically focusing the editor by tapping on it—which sets a new selection based on the location of the touch.

1 Like

Thanks for the tip on editor.activate()

selection [is] only visible when the editor is focused

That’s something that’s bitten me a few times in the past, when depending on actions that process [[selection]]. For now my workaround is to turn the “Show selection count” indicator back on.

Thanks all.