1 Reply Latest reply on Aug 2, 2014 3:10 AM by Teetan VK

    How can I combine two very good ideas together?

    Teetan VK Level 1

      I got two scripts

      both function: find-change on master

      I love both very mcuh

      but how can combine two scripts together?

       

      1st:

       

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

      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 searchList = [

              [ "USA" , "Obama" ],

              [ "rocket" , "sky" ],

              [ "New York" , "Los Angeles" ]

          ];

       

          app.findGrepPreferences = app.changeGrepPreferences = null;

          app.findChangeGrepOptions.includeMasterPages = true;

          var target = app.documents.everyItem().masterSpreads.everyItem().textFrames.everyItem();

         

          for ( var j = 0; j < searchList.length; j++ ) {

              var curSearch = searchList[j];

              app.findGrepPreferences.findWhat = curSearch[0]; 

              app.changeGrepPreferences.changeTo= curSearch[1]; 

              target.changeGrep(); 

          }    

          app.findGrepPreferences = app.changeGrepPreferences = null;

      } // main

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

       

      made by Kai Rübsamen

       

       

       

       

      2nd:

       

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

      var myDocuments = app.documents.everyItem();
      var myLayers = myDocuments.layers.everyItem();

      //Locked text frames on master spreads are not touched.
      //If you want to unlock them you have to add something like this:

      //Unlock ALL page items in every document on spreads and master spreads (optional):
      myDocuments.pageItems.everyItem().locked = false;

      var myPageItemsOnNormalSpreads = myDocuments.spreads.everyItem().pageItems.everyItem();

      //Unlock the layers (optional)
      myLayers.locked = false;

      //Lock all first level page items on normal spreads
      //That leaves the page items on master spreads unlocked:
      myPageItemsOnNormalSpreads.locked = true;

      //Now for the GREP part:
      app.findChangeGrepOptions.includeMasterPages = true;
      app.findGrepPreferences = changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "\\b20\\d+\\b";

      //Do the search on "all open document" level
      //That will return an array of arrays:
      var myTarget = myDocuments.findGrep();

      //Flattening the myTarget array:
      var flattenedFoundArray = [];

      for(var n=0;n<myTarget.length;n++){
          flattenedFoundArray = flattenedFoundArray.concat(myTarget[n]);
          };

      //Change contents of found texts in flattened array:
      for(var n=flattenedFoundArray.length-1;n>=0;n--){
          flattenedFoundArray[n].contents = String(Number(flattenedFoundArray[n].contents) + 1);
          };

      //Unlock all objects on normal spreads:
      myPageItemsOnNormalSpreads.locked = false;

      //Reset GREP find and change prefs:
      app.findGrepPreferences = changeGrepPreferences = null;

       

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

       

      made by Uwe

       

       

       

      thanks

       

      Teetan

        • 1. Re: How can I combine two very good ideas together?
          Teetan VK Level 1

          Hi, everyone

           

          I try to combine to script together

          but got an error

          can you guys help me fix it?

           

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

           

          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 myDocuments = app.documents.everyItem();
          var myLayers = myDocuments.layers.everyItem();

          //Locked text frames on master spreads are not touched.
          //If you want to unlock them you have to add something like this:

          //Unlock ALL page items in every document on spreads and master spreads (optional):
          myDocuments.pageItems.everyItem().locked = false;

          var myPageItemsOnNormalSpreads = myDocuments.spreads.everyItem().pageItems.everyItem();

          //Unlock the layers (optional)
          myLayers.locked = false;

          //Lock all first level page items on normal spreads
          //That leaves the page items on master spreads unlocked:
          myPageItemsOnNormalSpreads.locked = true;

              var searchList = [
                  [ "USA" , "Obama" ],
                  [ "rocket" , "sky" ],
                  [ "New York" , "Los Angeles" ]
              ];

              app.findGrepPreferences = app.changeGrepPreferences = null;
              app.findChangeGrepOptions.includeMasterPages = true;

          var myTarget = myDocuments.findGrep();

          //Flattening the myTarget array:
          var flattenedFoundArray = [];

          for(var n=0;n<myTarget.length;n++){
              flattenedFoundArray = flattenedFoundArray.concat(myTarget[n]);
              };


              for ( var j = 0; j < searchList.length; j++ ) {
                  var curSearch = searchList[j];
                  app.findGrepPreferences.findWhat = curSearch[0]; 
                  app.changeGrepPreferences.changeTo= curSearch[1]; 
                  target.changeGrep(); 
              }    

          //Unlock all objects on normal spreads:
          myPageItemsOnNormalSpreads.locked = false;

              app.findGrepPreferences = app.changeGrepPreferences = null;

          } // main

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

           

          thanks

           

          Teetan