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?
 
steven_bryantConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
steven_bryantCommented:
could you post a short sample of code that illustrates your point.
0
 
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
 
pindemad2Author Commented:
Thank you!
I will give more attention to \r in InDesign scripting in the future :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.