bgmCoder
bgmCoder

Reputation: 6370

How to replace selected text in InDesign via script?

In my scripted UI panel, I have a button that is supposed to insert some text. I came up with this routine, which, indeed, inserts whatever text wherever I want, but if there is any text already selected, it doesn't replace the selection.

How can I modify this function to replace the selection? If there is nothing selected, it should just insert the text normally.

function insertText(whattext){
    if( app.selection.length < 1 ){ exit(); }
    var tf = app.selection;
    for( var q = 0; q < tf.length; q++ ){
        var thisframe = tf[q];
        var originaltext = thisframe.contents;
        thisframe.contents = originaltext + whattext;
    }
}

Upvotes: 0

Views: 3846

Answers (1)

bgmCoder
bgmCoder

Reputation: 6370

Hmmm... well, this seems to work pretty well... [embarassed look on face]

function insertText(whattext){ app.selection[0].contents = whattext; }

Upvotes: 1

Related Questions