changing contents of TextStyleRange

We use javascript to search and replace text by iterating through all textstyleranges in a indesign document.
Usually this works fine, but for some documents we get a problem: once content is changed in a textStyleRange its ParagraphStyle is automatically applied to the next TextStyleRange (eg. maybe changing a normal line to a headline)

This only happens for textStyleranges that are stored weirdly "recursively" (the textStyleRanges collection in a textStyleRange contains the current textStyleRange and the next textStyleRange)
pindemad2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steven_bryantCommented:
The definition of textStyleRange is "A continuous range of identical text formatting attributes". This can change in middle of a paragraph. So changing paragraph style of range may affect next range paragraph style if in the same paragraph.
0
pindemad2Author Commented:
the only member I change in textStyleRange is contents - InDesign then changes the paragraphStyle of the next textStyleRange
0
steven_bryantCommented:
could you post a short sample of code that illustrates your point.
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

pindemad2Author Commented:
basically its:
...
var page=doc.pages.item(0);
var textframe=page.textFrames.item(0);
var textStyle=textFrame.textStyles.item(0);
var secondTextStyle=textFrame.textStyles.item(1);
textStyle.contents='some text';

at this point secondTextStyle paragraphStyle changes to the same as that of textStyle.

textStyle.textStyles has 2 members: the first is textStyle itself the second is secondTextStyle
0
steven_bryantCommented:
I imagine you mean

var doc = app.activeDocument;
var page=doc.pages.item(0);
var textFrame=page.textFrames.item(0);
var textStyle=textFrame.textStyleRanges.item(0);
var secondTextStyle=textFrame.textStyleRanges.item(1);
textStyle.contents='some text\r';

I'm assuming that each range is a complete paragraph including paragraph end character, making it necessary to add \r to text.

My test shows the opposite that the second paragraph gets paragraph style of first paragraph.



0
pindemad2Author Commented:
sorry, of cource you are right that I meant textStyleRange (not textStyle)

and the problem is exactly that the paragraphStyle of the second textStyleRange changes to that of the first when the contents of the first textStyleRange is changed.

I want to change the contents of the first textStyleRange - with no changes to any paragraphStyles
0
steven_bryantCommented:
You should exclude last character if it is a paragraph mark

var doc = app.activeDocument;
var page=doc.pages[0];
var textFrame=page.textFrames[0];
var textStyle=textFrame.textStyleRanges[0];
if(textStyle.characters[-1].contents == '\r')
{
      textStyle = textStyle.texts.itemByRange(textStyle.insertionPoints[0],textStyle.insertionPoints.previousItem(textStyle.insertionPoints[-1]));
}

textStyle.contents='some text';


It may also be necessary to remove last paragraph mark of incoming text  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pindemad2Author Commented:
Thank you!
I will give more attention to \r in InDesign scripting in the future :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Page Layout Software

From novice to tech pro — start learning today.