7 Replies Latest reply on Mar 20, 2017 9:31 PM by Qwertyfly...

    Select layers with a timeout


      Hi. I'm trying to code up a little animation plugin and I would like to be able to loop through all the layers and select them one by one (mark as an activeLayer) consecutively with a delay. The selection works but the problem is the sleep() function. I use it in a loop in the playAnimation() method (shown below). Basically illustrator only shows the selection when the loop is over, so when I run the script there is a long pause and then we can see the last layer get selected visually. I'm guessing sleep() may not be the right way to do this since it seems to block graphical updates in illustrator. Can someone steer me in the right direction on how to make layers be selected graphically?


      // Create button
      var animPalette = new Window("palette");
      playButton = animPalette .add("button", undefined, "Play");
      playButton.onClick = function() {
         // Convert function into a string so we can send it as a message
         var message = strMethod(playAnimation);
         var bt = new BridgeTalk;
          bt.target = 'illustrator';
          bt.body = message;
      // Trying to select layer by later with a delay
      // This works but it does not update graphically until the loop is over
      function playAnimation()
           var doc = app.activeDocument;
           var numLayers = doc.layers.length;
          // Check if there is more than one layer
          if (doc.layers.length > 1)
              for(var i=doc.layers.length-1; i >= 0; i--)
                    $.sleep (500);
      // Convert  a method into a string
      function strMethod (fn)  {
          return fn.toString() + fn.name + "()";
      // Evaluate methods when they get sent through BridgeTalk
      BridgeTalk.onRecieve = function(message)
           //$.writeln("message recieved");
        • 1. Re: Select layers with a timeout
          Qwertyfly... Level 4


          this could make it visual, not tested.

          but your still halfway through a program/script that only has 1 thread that is just sitting and ticking.

          $.sleep(); is not a very well liked way of dealing with timing.

          you are best using setTimeout(function,milliseconds); or setInterval(function,milliseconds);

          1 person found this helpful
          • 2. Re: Select layers with a timeout
            zertle Level 2

            redraw(); seems to work up to a certain point but then AI freezes after a few seconds.


            #target illustrator
            function WigglePaths(){
                var paths = app.activeDocument.pathItems;
                var i, j, curPath;
                for( i = 0; i < 250; i++ ) {
                    for( j = 0, jj = paths.length; j < jj; j++ ) {
                        curPath = paths[j];
                        curPath.top += Math.random() * 10-5;
                        curPath.left += Math.random() * 10-5;
            • 3. Re: Select layers with a timeout
              Silly-V Adobe Community Professional

              Hey, there's not setTimeouts in extendscript, are there?

              • 4. Re: Select layers with a timeout
                pencilking2002 Level 1

                I haven't seen a setTimeout. That would be awesome if it existed!

                • 5. Re: Select layers with a timeout
                  pencilking2002 Level 1

                  Thanks Qwertyfly...! app.redraw() seems to do the job. I don't think we have access to setTimeout in extendscript (I could be wrong). Maybe take that part out of your answer so I could mark it correct?

                  • 6. Re: Select layers with a timeout
                    pencilking2002 Level 1

                    @zertle Oh damn it freezes up on you? I'm not seeing that in my code but I'm just getting started with my plugin.

                    • 7. Re: Select layers with a timeout
                      Qwertyfly... Level 4

                      Appy polly loggies,


                      And I just finished telling someone that ECMA3 does not know Json etc...

                      I have been playing with HTML Panels, and was using SetTimeout in Javascript.

                      you could run your script as smaller sections sent to extendscript through bridgetalk from a HTML panel, but that sounds like a lot more work then $.Sleep()...


                      So redraw() or app.redraw() seems to be the best solution.

                      I'm not surprised illy crashed, you would be slamming the javascript engine quite hard.

                      adding try/catch statement may keep it running longer.

                      also, do you need it to refresh that fast.

                      $.Sleep(1000); = 1 second

                      you have 50 in there. so it trying to redraw the screen 20 times per second.

                      can you get away with half that?