4 Replies Latest reply on Aug 19, 2016 4:49 AM by cweeks

    Change from one Paragraph Style to another?

    cweeks Level 1

      I've done a lot of searching on this and I can only find more complex versions of what I'm trying to do.

       

      I simply need to find all text in the document with an applied paragraph style and change it to another existing paragraph style. I've done trial and error a bunch of different ways and I'm not even coming close. Seems like it should be quite easy.

       

      Please keep in mind that the first paragraph style cannot be deleted from the document, it needs to stay in my template.

       

      Any suggestions? It'd be a huge help.

       

      Thanks!

        • 1. Re: Change from one Paragraph Style to another?
          TᴀW Adobe Community Professional & MVP

          app.findTextPreferences = app.changeTextPreferences = null;

          pStyles = app.activeDocument.paragraphStyles;

          pStyle1 = pStyles.itemByName("FirstPara");

          pStyle2 = pStyles.itemByName("SecondPara");

          app.findTextPreferences.appliedParagraphStyle = pStyle1;

          app.changeTextPreferences.appliedParagraphStyle = pStyle2;

          app.activeDocument.changeText();

           

          ... it would be a little more complicated (not much) if the paragraph styles in question were in a group.

           

          HTH,

          Ariel

          • 2. Re: Change from one Paragraph Style to another?
            Kai Rübsamen Level 4

            Why is there a need for a script?

            • 3. Re: Change from one Paragraph Style to another?
              cweeks Level 1

              The explanation is quite long, so bear with me.

               

              The context

              I manage a team at a data news & analytics company. We produce long daily newsletters that require fast turnaround, so the process is largely automated. We have an add-on called Xtags from Em Software. Xtags pairs different elements of the articles in the newsletters with names that match up with Paragraph Styles in InDesign. So we generate the xtag, load it into the ID file (text flows in) and everything is auto-formatted.

               

              The problem

              Unfortunately this process was in place before I started my position here more than 3 years ago, and it's not to my standard. For text in the newsletter that requires linking (generally reference or company links listed at the end of the article), instead of having the links embedded, it pulls in as two separate lines (both tagged with different para styles). First is the URL. This text is set to white with a negative baseline shift. Second is the descriptive text. This text is set to your typical hyperlink blue.

               

              The potential solution

              Obviously this is a pretty ridiculous way of doing things. It causes problems too. The links don't line up exactly right. If URLs are too long, the line will break and the hyperlink won't function. So I started doing research 2 years ago on whether or not the Xtags add-on had a coding capability to embed the links. Turns out they did! Good news, right? Well, that portion needed to be implemented by a different team in the company that handles such back-end things. I brought it to their attention immediately. TWO YEARS AGO. I've been waiting and checking back every 6 months. That team is so overwhelmed. And it's hard to explain why this is so valuable to those who don't have layout production and InDesign knowledge.

               

              A workaround (the script!)

              So I decided to take matters into my own hands and write an InDesign script that will embed the links. Keep in mind I went into this with 0% scripting knowledge. I read a lot, watched a lot of Lynda.com videos, and prepared myself as best I could so I wouldn't be completely lost.

               

              I am very close to finishing this script. Most of the link types are successfully embedding. I only have one link type left. One type is the company links at the end. There are two separate blue-text paragraphs styles matching up to only one white-url paragraph style. The reason is because some of the company links have:

              Company Name > tab > Ticker.

              While others have:

              Company Name > tab > Ticker > tab > State.

               

              So when this was set up, they did two different styles that would handle either scenario. The JS code I used for the other link types don't work in this situation. I figured perhaps I could adjust the tabs on one of the paragraph styles to handle either scenario, and then in the script, right before embedding the links in this section, I could change the text in the second para style to the first para style.

               

              I suppose the better solution would be to figure how to have to consider EITHER paragraph style. I tried this first and couldn't get it to work right. See below. I would need line 13 to say either "CH_BLUE" or/and "CH_BLUE2".

               

              // find URL  
                  app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                  app.findGrepPreferences.findWhat = '(https?|www)\\S+\\>';
                  app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("hiddenline_ch");
              
              
              var mHiddenlineCH  = doc.findGrep();
              
              
              // find Text  
                  app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;     
                  app.findGrepPreferences.findWhat = ".+";   
                  app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("CH_BLUE");
                  app.findGrepPreferences.fillColor = 'Blue';
                  
              var mCHBlue  = doc.findGrep();  
                
              for (var ka = 0; ka <mCHBlue.length; ka++){  
                  var mHyperlinkDestination2 = doc.hyperlinkURLDestinations.add(mHiddenlineCH[ka].contents);  
                  var mHyperlinkTextSource2 = doc.hyperlinkTextSources.add(mCHBlue[ka]);  
                  var hyperlinkName = ka + "_";
                  mHyperlink = doc.hyperlinks.add(mHyperlinkTextSource2,mHyperlinkDestination2,{name:"Co_" + hyperlinkName});  
                  mHyperlink.visible=false;  
              }  
              //remove URL text  
              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;     
              app.findGrepPreferences.findWhat = '(https?|www)\\S+\\>' + String.fromCharCode(13);  
              app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("hiddenline_ch");
              app.changeGrepPreferences.changeTo = '';    
              doc.changeGrep();
              

               

              Wow, that was a lot. If you read through all this, a huge thanks for hearing me out. I will be celebrating when this is finished, tested and implemented. Then I can get back to my normal job duties. haha :-)

              • 4. Re: Change from one Paragraph Style to another?
                cweeks Level 1

                Thanks Ariel! I'll try this out.