Skip to main content
ali u
Inspiring
February 10, 2017
Answered

inserting unicode characters around selected text

  • February 10, 2017
  • 1 reply
  • 1082 views

Hi, I select some text and use this two lines to add two characters before and after my selected text, it works but the selected text changes and includes one of the added characters. How can I add these tho characters and have the selection stay the same?

app.selection[0].insertionPoints[-1].contents = "\u0041";
app.selection[0].insertionPoints[0].contents = "\u0042";
This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Store some properties before changes and use them:

var

    mText = app.selection[0].texts[0],

    len = mText.length;

mText.insertionPoints[-1].contents = "\u0041";  

mText.insertionPoints[0].contents = "\u0042";

mText = app.selection[0].texts[0];

mText.characters.itemByRange(0,len-1).select();

Jarek

1 reply

Jump_Over
Jump_OverCorrect answer
Legend
February 10, 2017

Hi,

Store some properties before changes and use them:

var

    mText = app.selection[0].texts[0],

    len = mText.length;

mText.insertionPoints[-1].contents = "\u0041";  

mText.insertionPoints[0].contents = "\u0042";

mText = app.selection[0].texts[0];

mText.characters.itemByRange(0,len-1).select();

Jarek

ali u
ali uAuthor
Inspiring
February 11, 2017

Thank you!

Can I apply a certain character style to only these two added characters?

Jump_Over
Legend
February 11, 2017

Hi,

A property 'index' can be helpful:

var

    mText = app.selection[0].texts[0],

    mIndex = mText.index,

    len = mText.length,

    mStory = mText.parentStory,

    CSt1 = app.activeDocument.characterStyles.item("A"),

    CSt2 = app.activeDocument.characterStyles.item("B");

mText.insertionPoints[-1].contents = "\u0041";  

mText.insertionPoints[0].contents = "\u0042";

mText = app.selection[0].texts[0];

mStory.characters[mIndex].appliedCharacterStyle = CSt1;

mStory.characters[mIndex+len+1].appliedCharacterStyle = CSt2;

mText.characters.itemByRange(0,len-1).select();

Jarek