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
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
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.
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
Copy link to clipboard
Copied