4 Replies Latest reply on Feb 2, 2012 3:41 AM by கற்பனை (Imagine)

    Clarification about Paragraph style from word?

    கற்பனை (Imagine) Level 3

      Hi All,

       

      I'm getting the range of word from the selected Paragraphs, I want to capturing that word applied paragraph style by using the below code, it display the undefined value in the console.

       

      $.writeln(app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).appliedParagraphStyle.name);
      

       

      If try the same approach on the single word.  it display the paragraph name.

       

      $.writeln(app.selection[0].paragraphs.firstItem().words.item(0).appliedParagraphStyle.name);
      

       

      I would like to know the differences between these syntax.

       

      Regards,

      Imagine.

        • 1. Re: Clarification about Paragraph style from word?
          [Jongware] Most Valuable Participant

          "itemByRange" works in a peculiar way. It returns a sort of 'temporary collection' of its items. This

           

          alert(app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).constructor.name);

           

          shows "Word" as its constructor, not "Array" -- but it is definitely not a single Word item (because otherwise using "appliedParagraphStyle.name" would have worked).

           

          Read Marc Autret's blog post On 'everyItem()' - Part 1 for a more accurate description

           

          In your example it's not necessary to get the paragraph style of 'more than one word'; you can use the paragraph itself to get it. But if you would use this construction to get the character style of a range of words, it won't work either. On the plus side, if you already know what range of words you are interrogating, you can simply use

           

          alert(app.selection[0].paragraphs.firstItem().words.item(2).appliedCharacterStyle.name);

          • 2. Re: Clarification about Paragraph style from word?
            [Jongware] Most Valuable Participant

            Here is an interesting thing (possibly Marc mentions this in his blog post -- have you read it yet?).

             

            Since itemByRange on a Word collection returns yet another Word collection, you can interrogate it usings its 'words' property again!

             

            This will work up to the last line, where it fails because our own temporary collection only contains 3 words:

             

            alert (app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).contents);
            app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).words[0].underline = true;
            app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).words[1].underline = true;
            app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).words[2].underline = true;
            app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).words[3].underline = true;
            
            • 3. Re: Clarification about Paragraph style from word?
              கற்பனை (Imagine) Level 3

              Thanks you so much for the great explanation.

              • 4. Re: Clarification about Paragraph style from word?
                கற்பனை (Imagine) Level 3

                Hi Jongware,

                 

                I'm unable to getting the pstyle from the range of words because it's a word collection.  If I'm selecting that range of words and getting the pstyle from that, got a proper pstyle name, because that selected words is a Text object.

                 

                The select() method converts the range of word into text object.

                 

                $.writeln(app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).appliedParagraphStyle.name); // Error

                app.selection[0].paragraphs.firstItem().words.itemByRange(2, 4).select();

                $.writeln(app.selection[0].appliedParagraphStyle.name); // working

                 

                I follow your suggestion like to get the pstyle name from the paragraph itself.

                 

                Thanks Jongware.