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

InsertionPoint becomes invalid if there is a change on document

New Here ,
May 03, 2016 May 03, 2016

Copy link to clipboard

Copied

Hello,

I want to find certain words (e.g. specific character style applied ones) and append sequence numbers to them. e.g. I want "Burak drinks coffee" became "Burak [1] drinks coffee", "Ahmet plays football" to "Ahmet [2] plays football".

What I currently do is find all the words that matches the regex ( via pages.allPageItems.findGrep() ), order them accordingly to the page index and the location of the character groups (.horizontalOffset and .baseline). I've created a function (lets name it find here) that returns ordered list of what findGrep returns to me

var total = find();

/*
if(total[0].contents != "Burak" && total[1].contents != "Ahmet") {
alert("Something must be wrong here ");
}
*/

for(var i = 0; i < total.length; ++i) {

    var ipi = total.insertionPoints[-1];

    ipi.appliedCharacterStyle = /* bla bla bla */;

    ipi.contents = '[' + (i + 1) + ']';

}

After first iteration Burak becames Burak [1] correctly, but since there is extra 4 character (including the space), total[1] becomes something different (e.g. in the second operation Ahmet becames A [2]hmet)

I think it happens because total[1] just points somewhere in the document and It points another one when I add things to the document.

However, if I call the "expensive" find() function every iteration, it just works.

var total = find();

for(var i = 0; i < total.length; ++i) {

    var ipi = (find()).insertionPoints[-1];

    ipi.appliedCharacterStyle = /* bla bla bla */;

    ipi.contents = '[' + (i + 1) + ']';

}

But this way I restart the search for every word. I find the all matches (takes a lot of time), give number to first one, find the matches again (takes a lot of time), give number to second, ... etc.

I am wondering is there any way to keep what total[1] points after modifying total[0]. I got all of the words in first place already

TOPICS
Scripting

Views

393

Translate

Translate

Report

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
Advisor ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

Indeed adding/removing text will change the index of the following insertion points.
There are 3 ways to work around it.

The first one you already found, that is to search again.

A better approach is to actually keep track of the changes you made: if you add 4 characters after insertionPoint 40, all subsequent IP's will shift by 4. It is a bit more complicated implement, but in the end it should work faster than doing another search.

The simplest and best way to go about this is to change your text starting from back to front. This way you don't care that the IP's drift. However, this means you cannot first sort the found results according to some arbitrary criteria.

Votes

Translate

Translate

Report

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
New Here ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

Thanks for the response,

Looking forwards for the third way. I could sort the array twice, iterating second one and call indexOf on first array to give a number.

However, how can I detect which InsertionPoint I should start first or update it's index? There are TextAreas connected to Stories, and independent TextAreas in pages. I have to order them in a specific way, where the formula is unknown, to make sure iterating the loop wont cause that kind of trouble

Votes

Translate

Translate

Report

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
People's Champ ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

LATEST

Go backwards:

Array of Text findGrep ([reverseOrder: bool])
Finds text that matches the find what value.

ParameterTypeDescription
reverseOrder

so findGrep(true);

OR

var n = found.length;

while ( n-- ) {

     found.contents = "bla";

}

That way indeces of trailing results shouldn't be affected.

Loic

Ozalto | Productivity Oriented - Loïc Aigon

Votes

Translate

Translate

Report

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