22 Replies Latest reply on Jul 27, 2014 5:32 AM by Teetan VK

    How can I find-change only in the master pages?

    Teetan VK Level 1

      Hi, everyone

       

      How can I find-change only in the master pages?

       

      for instance:

       

      I want to change 2013 to 2014 but only in the master pages.

       

      in all the opening files' master pages.

       

      How can I do that?

       

       

      Teetan

        • 1. Re: How can I find-change only in the master pages?
          Jump_Over Level 5

          Hi,

           

          First use find(), after this filter results and finally change those one you need to.

          There is no direct option.

           

          Jarek

          • 2. Re: How can I find-change only in the master pages?
            Teetan VK Level 1

            Thank you jarek

             

            I will use synchronize in a book

             

            Teetan

            • 3. Re: Re: How can I find-change only in the master pages?
              Kai Rübsamen Adobe Community Professional

              I would do it in this way:

               

              // Your settings
              // ---
              var SEARCH_PATTERN = '2013';
              var CHANGE_PATTERN = '2014';
              
              // Set SEARCH
              // ---
              app.findTextPreferences = app.changeTextPreferences = null;
              app.findChangeTextOptions.includeMasterPages = true;
              app.findTextPreferences.findWhat = SEARCH_PATTERN;
              app.changeTextPreferences.changeTo = CHANGE_PATTERN;
              
              // Variables
              // ---
              var allDocs = app.documents,
                  nDocs = allDocs.length,
                  nFounds = [],
                  counter = 0;
                  
              // SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS
              // ---
              for ( var i = 0; i < nDocs; i++ ) {
                  var curDoc = app.documents[i],
                  mPages = curDoc.masterSpreads.everyItem().pages.itemByRange(0, -1),
                  target = mPages.textFrames.everyItem().paragraphs.everyItem();
              
                  target.changeText();
                  nFounds.push(target.changeText().length);
              }
              
              app.findTextPreferences = app.changeTextPreferences = null;
              
              for ( var i = 0; i < nFounds.length; i ++ ) {
                  counter = counter + nFounds[i];
              }
                  
              
              alert( counter + " expressions have been changed." );
              
              
              

               

              – Kai

              • 4. Re: How can I find-change only in the master pages?
                Jump_Over Level 5

                Hi,

                 

                I was to quick with my 1st answer, Kai is right.

                changeText() method can be used with various targets. So if you prepare a target first - there is a direct option.

                 

                Jarek

                • 5. Re: How can I find-change only in the master pages?
                  Teetan VK Level 1

                  Hi, Jarek and Kai

                   

                  I try the write script by my self like this but not work:

                   

                  //---------------------------------------------------------------------------------------- -----------------

                   

                  app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Indent Paragraphs"); 

                   

                  function main(){

                  var _selection = app.selection[0]; 

                  for ( i = 1; i < app.documents.length; i++) {    

                    myDoc = app.documents[i];    

                  app.findGrepPreferences = changeGrepPreferences = null; 

                  app.findGrepPreferences.findWhat = "\\b20+\\b"; 

                  var found = app.selection[0].findGrep(); 

                  for(var f=0;f<found.length;f++) 

                          app.findGrepPreferences = changeGrepPreferences = null; 

                          app.findGrepPreferences.findWhat = "\\d+"; 

                          var foundnum = found[f].findGrep(); 

                          var newnum = parseInt(foundnum[0].contents) + 1; 

                          app.changeGrepPreferences.changeTo = newnum.toString(); 

                          found[f].changeGrep(); 

                           } 

                       }

                  }

                   

                  //---------------------------------------------------------------------------------------- ------------------

                   

                  how can fix it?

                  • 6. Re: Re: How can I find-change only in the master pages?
                    Kai Rübsamen Adobe Community Professional

                    Hi,

                     

                    I realize, that here is no need for itemByRange.

                     

                    So the target line could be writen shorter (no itemByRange needed):

                     

                    target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();

                     

                    – Kai

                    • 7. Re: How can I find-change only in the master pages?
                      Teetan VK Level 1

                      Hi,

                       

                      How can I combine the two scripts idea into one?

                       

                      Teetan

                      • 8. Re: How can I find-change only in the master pages?
                        Jump_Over Level 5

                        @Teetan

                         

                        Which numbers are you going to grow?

                        - each found on master pages

                        - only the one started with "20"

                        - is it posible to match a number 2099 there?

                         

                        Jarek

                        • 9. Re: How can I find-change only in the master pages?
                          Teetan VK Level 1

                          Hi, I try again

                           

                          changed it into this:

                           

                          but not error

                           

                          //---------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------

                           

                          app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Find-change num"); 

                           

                          function main(){

                              for ( var i = 0; i < nDocs; i++ ) { 

                                  var curDoc = app.documents[i], 

                                  mPages = curDoc.masterSpreads.everyItem().pages.itemByRange(0, -1), 

                                  target = mPages.textFrames.everyItem().paragraphs.everyItem(); 

                           

                          app.findGrepPreferences = changeGrepPreferences = null; 

                          app.findGrepPreferences.findWhat = "\\b20+\\b"; 

                          var found = app.selection[0].findGrep(); 

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

                                  app.findGrepPreferences = changeGrepPreferences = null; 

                                  app.findGrepPreferences.findWhat = "\\d+"; 

                                  var foundnum = found[f].findGrep(); 

                                  var newnum = parseInt(foundnum[0].contents) + 1; 

                                  app.changeGrepPreferences.changeTo = newnum.toString(); 

                                  found[i].changeGrep(); 

                                   } 

                               }

                          }

                           

                          //---------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------

                           

                          Teetan

                          • 10. Re: How can I find-change only in the master pages?
                            Teetan VK Level 1

                            Hi,

                             

                            Hi, Jarek and Kai

                             

                            what I want to change has growing requested, and I wish it has an undo possible.

                             

                            thank you

                             

                            Teetan

                            • 11. Re: How can I find-change only in the master pages?
                              Teetan VK Level 1

                              Hi

                               

                              Jarek

                               

                              may be 2099

                               

                              Teetan

                              • 12. Re: Re: How can I find-change only in the master pages?
                                Kai Rübsamen Adobe Community Professional

                                Hi,

                                 

                                I changed my current version in the meantime a bit, since the counter did show the wrong results, if you start the script a second time.

                                 

                                // Your settings
                                // ---
                                var SEARCH_PATTERN = '2013';
                                var CHANGE_PATTERN = '2014';
                                
                                // Set SEARCH
                                // ---
                                app.findTextPreferences = app.changeTextPreferences = null;
                                app.findChangeTextOptions.includeMasterPages = true;
                                app.findTextPreferences.findWhat = SEARCH_PATTERN;
                                app.changeTextPreferences.changeTo = CHANGE_PATTERN;
                                
                                // Variables
                                // ---
                                var allDocs = app.documents,
                                    nDocs = allDocs.length,
                                    nFounds = [],
                                    counter = 0;
                                  
                                // SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS
                                // ---
                                for ( var i = 0; i < nDocs; i++ ) {
                                    var curDoc = app.documents[i],
                                    target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();  
                                    var allFounds = target.changeText();
                                  
                                    for (var n = 0; n < allFounds.length; n ++) {
                                        var curFound = allFounds[n];
                                        if (curFound.length != 0) {
                                            counter += curFound.length;
                                        }
                                    }
                                }
                                
                                app.findTextPreferences = app.changeTextPreferences = null;
                                
                                alert( counter + " expressions have been changed." );
                                
                                
                                

                                 

                                Teetan, I have no idea what you try to do with your second script. Please provide clear steps (1,2,3) and maybe a screenshot before / after.

                                 

                                And: Realize that 'undo' is available only per document. So if you have 20 docs open, you must undo in every document, even if we spent a doScript-Function!

                                • 13. Re: How can I find-change only in the master pages?
                                  Teetan VK Level 1

                                  Hi,

                                   

                                  Kai

                                   

                                  sorry I confuse you in some way

                                   

                                  now I clarify it

                                   

                                  1)  I want to find-change the digit only in all the opening files' master

                                  2)  the digit has growing requested by years, I mean this year I find 2013 change to 2014 and next year I have to chan 2014 change to 2015

                                   

                                  and finally I wish the script has UndoModes.ENTIRE_SCRIPT function

                                   

                                   

                                  Regard

                                   

                                  Teetan

                                  • 14. Re: How can I find-change only in the master pages?
                                    Teetan VK Level 1

                                    Hi Kai

                                     

                                    this script can find digit then change +1

                                     

                                    //---------------------------------------------------------------------------------------- -----

                                     

                                    app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Indent Paragraphs"); 

                                     

                                    function main(){

                                    var _selection = app.selection[0]; 

                                    app.findGrepPreferences = changeGrepPreferences = null; 

                                    app.findGrepPreferences.findWhat = "aged \\d+"; 

                                    var found = app.selection[0].findGrep(); 

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

                                            app.findGrepPreferences = changeGrepPreferences = null; 

                                            app.findGrepPreferences.findWhat = "\\d+"; 

                                            var foundnum = found[i].findGrep(); 

                                            var newnum = parseInt(foundnum[0].contents) + 1; 

                                            app.changeGrepPreferences.changeTo = newnum.toString(); 

                                            found[i].changeGrep(); 

                                        } 

                                    app.findGrepPreferences = changeGrepPreferences = null; 

                                     

                                    //---------------------------------------------------------------------------------------- -------------

                                     

                                    I get this idea, but I want it only work on master pages

                                     

                                    Regard

                                     

                                    Teetan

                                    • 15. Re: How can I find-change only in the master pages?
                                      Teetan VK Level 1

                                      Hi Kai

                                       

                                      can you help correct this script:

                                       

                                       

                                              app.doScript('main()', ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Find-change digit" );          

                                       

                                      function main(){ 

                                       

                                      // Set SEARCH 

                                      // --- 

                                      app.findTextPreferences = app.changeTextPreferences = null; 

                                      app.findChangeTextOptions.includeMasterPages = true; 

                                      app.findTextPreferences.findWhat = "2017"; 

                                      app.changeTextPreferences.changeTo = "2018"; 

                                       

                                      app.findTextPreferences = app.changeTextPreferences = null; 

                                      app.findChangeTextOptions.includeMasterPages = true; 

                                      app.findTextPreferences.findWhat = "2015"; 

                                      app.changeTextPreferences.changeTo = "2016"; 

                                       

                                      app.findTextPreferences = app.changeTextPreferences = null; 

                                      app.findChangeTextOptions.includeMasterPages = true; 

                                      app.findTextPreferences.findWhat = "2014"; 

                                      app.changeTextPreferences.changeTo = "2015"; 

                                       

                                      app.findTextPreferences = app.changeTextPreferences = null; 

                                      app.findChangeTextOptions.includeMasterPages = true; 

                                      app.findTextPreferences.findWhat = "2013"; 

                                      app.changeTextPreferences.changeTo = "2014"; 

                                       

                                      // Variables 

                                      // --- 

                                      var allDocs = app.documents, 

                                          nDocs = allDocs.length, 

                                          nFounds = [], 

                                          counter = 0; 

                                         

                                      // SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS 

                                      // --- 

                                      for ( var i = 0; i < nDocs; i++ ) { 

                                          var curDoc = app.documents[i], 

                                          target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();   

                                          var allFounds = target.changeText(); 

                                         

                                          for (var n = 0; n < allFounds.length; n ++) { 

                                              var curFound = allFounds[n]; 

                                              if (curFound.length != 0) { 

                                                  counter += curFound.length;

                                                      }

                                              } 

                                          } 

                                       

                                      app.findTextPreferences = app.changeTextPreferences = null; 

                                       

                                      alert( counter + " expressions have been changed." );

                                       

                                       

                                       

                                      Teetan

                                      • 16. Re: Re: How can I find-change only in the master pages?
                                        Jump_Over Level 5

                                        Hi Teetan,

                                         

                                        To define a goal is more than half way of creating script.

                                         

                                        Check this:

                                        app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "grow numbers on masters");
                                        function main() {
                                          catchOption = app.findChangeGrepOptions.includeMasterPages;
                                          app.findChangeGrepOptions.includeMasterPages = true;
                                          app.findGrepPreferences = changeGrepPreferences = null;
                                          app.findGrepPreferences.findWhat = "\\b\\d+\\b";
                                        
                                        var
                                          mTarget = app.documents.everyItem().masterSpreads.everyItem().textFrames.everyItem(),
                                          mFound = mTarget.findGrep(),
                                          mConFound = [],
                                          len = mFound.length;
                                        
                                          if (len && mFound[0].constructor.name == "Array")
                                               while (len-->0)
                                                    mConFound = mConFound.concat(mFound[len]);
                                        
                                          len = mConFound.length;
                                          while (len-->0) {
                                               mConFound[len].contents = String(Number(mConFound[len].contents) + 1);
                                          }
                                          app.findChangeGrepOptions.includeMasterPages = catchOption;
                                        }
                                        

                                         

                                        Jarek

                                        • 17. Re: How can I find-change only in the master pages?
                                          Teetan VK Level 1

                                          Hi Jreak,

                                           

                                          it's what I want

                                           

                                          thank you so much

                                           

                                          appreciate

                                           

                                          Teetan

                                          • 18. Re: How can I find-change only in the master pages?
                                            Teetan VK Level 1

                                            Hi Jreak

                                             

                                            Can you tell me

                                             

                                            this script can only work on:

                                             

                                            //---------------------------------------------------------------------------------------- -----------

                                            app.doScript('main()', ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Find-change digit" );          

                                             

                                            function main(){ 

                                             

                                            // Set SEARCH 

                                            // --- 

                                            app.findTextPreferences = app.changeTextPreferences = null; 

                                            app.findChangeTextOptions.includeMasterPages = true; 

                                            app.findTextPreferences.findWhat = "2017"; 

                                            app.changeTextPreferences.changeTo = "2018"; 

                                             

                                            app.findTextPreferences = app.changeTextPreferences = null; 

                                            app.findChangeTextOptions.includeMasterPages = true; 

                                            app.findTextPreferences.findWhat = "2015"; 

                                            app.changeTextPreferences.changeTo = "2016"; 

                                             

                                            app.findTextPreferences = app.changeTextPreferences = null; 

                                            app.findChangeTextOptions.includeMasterPages = true; 

                                            app.findTextPreferences.findWhat = "2014"; 

                                            app.changeTextPreferences.changeTo = "2015"; 

                                             

                                            //========================================================

                                            app.findTextPreferences = app.changeTextPreferences = null;    // only work on this part

                                            app.findChangeTextOptions.includeMasterPages = true;           // the rest are unfunction

                                            app.findTextPreferences.findWhat = "2013";                            // can you tell me why?

                                            app.changeTextPreferences.changeTo = "2014";                     //  can you make it happen, in case of find something not a digit

                                            //============================================================

                                             

                                            // Variables 

                                            // --- 

                                            var allDocs = app.documents, 

                                                nDocs = allDocs.length, 

                                                nFounds = [], 

                                                counter = 0; 

                                               

                                            // SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS 

                                            // --- 

                                            for ( var i = 0; i < nDocs; i++ ) { 

                                                var curDoc = app.documents[i], 

                                                target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();   

                                                var allFounds = target.changeText(); 

                                               

                                                for (var n = 0; n < allFounds.length; n ++) { 

                                                    var curFound = allFounds[n]; 

                                                    if (curFound.length != 0) { 

                                                        counter += curFound.length;

                                                            }

                                                    } 

                                                } 

                                             

                                            app.findTextPreferences = app.changeTextPreferences = null; 

                                             

                                            alert( counter + " expressions have been changed." );

                                             

                                            }

                                            //---------------------------------------------------------------------------------------- -----------

                                             

                                             

                                            regard

                                             

                                            Teetan

                                            • 19. Re: Re: How can I find-change only in the master pages?
                                              Kai Rübsamen Adobe Community Professional

                                              Jarek, your version ist much shorter, than my script, great

                                               

                                              I have this in the meantime:

                                               

                                              if ( app.documents.length > 0 ) {
                                                  if ( app.scriptPreferences.version >= 6 ) {
                                                      app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Search in Masterpages" );        
                                                  }
                                                  else {
                                                      main();
                                                  }
                                              } else {
                                                  alert ( "No documents open!" );
                                              }
                                              
                                              function main() {
                                              
                                                  // Your settings
                                                  // ---
                                                  var SEARCH_PATTERN = '\\b201[4-6]\\b';
                                              
                                                  // Set SEARCH
                                                  // ---
                                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                                  app.findChangeGrepOptions.includeMasterPages = true;
                                                  app.findGrepPreferences.findWhat = SEARCH_PATTERN;
                                              
                                                  // Variables
                                                  // ---
                                                  var allDocs = app.documents,
                                                      nDocs = allDocs.length,
                                                      nFounds = [],
                                                      counter = 0;
                                                     
                                                  // SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS
                                                  // ---
                                                  for ( var i = 0; i < nDocs; i++ ) {
                                                      var curDoc = app.documents[i],
                                                      target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();   
                                                      var allFounds = target.findGrep();
                                                     
                                                      for ( var n = 0; n < allFounds.length; n ++ ) {
                                                          var framesFound = allFounds[n];
                                                          if ( framesFound.length > 0 ) {
                                                               for ( var j = framesFound.length-1; j >= 0 ; j-- ) {
                                                                          var curFound = framesFound[j];
                                                                      var newNumber = (parseInt( curFound.contents) +1).toString();
                                                                      curFound.contents = newNumber;                                                   
                                                              } // for
                                                          counter += framesFound.length;
                                                          } // if
                                                      } // for
                                                  } // for
                                              
                                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                                  alert( counter + " expressions have been changed." );
                                              } // main
                                              
                                              
                                              • 20. Re: How can I find-change only in the master pages?
                                                Teetan VK Level 1

                                                Hi, Kai

                                                 

                                                what if find-change something not a digit, but only in master?

                                                 

                                                Teetan

                                                • 21. Re: Re: How can I find-change only in the master pages?
                                                  Kai Rübsamen Adobe Community Professional

                                                  Teetan, first of all, after Jareks code, I correct my version a bit. Nevertheless my version is a bit longer than Jareks, because I check some things more, have a alert at the end and I’m not able to write such short scripts as Jarek (as a advanced beginner I have always problems with while loops …).

                                                   

                                                  The trick ist the line nr. 16 SEARCH_Pattern. Here you can enter the right expression. E.g. if you want to find 2013 - 2016 change [4-6] to [3-6]. However: The script assumes, that you want to grow up numbers +1. So if you want to change other things on the masterpages, all those scripts from today won’t work and must be modified > new goals >> new scripts ;-)

                                                   

                                                  if ( app.documents.length > 0 ) {
                                                      if ( app.scriptPreferences.version >= 6 ) {
                                                          app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Search in Masterpages" );       
                                                      }
                                                      else {
                                                          main();
                                                      }
                                                  } else {
                                                      alert ( "No documents open!" );
                                                  }
                                                  
                                                  function main() {
                                                  
                                                      // Your settings
                                                      // ---
                                                      var SEARCH_PATTERN = '\\b201[4-6]\\b';
                                                  
                                                      app.findGrepPreferences = app.changeGrepPreferences = null;
                                                      app.findChangeGrepOptions.includeMasterPages = true;
                                                      app.findGrepPreferences.findWhat = SEARCH_PATTERN;
                                                  
                                                      var target = app.documents.everyItem().masterSpreads.everyItem().textFrames.everyItem();          
                                                      var allFounds = target.findGrep();
                                                    
                                                      var counter = 0;
                                                    
                                                      for ( var n = 0; n < allFounds.length; n ++ ) {
                                                          var framesFound = allFounds[n];
                                                          if ( framesFound.length > 0 ) {
                                                              for ( var j = framesFound.length-1; j >= 0 ; j-- ) {
                                                                  var curFound = framesFound[j];
                                                                  curFound.contents = String( Number( curFound.contents) + 1 );                                                  
                                                              }
                                                          counter += framesFound.length;
                                                          } // if
                                                      } // for
                                                  
                                                      app.findGrepPreferences = app.changeGrepPreferences = null;
                                                      alert( counter + " expressions have been changed." );
                                                  } // main
                                                  
                                                  
                                                  

                                                   

                                                  but not today. Sunny wheather. Let’s go outside .