Skip to main content
Inspiring
September 22, 2011
Answered

Applying paragraph style to a single paragraph affects all previous paragraphs

  • September 22, 2011
  • 2 replies
  • 1050 views

Hey,

I'm trying to create a script that would take an array of text objects that describe what the text and style of a particular line of text is. And in some lines to apply Character Styles as well.

The problem is that, while the script works when the loop is in the character styles "mode", it seems to apply the paragraph styles to all the previous lines of text when the aforementioned object asks it to apply only the paragraph style (no character styles).

The offending line is this:

if ( typeof line.text == 'string' ) {

    frame.contents += "\r" + line.text;

    frame.parentStory.paragraphs[-1].appliedCharacterStyle = doc.characterStyles[0];

    frame.parentStory.paragraphs[-1].applyParagraphStyle(style);

    continue;

}

I even copied the style-apllying lines from the Character-Styles-applying section of the script (the else part), but it still applies the style to all the previous lines of text. The script below is the full code I have I long with one of the objects that text data. If I comment out the "offending lines" mentioned above, the script applyes all the styles correctly. Otherwise, only the last line (the Product - Description style) is applied, while all the previous lines attain the 'Spacer' style.

Can anyone see what I'm doing wrong? (sorry for the giant piece of code, but I can't pin-point what's not relevant here).

[{

    'text': [

        {

            'style': 'Product - Title No Indent',

            'text': [

                'Test Product',

                {

                    'style': 'Superscript',

                    'text': '®'

                },

                ' Title'

            ]

        },

        { 'style': 'Spacer', 'text': ' x' },

        { 'style': 'Spacer', 'text': ' x' },

        { 'style': 'Spacer', 'text': ' x' },

        {

            'style': 'Product - Description',

            'text': ['Designed for monitoring steam sterilization processes.']

        },

    ]

}];

function add_text_to_frame (frame, product) {

    var i, l, j, k, line, style, line_styles, char_style, line_text, bit;

    for ( i = 0, l = product.text.length; i < l; i++ ) {

        line = product.text;

        style = doc.paragraphStyles.item( line.style );

        if ( typeof line.text == 'string' ) {

            frame.contents += "\r" + line.text;

            frame.parentStory.paragraphs[-1].appliedCharacterStyle = doc.characterStyles[0];

            frame.parentStory.paragraphs[-1].applyParagraphStyle(style);

            continue;

        }

        else {

            line_styles = new Array();

            line_text = '';

            for ( j = 0, k = line.text.length; j < k; j++ ) {

                bit = line.text;

                if ( typeof bit == 'string' ) {

                    line_text += bit;

                }

                else {

                    line_styles.push({

                        'start': line_text.length,

                        'end': line_text.length + bit.text.length,

                        'style': bit.style

                    });

                    line_text += bit.text;

                }

            }

            frame.contents += "\r" + line_text;

            frame.parentStory.paragraphs[-1].appliedCharacterStyle = doc.characterStyles[0];

            frame.parentStory.paragraphs[-1].applyParagraphStyle(style);

            for ( j = 0, k = line_styles.length; j < k; j++ ) {

                char_style = doc.characterStyles.item(line_styles.style);

                frame.parentStory.paragraphs[-1].characters.itemByRange(

                    line_styles.start,

                    line_styles.end

                ).applyCharacterStyle(char_style, true);

            }

        }

    }

}

This topic has been closed for replies.
Correct answer

This was strange. Can't really see what is going wrong here. But it seems to be solved by changing this line:

text_frame.contents += "\r" + product.text.text;

to this:

text_frame.parentStory.insertionPoints[-1].contents = "\r" + product.text.text;


This has the further advantage of targeting the story and therefor not breaking if the textframe overflows.

2 replies

Zoffix222Author
Inspiring
September 22, 2011

Here's the shortest code with the problem I could come up with.... You can run it if you create two different default paragraph styles: 'Product - Description' and 'Product - Title No Indent';

The problem is that the first line should have 'Product - Description' style, but it instead it gets 'Product - Title No Indent'.

If I comment out one of the paragraphs, everything works fine. What's wrong?

var doc = app.documents.add(),

    product, text_frame, i, l;

product = {

    'text': [

        {

            'style': 'Product - Description',

            'text': 'Designed for monitoring sterilization processes.'

        },

        {

            'style': 'Product - Title No Indent',

            'text': 'Designed for monitoring sterilization processes.'

        },

        {

            'style': 'Product - Description',

            'text': 'Designed for monitoring sterilization processes.'

        },

    ]

};

text_frame = doc.pages[0].textFrames.add();

text_frame.geometricBounds = [ '0pt', '0pt', '240pt', '300pt'];

for ( i = 0, l = product.text.length; i < l; i++ ) {

    text_frame.contents += "\r" + product.text.text;

    text_frame.parentStory.paragraphs[-1].appliedCharacterStyle = doc.characterStyles[0];

    text_frame.parentStory.paragraphs[-1].applyParagraphStyle(

        doc.paragraphStyles.item( product.text.style )

    );

}

Zoffix222Author
Inspiring
September 22, 2011

Well, I solved the problem... by adding all of the text to the frame first, and then applying the styles.

Makes me wonder whether I am adding the text wrong, or there's a bug in InDesign that resets all the paragraph styles to the style of the last paragraph in the frame upon text addition?

Correct answer
September 22, 2011

This was strange. Can't really see what is going wrong here. But it seems to be solved by changing this line:

text_frame.contents += "\r" + product.text.text;

to this:

text_frame.parentStory.insertionPoints[-1].contents = "\r" + product.text.text;


This has the further advantage of targeting the story and therefor not breaking if the textframe overflows.

Zoffix222Author
Inspiring
September 22, 2011

Playing around with the code, I see that the problem arrises not when the script is in a particular part of that if statement, but when I have more than 2 paragraphs... Weird :S