Replace selected text

update
#1

What is wrong with this script? I am sure it is a total newbie mistake

var $content, cb;

$content = draft.content;

cb = app.getClipboard();

SetSelectedText(cb);

draft.update();

#2

Try this:

draft.setSelectedText(app.getClipboard());
draft.update();

So, there’s a few things “wrong” with your code. First, you don’t need to declare variables before assigning them content, and variables don’t need to (and arguably shouldn’t) start with a dollar sign in JavaScript.

The key error though is you are trying to do setSelectedText without using the Draft it’s attached to.

Hopefully my code works - it is untested though!

#3

Just a couple of extra bits.

These days, JavaScript variables prefixed with dollar symbols tend to, purely by convention, be associated with variables available from libraries/frameworks, rather than mainline code.

If the functionality of this script is feature complete, then it can also be achieved with an arguably simpler action step.

1 Like
#4

I get an error message that says draft.setSelectedTexti. Is not a function. Maybe I need to define a function first. Believe it or not I am an ABAP programmer but am just dipping my finger into JavaScript.

#5

I changed it to editor.setSelectedText(…

And it worked.

Thanks for pointing me in the right direction. (After your reply another post made some more sense to me).

#6

Thank you sylumer.! That works too. It will replace a selected text.

#7

Whoops, sorry, this is what happens when you code without testing while half asleep!