2 Replies Latest reply on Aug 13, 2009 6:30 AM by [Jongware]

    Need help with loop

    Seth McGraw Level 1

      The script below analyses an indesign doc for missing links and then places library asset on the page, cuts and "pastes into" any frame with a missing link (the cut and paste thing sucks, but we can't figure out any other way to avoid an external image source for the missing pic placeholder graphic). The loop finds the missing links except for 2. If there are 5 missing links, it finds 3. If there are 10 missing links, it finds 8...etc. Why won't it catch them all?



      Script below:


      function myCheckGraphics(){
              for(var myCounter = 0; myCounter < myDocument.allGraphics.length; myCounter++){
                  var myGraphic = myDocument.allGraphics[myCounter];
                  if(myGraphic.itemLink.status == LinkStatus.LINK_MISSING){
                      var myLib=app.libraries.item("Missing Link.indl");
                  if(File.fs == "Macintosh"){
                      var myLibPath=app.open(File("/Applications/Adobe Indesign CS3/Cover Setup/Missing Link.indl"));
                  myLibPath=app.open(File("/c/Program%20Files/Adobe/Cover Setup/Missing Link.indl"));
                  var selectMiss=app.select(myGraphic);   
                  var selectBox=app.select(missLink);
                  var myX=missLink.visibleBounds;
                  var myTop=myX[0];
                  var myLeft=myX[1];
                  var myBottom=myX[2];
                  var myRight=myX[3];
                  var myWide=myRight-myLeft;
                  var myBigBottom=myBottom-myTop;
                      myAss=myLib.assets.item("Missing Link Big");
                  else if((myWide>15)||(myBigBottom>10)){
                      myAss=myLib.assets.item("Missing Link Medium");
                      myAss=myLib.assets.item("Missing Link Small");
                      }//end if missing link
                  }//end for loop
          function myMissAlert(){
              ////place the asset.//////
              myAss.placeAsset (myDocument) ;
              var myAsset=myDocument.pageItems.item("Missing Link");
              var selectAss=app.select(myAsset);
              var myDupe = app.copy() ;
              var det=myAsset.remove();
              var selectBox=app.select(missLink);
              var myPaste=app.pasteInto()
              app.select(NothingEnum.nothing, undefined);

        • 1. Re: Need help with loop
          Seth McGraw Level 1

          The problem has been solved by changing the counter to "+2" instead of "++". Since "++" means "the next one", can anyone tell me why this worked and clear my head of confusion?



          • 2. Re: Need help with loop
            [Jongware] Most Valuable Participant

            A fairly recurrent "problem" with Indesign.


            The loop checks for missing links on an array of images. However, as soon as you correct one of these links, the array it's checking gets updated in the background, so the loop seems to skip every other image!


            Suppose you have an array


            missingLinks = [ "a", "b", "c" ]


            (where all are missing) and you loop from 0 to 2. The first one checks #0 and corrects it. Now the array will change (in the background) to


            ["b", "c"]


            ... and your loop happily continues with #1 -- "c".


            The common solution is to loop over the array backwards:


            for(var myCounter = myDocument.allGraphics.length-1; myCounter >= 0; myCounter--)


            Notice you have to use "length - 1" and check "myCounter >= 0", because the array elements are numbered from 0 to length-1 -- perfectly logical for a computer, somewhat less so for a human. The regular forwards loop does exactly the same, but it's a bit more 'hidden' from casual inspection.