Simple action to combine lines of text?

Hello—I am still a javascript novice, and would like to create an action that takes a multiple-line selection, combines the lines, and replaces the selection. I currently do this by round-tripping a selection to a Shortcut that uses the Combine Text action, and returns to Drafts. But Shortcuts is so buggy and unreliable that it freezes about a quarter of the time. I’d love to have this line-combining happen in Drafts itself. I’ve looked through @sylumer indispensable TADpoLe library, but can’t find anything that does this line concatenation.

Thanks in advance for the help!

It sounds like you want to remove newline characters in the selection. If that’s the case, this line of JavaScript should suffice.

editor.setSelectedText(editor.getSelectedText().replaceAll("\n",""));
1 Like

That works brilliantly! Thanks @sylumer!

1 Like

Just a quick follow-up that, in certain cases (like when I have copied and pasted lines from a pdf), the \n wouldn’t work so I added a second script to the action:

editor.setSelectedText(editor.getSelectedText().replaceAll("\r"," "));