2 Replies Latest reply on Sep 29, 2013 8:48 AM by tusharde

    Image swap script causing crashes.

    tusharde Level 1

      I am writing a script that will randomly swap images between selected image frames. The script works fine (but it's still slow) on a smaller selection. But once the selection gets past 12/14 items it causes indesign to crash.

       

      any thoughts?

       

      thanks

       

       

      if (parseFloat(app.version) < 6) {
         main();
      } else {
         app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Swap Images");
      }
      
      function main(){
         var mySelection = app.selection;
         if (!mySelection.length < 3)
         {
            var framesArr = [];
            var imagesArr = [];
            for(var i = 0; i < mySelection.length; i++)
            { 
               if(mySelection[i].allGraphics.length && mySelection[i].allGraphics[0].itemLink.status == LinkStatus.NORMAL) {
                  framesArr.push(mySelection[i]);
                  imagesArr.push(mySelection[i].allGraphics[0].itemLink.filePath);
               }
            }
      
            randomizeLinks(framesArr, imagesArr);
      
         } else {
            alert("Please select three or more image frames");
         }
      }
      
      
      function randomizeLinks(framesArr, imagesArr)
      {
         imagesArrRand = imagesArr.sort(randOrd);
      
         swapImages(framesArr, imagesArrRand);
      }
      
      
      function randOrd()
      {
         return (Math.round(Math.random())-0.5);
      }
      
      
      function swapImages(frames, links)
      {
            for(var j = 0; j < frames.length; j++)
            {
                  frames[j].allGraphics[0].remove();
      //~             $.writeln(frames[j] + " " + links[j] + "\r\r")
                  frames[j].place(links[j]);
            }
      }