Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[JS] How to remove character in text frame?

Contributor ,
Jun 01, 2016 Jun 01, 2016

Hi

I want to remove one character from insertionPoint.

I tried below code.  But It was not able to get the results I want.

Please advice for me.

Thanks.

//app.selection[0].remove();    // "app.selection[0]" is current insertionPoint.

//app.selection[0].characters[0].remove();

//app.selection[0].texts[0].remove();

TOPICS
Scripting
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Jun 02, 2016 Jun 02, 2016

It's a little trickier than you might have thought:

app.selection[0].parent.characters[app.selection[0].index - 1].remove()

In other words, first you find the parent story of the insertionPoint. Then you find the nth character of that story, n being the index of the selected insertionPoint. Then subtract 1, as you want to delete backwards, so you want the character before the insertionPoint.

Ariel

Translate
People's Champ ,
Jun 02, 2016 Jun 02, 2016

It's a little trickier than you might have thought:

app.selection[0].parent.characters[app.selection[0].index - 1].remove()

In other words, first you find the parent story of the insertionPoint. Then you find the nth character of that story, n being the index of the selected insertionPoint. Then subtract 1, as you want to delete backwards, so you want the character before the insertionPoint.

Ariel

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 02, 2016 Jun 02, 2016

Following should work for you. I have considered that you do not have linked text frames, i.e, you story is contained in one text frame only.

var selectionContext = app.selection[0];

if(selectionContext && selectionContext.constructor.name == "InsertionPoint") {  // Make sure it is insertion point only

var index = selectionContext.index;

if(selectionContext.parent.constructor.name == "Story") {

    var ch = selectionContext.parent.characters.item(index);  // Make sure we have a char to delete

    if(ch && ch.isValid)

            ch.remove();

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 02, 2016 Jun 02, 2016

Thanks for your answer

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2016 Jun 03, 2016

Hello vinothr,

the parent of the insertionPoint could be also: "[object Cell]" or "[object Footnote]".

Uwe

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 03, 2016 Jun 03, 2016
LATEST

Yes, agree, perhaps, depending on the context, parent of InsertionPoint can be TextPath, Text, TextFrame, Paragraph, Word, Cell, Note, XMLElement, HiddenText, etc.

Line 4 can be removed

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines