3 Replies Latest reply on Dec 14, 2013 1:48 PM by Marc Autret

    Need help to optimize this script.

    Ruq Level 1

      Hello,

       

      I am using ID CS5 on Windows7.

       

      I have a requirment where I need to type text in a perticular text frame and have it copied to other text frames.

       

      I have written this little script which does the job for me.

      It has some obvious repitations and is inefficient.

       

      Please help me to optimize it.

       

       

      var myDocument = app.activeDocument;

      var myPage = myDocument.pages.item(0);

       

       

      //find a text frame with specific script label and store its contents in a variable

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

      {

                          currItem = myPage.pageItems.item(i);

                          if(currItem.label == "ParentText")

                          {

                                    var source = currItem.contents;

                          }

      }

       

       

      //find all text frames except the text frame with script label and set its contents to source

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

      {

                          currItem = myPage.pageItems.item(i);

       

                          if(currItem.label != "ParentText")

                          {

                                    currItem.contents = source;

                          }

      }

        • 1. Re: Need help to optimize this script.
          Jump_Over Level 5

          Hi,

           

          You could match your "template" textFrame using string functions.

          Text operations are faster and there is no needs of additional loops:

          var 
                    mLabel = "ParentText",
                    mBreak = ";_;",
                    mString = app.activeDocument.textFrames.everyItem().label.join(mBreak ),
                    mID = mString.search(mLabel), 
                    mFrames = app.activeDocument.textFrames.everyItem().getElements(),
                    source, k;
          
          if (mID == -1)  { alert ("Desired label not found"); exit(); }
          
          mID = mString.slice(0,mID).split(mBreak).length - 1;
          source = app.activeDocument.textFrames[mID].contents;
          
          for (k = 0; k < mFrames.length; k++)
                    if (mFrames[k].label != mLabel)
                              mFrames[k].contents = source;
          

           

          Joined array of strings returns a string,

          Search method returns index,

          Slice method cuts the string,

          Split method trasforms it back to the array which length (minus 1) is equal to textFrame index.

           

          The rest is to change frames contents inside a loop

           

          Jarek

          • 2. Re: Need help to optimize this script.
            Ruq Level 1

            Thanks Jump_Over for your reply.

             

            The script you suggested works like a charm.

             

            Thanks.

            • 3. Re: Need help to optimize this script.
              Marc Autret Level 4

              Nice one Jarek

               

              Thanks for sharing.

               

              @+

              Marc