8 Replies Latest reply on Jun 14, 2014 2:09 PM by camilo umana

    Script to renumber links in random order

    camilo umana Level 1

      I have chapter 1 page with 50 small photos in a very orthodox and symmetric grid that I should repeat in a different random order for other chapters.

      Links have a numbered logical syntax like:

      pict1.tif

      pict2.tif

      etc.

       

      For the chapter 2, the idea is repeat the first one but in a random order. Same photos, same layout, different order.

      Perhaps a script could rename the links?

      Thus, once the folder is duplicated I will have a second folder:

      picture 1.tif is now, for example, pict45.tif

      picture2.tif.is now, for example, pict13.tif

      etc.

       

      IF I relink a copy of chapter 1 to this second folder perhaps the idea could be solved.

        • 1. Re: Script to renumber links in random order
          Jump_Over Level 5

          Hi,

           

          I suggest to random links rather than duplicate source files and rename them.

          Lets assume we are talking about all graphic on pages 4-6. So we want to relink them to the same group of files but in random order.

          Array with this graphics (lets name it targetArray) is mDoc.pages.itemByRange(3,5).allGraphics;

          Array with source files (lets name it waitRoomArray) is push from each targetArray's (element)[0].itemLink.filePath;

           

          Now we can define a randoming function (lets name it myRandom(scope) ):

          function myRandom(scope) { // returns a random number from 0 to scope

          var

            factor = 0.999999999999999 / scope,

            res = Math.floor( Math.random() / factor );

          return res;

          }

           

          Now we can iterate (growing step) through targetArray and relink its items randomly:

          while (waitRoomArray.length) {

            currIndex =  myRandom(waitRoomArray.length);

            currLinkFile = waitRoomArray.splice(currIndex, 1);

            targetArray[step][0].itemLink.relink(currLinkFile);

            step++;

          }

           

          So whole code can be like this:

          var
            startPNum = 3,
            endPNum = 5,
            mDoc = app.activeDocument,
            targetArray = mDoc.pages.itemByRange(startPNum,endPNum).allGraphics,
            waitRoomArray = [], step = 0,
            k, currIndex, currLinkPath;
          
          
          for (k = 0; k < targetArray.length; k++)
            waitRoomArray.push(targetArray[k][0].itemLink.filePath);
          
          while (waitRoomArray.length) {
            currIndex =  myRandom(waitRoomArray.length);
            currLinkPath = waitRoomArray.splice(currIndex, 1);
            targetArray[step][0].itemLink.relink( File(currLinkPath) );
            step++;
            }
          
          function myRandom(scope) { // returns a random number from 0 to scope
            var
                 factor = 0.999999999999999 / scope,
                 res = Math.floor( Math.random() / factor );
            return res;
            }
          

           

          Jarek

          • 2. Re: Script to renumber links in random order
            camilo umana Level 1

            Jarek,

             

            Thanks. I assume this is a correct answer but could not tested the script because I have an amateur error.

            Could you please help me to run it?

            One question; the item to be changed is only Doc.pages.itemByRange?

            Thanks again for your time.

            Screen Shot 2014-06-12 at 08.50.12.jpg

            • 3. Re: Re: Script to renumber links in random order
              Jump_Over Level 5

              Hi,

               

              Do not copy code line with numbers (1.  2.  etc)

               

              1. var 
              2.   startPNum = 3
              3.   endPNum = 5
              4.   mDoc = app.activeDocument, 

               

              Place it as plain text:

               

              var

                startPNum = 3,

                endPNum = 5,

                mDoc = app.activeDocument,

                targetArray = mDoc.pages.itemByRange(startPNum,endPNum).allGraphics,

                waitRoomArray = [], step = 0,

                k, currIndex, currLinkPath;

               

               

              for (k = 0; k < targetArray.length; k++)

                waitRoomArray.push(targetArray[k][0].itemLink.filePath);

               

              while (waitRoomArray.length) {

                currIndex =  myRandom(waitRoomArray.length);

                currLinkPath = waitRoomArray.splice(currIndex, 1);

                targetArray[step][0].itemLink.relink( File(currLinkPath) );

                step++;

                }

               

              function myRandom(scope) { // returns a random number from 0 to scope

                var

                    factor = 0.999999999999999 / scope,

                    res = Math.floor( Math.random() / factor );

                return res;

                }

               

              Jarek

              • 4. Re: Script to renumber links in random order
                camilo umana Level 1

                Screen Shot 2014-06-13 at 03.21.24.jpg



                Jarek, hi.


                It works when it is only ONE picture by page.

                But, with many pictures in the page, as in the example, the script is dead.

                (no error at all)

                 

                I have six pages with the same grid of pictures repeated in a file.

                The idea is that in p. 2, 3, etc, the script alters the order in random to have different positions.

                 

                Tried different combinations but were useless.

                 

                Thanks.

                 

                PD. The screenshots show the script working with single images.

                jarek-on.jpgjarek-of.jpg

                • 5. Re: Script to renumber links in random order
                  Jump_Over Level 5

                  Hi,

                   

                  Try to discover how it works, pls

                  1. set start page

                  2. set end page

                  3. script will read all pictures inbetween this range and mix them up.

                   

                  It doesn't?

                   

                  If it does - mark a proper post as "correct" in this thread, pls.

                   

                  Jarek

                  • 6. Re: Script to renumber links in random order
                    camilo umana Level 1

                    Jarek,

                     

                    Thanks (I am deeply interested in this thread and your help and time!)

                    I was checking extensively the script this morning and «seems» that it works but with a single picture by page.

                    With many pictures it is different. The link palette (partial view) shows no change at all after running the script.

                    Please see the pictures above.

                     

                    Screen Shot 2014-06-13 at 13.05.49.jpg

                    Screen Shot 2014-06-13 at 13.02.18.jpg

                    • 7. Re: Re: Script to renumber links in random order
                      Jump_Over Level 5

                      Hi,

                       

                      I've got your point, sorry - it was an array inside array.

                      Here is the fixed one:

                      var 
                        startPNum = 3, 
                        endPNum = 3, 
                        mDoc = app.activeDocument, 
                        mGraphics = mDoc.pages.itemByRange(startPNum,endPNum).allGraphics, 
                        waitRoomArray = [], targetArray = [],
                        step = 0, 
                        k, currIndex, currLinkPath; 
                      
                      for (k = 0; k < mGraphics.length; k++) 
                        targetArray = targetArray.concat(mGraphics[k]);
                      
                      for (k = 0; k < targetArray.length; k++) 
                        waitRoomArray.push(targetArray[k].itemLink.filePath); 
                      
                      while (waitRoomArray.length) { 
                        currIndex =  myRandom(waitRoomArray.length); 
                        currLinkPath = waitRoomArray.splice(currIndex, 1); 
                        targetArray[step].itemLink.relink( File(currLinkPath) ); 
                        step++; 
                        } 
                      
                      function myRandom(scope) { // returns a random number from 0 to scope 
                        var 
                            factor = 0.999999999999999 / scope, 
                            res = Math.floor( Math.random() / factor ); 
                        return res; 
                        } 
                      

                       

                      hopefully...

                      Jarek

                      • 8. Re: Script to renumber links in random order
                        camilo umana Level 1

                        Jarek, well, now the script is working perfectly

                        Well, again, thanks. One thousand thanks for this piece.

                        Regards and nice weekend.