6 Replies Latest reply on Sep 14, 2016 4:14 PM by Oriup

    [win][js][cs4]find/change paragraph styles without losing local overrides

    chrlsdrwn12

      Hi guys i need a little help here.

       

      I created a javascript which will find and replace doc file paragraph styles with indesign paragraph styles. everything works fine but the problem is that when it applies the paragraph styles it loses all local overrides. i tried:

      changetext(false);

      but its not working is there some kind of conditional parameter that i can put in the changetext() function so that it wont clear local overrides?

       

      Thanks.

      CharlesD

        • 1. Re: [win][js][cs4]find/change paragraph styles without losing local overrides
          Kasyan Servetsky Level 5

          Use findGrep() (or findText()) instead of changeText(). This returns an array of found items. Then you can loop through the array and apply a paragraph style without clearing overrides (the 2nd parameter should be false).

           

          Example:

          Main();
          
          function Main() {
              var foundItem;    
              var doc = app.activeDocument;
          
              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
              
              app.findGrepPreferences.findWhat = ".+";
              app.findGrepPreferences.appliedParagraphStyle = "Body text";
              
              var foundItems = doc.findGrep();
              
              for (var i = 0; i < foundItems.length; i++) {
                  foundItem = foundItems[i];
                  foundItem.applyParagraphStyle(doc.paragraphStyles.item("Body"), false);
              }
          
              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
          }
          
          • 2. Re: [win][js][cs4]find/change paragraph styles without losing local overrides
            chrlsdrwn12 Level 1

            Thanks Kasyan. You're always there to save my day. Thank you very much...

             

            I slightly modify the script to go through all my paragraph styles that need to be replaced coz there are hundreds of them. Im not sure though if this is the best or is there any faster way to do this:

             

            replace("T1", "Text", "T1");

            replace("T", "Text", "T");

            replace("1", "Heads", "1");

            replace("2", "Heads", "2");

             

            function replace(input, folder, output) {

                var foundItem;   

                var doc = app.activeDocument;

             

                app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

               

                app.findGrepPreferences.findWhat = ".+";

                app.findGrepPreferences.appliedParagraphStyle = input;

               

                var foundItems = doc.findGrep();

               

               

                for (var i = 0; i < foundItems.length; i++) {

                    foundItem = foundItems[i];

                    foundItem.applyParagraphStyle(doc.paragraphStyleGroups.item(folder).paragraphStyles.item( output), false);

                }

             

                app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

            }

            • 3. Re: [win][js][cs4]find/change paragraph styles without losing local overrides
              chrlsdrwn12 Level 1

              Awwww.

               

              The problem now is that it gives an error whenever that paragraph style im finding is not available. How can i make it skip if its not available?

               

              Or im thinking if its possible to find used paragraph styles first then start replacing from that list so i wont have to guess all the possible paragraph styles. something like this:

               

              create a lookup array or dictionary

              get all used paragraph styles

              put it in an array["T1", "T", "1", "2"]

              then start mapping it in the dictionary

              delete all unused paragraph styles

               

              is this even possible?

              • 4. Re: [win][js][cs4]find/change paragraph styles without losing local overrides
                Kasyan Servetsky Level 5

                You can check if a paragraph style exists like so:

                if (doc.paragraphStyles.itemByName("T1") == null) {
                     // skip it
                }
                

                 

                Alternatively, you can create styles if they don't exist:

                if (doc.paragraphStyles.itemByName("T1") == null) {
                    var parStyleT1 = doc.paragraphStyles.add({name : "T1"});
                }
                else {
                    var parStyleT1 = doc.paragraphStyles.itemByName("T1");
                }
                

                After running the script you can change their definitions --  e.g. by loading them from another document.

                • 6. Re: [win][js][cs4]find/change paragraph styles without losing local overrides
                  Oriup Level 1

                  Hi Kasyan,

                   

                  How to skip swatches if not found in activeDocument?

                   

                  var doc = app.activeDocument;

                   

                   

                  //italics

                  replaceColor("Word_R255_G0_B0", "Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_it_0_emp_red");

                  replaceColor("Word_R0_G128_B0", "Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_it_0_emp_green");

                  //replaceColor("Word_R0_G176_B80", "Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_it_0_emp_green");

                  replaceColor("Word_R0_G0_B255", "Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_it_0_emp_blue");

                  replaceColor("Word_R0_G112_B192", "Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_it_0_emp_blue");

                  replaceColor("Word_R51_G102_B255", "Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_it_0_emp_blue");

                  //replaceColor("Word_R112_G48_B160", "Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_it_0_emp_purple");

                  replaceColor("Black", "Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_it");

                   

                   

                  //bold

                  replaceColor("Word_R255_G0_B0", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bld_0_emp_red");

                  replaceColor("Word_R0_G128_B0", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bld_0_emp_green");

                  //replaceColor("Word_R0_G176_B80", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bld_0_emp_green");

                  replaceColor("Word_R0_G0_B255", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bld_0_emp_blue");

                  replaceColor("Word_R0_G112_B192", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bld_0_emp_blue");

                  replaceColor("Word_R51_G102_B255", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bld_0_emp_blue");

                  //replaceColor("Word_R112_G48_B160", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bld_0_emp_purple");

                  replaceColor("Black", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bld");

                  //replaceColor("Word_R191_G191_B191", "Bold", "03_Text", "TXT1_FrutigerLTCom", "TXT1_8pt_bld_k60");

                   

                   

                  //bold italics

                  replaceColor("Word_R255_G0_B0", "Bold Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bi_emp_red");

                  replaceColor("Word_R0_G128_B0", "Bold Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bi_emp_green");

                  //replaceColor("Word_R0_G176_B80", "Bold Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bi_emp_green");

                  replaceColor("Word_R0_G0_B255", "Bold Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bi_emp_blue");

                  replaceColor("Word_R0_G112_B192", "Bold Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bi_emp_blue");

                  replaceColor("Word_R51_G102_B255", "Bold Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bi_emp_blue");

                  //replaceColor("Word_R112_G48_B160", "Bold Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bi_emp_purple");

                  replaceColor("Black", "Bold Italic", "03_Text", "TXT1_FrutigerLTCom", "TXT1_bi");

                   

                   

                  function replaceColor(color, fontType, SgOne, SgTwo, chStyle){

                       var foundItem;

                      

                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                      app.findGrepPreferences.findWhat = "(.+)";

                      app.findGrepPreferences.fillColor = color;

                      app.findGrepPreferences.fontStyle = fontType;

                   

                   

                      var foundItems = doc.findGrep();

                      for (var i = 0; i < foundItems.length; i++) {

                          foundItem = foundItems[i];

                          foundItem.applyParagraphStyle(doc.characterStyleGroups.item(SgOne).characterStyleGroups.i tem(SgTwo).characterStyles.item(chStyle), true);

                      }

                   

                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

                  };

                   

                   

                   

                   

                  function replaceColor(color, fontType, SgOne, SgTwo, chStyle){

                      var foundItem;  

                      var doc = app.activeDocument;

                   

                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                      app.findGrepPreferences.findWhat = "(.+)";

                      app.findGrepPreferences.fillColor = color;

                      app.findGrepPreferences.fontStyle = fontType;

                    

                      var foundItems = doc.findGrep();

                    

                    

                      for (var i = 0; i < foundItems.length; i++) {

                          foundItem = foundItems[i];

                          foundItem.applyCharacterStyle(doc.characterStyleGroups.item(SgOne).characterStyleGroups.i tem(SgTwo).characterStyles.item(chStyle), false);

                      }

                  if (doc.swatches.itemByName("Word_R0_G176_B80") == null) {  };

                  if (doc.swatches.itemByName("Word_R112_G48_B160") == null) {  };

                   

                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

                  };

                   

                  My problem is how to skip multiple swatches, they are commented.

                   

                  Please help me.