12 Replies Latest reply on Nov 9, 2008 4:14 AM by (Dave_Saunders)

    [JS, CS2] Find Text - Change Paragraph Style

      I have text supplied to me formated with <g>,< b>,<k> or <r> at the start of paragraphs, this represents different paragraph styles that need to be applied. <br /><br />I started a script below, but it comes up with an error at "app.findTextPreferences.findText" <br /><br />have a used the wrong function here? <br />Is there an easy way for it to run through all of them as well, or should I just repeat this script with < b>,<k> and <r><br /><br />var myDoc = app.activeDocument;<br />var greenBullet = ["<g>", "<g>"]<br />replaceGreen();<br /><br />function replaceGreen() {<br />for (i = 0; i < greenBullet.length; i++) {<br />            var myGreenArray = greenBullet[i];<br />            app.findTextPreferences.findText = myGreenArray[0];<br />            app.changeTextPreferences.changeTo = myGreenArray[1];<br />            app.changeTextPreferences.appliedCharacterStyle=myDoc.characteerStyles.item("OFI Body copy_GREEN BULLET");<br />            myDoc.changeText();<br />            }<br />}
        • 1. Re: [JS, CS2] Find Text - Change Paragraph Style
          Level 1
          You're using CS3 terminology in CS2. In CS2, those properties are app.findPreferences and app.changePreferences. And you use the search method, not changeText.

          Dave
          • 2. Re: [JS, CS2] Find Text - Change Paragraph Style
            Level 1
            Thanks Dave.

            OK... so admittedly I am an amateur with JS. Especially JS for InDesign.

            So, can anyone please head me in the right direction to achieve what I am trying do here please?


            On a side note - I have tried reading through the scripting reference guide, but it doesn't seem to help me - is there a good introduction guide to learn scripting for InDesign/.
            • 3. Re: [JS, CS2] Find Text - Change Paragraph Style
              Level 1
              Go here:

              http://jsid.blogspot.com

              type "search" in the Search box.

              Scroll down the page until you find the article called:

              Another Justification Job

              and look at the first code example in that article.

              Dave
              • 4. Re: [JS, CS2] Find Text - Change Paragraph Style
                Level 1
                Hrm... <br /><br />OK so... <br /><br />var myDoc = app.activeDocument;<br />app.findPreferences = null;<br />app.changePreferences = null; <br /><br />myDoc.search("<g>", false, false, undefined, undefined, {appliedParagraphStyle:"GREEN"});<br /><br />WORKS SWEET! Thanks! <br /><br />One last question, before the above runs, I want to have all the text boxes on a particular layer set to a certain style:<br /><br />How do I define "search for ANY text on Layer X"
                • 5. Re: [JS, CS2] Find Text - Change Paragraph Style
                  Level 1
                  You need to examine the itemLayer property of all the text frames in the document. Use the document's allPageItems property, cycle through them to find the text frames.

                  Dave
                  • 6. Re: [JS, CS2] Find Text - Change Paragraph Style
                    Level 1
                    sorry - you've lost me.

                    any chance you have another example?
                    • 7. Re: [JS, CS2] Find Text - Change Paragraph Style
                      Level 1
                      You need something like this. I think the applyObjectStyle call is right. It was added late in the CS2 cycle and didn't make it into the documentation:
                      myOstyle = myDoc.objectStyles.item("oStyle"); // use your style name
                      
                      myPIs = myDoc.allPageItems;
                      for (var j = myPIs.length - 1; j >= 0; j--) {
                        if (myPIs[j] instanceof TextFrame && myPIs[j].itemLayer.name = "X") {
                          myPIs[j].applyObjectStyle(myOstyle);
                        }
                      }
                      Be warned, I've created this here and haven't tested it. It also assumes that you're setting myDoc as you showed above.

                      Dave
                      • 8. Re: [JS, CS2] Find Text - Change Paragraph Style
                        Level 1
                        whats the advantage of using <br /><br />for (var j = myPIs.length - 1; J >= 0; j--)<br /><br />instead of <br /><br />for (j=0, j<myPIs.length; j++)
                        • 9. Re: [JS, CS2] Find Text - Change Paragraph Style
                          Level 1
                          Two reasons:

                          1. Counting down is always safe. Counting up will sometimes bite you in the rump. For example, if you're cycling through found text and changing the content, or if you're deleting objects. The as yet unprocessed references are broken by the actions and the script doesn't work.

                          2. I always try to use constructions that involve ">" rather than "<" because it's easier to post the script here.

                          Dave
                          • 10. Re: [JS, CS2] Find Text - Change Paragraph Style
                            Level 1
                            Thanks Dave!<br /><br />OK, Now I am getting this message when I run it:<br /><br />"A layer with this name already exists...<br />...     if (myPageItems[i]  instanceof TextFrame && myPageItems[i].itemLayer.name = "listings")"<br /><br />So it's trying to create a new layer called listings.<br />Also - it seems to want to move the items to that layer?<br /><br />var myDoc = app.activeDocument;<br />app.findPreferences = null;<br />app.changePreferences = null; <br /><br />myObjectStyle = myDoc.objectStyles.item("defaultText");<br />myPageItems = myDoc.allPageItems;<br />for (var i = myPageItems.length - 1; i >=0; i--) {<br />     if (myPageItems[i]  instanceof TextFrame && myPageItems[i].itemLayer.name = "listings") {<br />          myPageItems[i].applyObjectStyle(myObjectStyle);<br />     }<br />}<br /><br />myDoc.search("<g>", false, false, undefined, undefined, {appliedParagraphStyle:"OFI Body copy_GREEN BULLET"});<br />myDoc.search("<k>", false, false, undefined, undefined, {appliedParagraphStyle:"OFI Body copy_BLACK BULLET"});<br />myDoc.search("<r>", false, false, undefined, undefined, {appliedParagraphStyle:"OFI Body copy_RED BULLET"});<br />myDoc.search("< b>", false, false, undefined, undefined, {appliedParagraphStyle:"OFI Body copy_BLUE BULLET"});
                            • 11. Re: [JS, CS2] Find Text - Change Paragraph Style
                              Level 1
                              banevermind - fixed it.

                              if (myPageItems[i] instanceof TextFrame && myPageItems[i].itemLayer.name == "listings") {

                              HOWEVER... I should have mentioned this earlier, the boxes I am trying to apply the default paragraph style to (by way of the object style) are linked text boxes.

                              It won't apply the paragraph style to the boxes unless they are all changed at the SAME time (tested manually)

                              so While it's changing the object style of the text boxes, it's not applying the paragraph style to the text.

                              suggestions?
                              • 12. Re: [JS, CS2] Find Text - Change Paragraph Style
                                Level 1
                                You may have run into a limitation of the product there, but you can always apply a paragraph style to the text of a text frame:

                                myTextFrame.texts[0].appliedParagraphStyle = "Fred";

                                will get the job done.

                                Dave