7 Replies Latest reply on Apr 28, 2014 9:14 AM by Matthias Guntrum

    Anything similar to javascript's setInterval in ExtendScript?

    Kashmira

      I need a way to poll my server for new jobs for the illustrator script to work on every few hours.

      I tried with $.sleep(), but that is blocking, I need a non-blocking way to do this.

      Any advice?

       

      Thanks!

      Kashmira

        • 1. Re: Anything similar to javascript's setInterval in ExtendScript?
          W_J_T Level 4

          I have often wondered the same thing and researched it pretty extensively but to no avail. I am still wondering if there is some hack approach using dates, loops or practically anything to make a home brewed interval timer. But it seems like no. Even using Date for checking pretty much ends up emulating $sleep() from my tests. Why Adobe omitted common timers and intervals from the internal JS is beyond me, setInterval and setTimeout would be so useful, likewise the lack of mouse events, both those would open up entire new worlds of possibilities.

           

          Here is a thread in the InDesign forum speaking of "app.scheduleTask" using Bridge: app.scheduleTask(script, delay [, repeat])

          http://forums.adobe.com/message/3706907#3706907

           

          I tried the "app.scheduleTask" approach and never got it working in Illustrator however. <-- I hope someone proves otherwise and says it is possible in Illustrator, please let me know if you do. Otherwise I don't think there are any solutions via JS. Maybe a timer in Applescript or VBScript however running in the background and working with Illustrator externally would be an alternative.

          • 2. Re: Anything similar to javascript's setInterval in ExtendScript?
            Kashmira Level 1

            Thank you! I will try using scheduleTask and update this thread on my findings.

            • 3. Re: Anything similar to javascript's setInterval in ExtendScript?
              Kashmira Level 1

              Ok, so here is what *might* work for what I need to do:

              This wakes up every 5 seconds, checks if illustrator is busy, in which case it is still not done with the previous invocation of the illustrator script. If its idle, it runs the script. A bit roundabout, but at least something that works for me!

              Thanks for pointing me in the right direction!

               

               

              #target bridge

               

              var myScript = 'CreateBridgeTalkMessage()';

              app.scheduleTask(myScript, 50000, true);

               

              function CreateBridgeTalkMessage() {

                  var status = BridgeTalk.getStatus("illustrator");

               

                  if(status == "IDLE" || status == 'ISNOTRUNNING')

                  {

                      var myScript = IllustratorScript.toString() + '\r';

                      myScript += 'IllustratorScript();';

                      var bt = new BridgeTalk();

                      bt.target = "illustrator";

                      bt.body = myScript;

                      bt.send();

                  }

              }

               

              function IllustratorScript() {   

                 eval(readFile('C:\scripts\illustrator\batchFass.jsx'));

              }

               

              The readFile function is like this:

               

              readFile: function(filename) {

                      var file = new File(filename);

                      file.open('r');

                      return file.read();

              }

              • 4. Re: Anything similar to javascript's setInterval in ExtendScript?
                W_J_T Level 4

                Glad it was helpful to you and your efforts.

                 

                [Q]. What version of Illustrator are your using and are you calling that from Illustrator or from natively in the ExtendScript Toolkit?

                 

                I ask because I have CS5 and when I run a dumbed down version (seen below) in Illustrator I get "TypeError: app.scheduleTask is not a function", when I run it directly in the ESTK, I don't get the error but yet it does not yield anything either.

                 

                #target bridge
                var num = 0;
                var myScript = 'CreateBridgeTalkMessage()';
                app.scheduleTask(myScript, 50000, true);
                function CreateBridgeTalkMessage() {
                    var status = BridgeTalk.getStatus("illustrator");
                    if(status == "IDLE" || status == 'ISNOTRUNNING'){
                        var bt = new BridgeTalk();
                        bt.target = "illustrator";
                        bt.body = "alert(\"Alert_\"+num);";
                        bt.send();
                    }
                    num += 1;
                }
                
                • 5. Re: Anything similar to javascript's setInterval in ExtendScript?
                  Kashmira Level 1

                  I am using CS5 as well, and running it directly from ESTK. Have yet to try it from illustrator.

                  I tried your script, and I think there is some issue with the body you are sending. I tried only this:

                   

                  bt.body = "alert('hello')";

                   

                  and that worked.

                   

                  I think the issue is the parameter "num"

                   

                  I printed the one you were sending, and it was going out to illustrator as

                   

                  alert("Alert_" + num);

                   

                  Ilustrator does not know what "num" is, so I tried this:

                  bt.body = "alert('Alert_'" + num + ")";

                  It was sending the correct value for num, but illustrator still didn't like it.

                   

                  So maybe that's something you can play around with?

                   

                  -Kashmira

                  • 6. Re: Anything similar to javascript's setInterval in ExtendScript?
                    W_J_T Level 4

                    Thanks for the feedback. What a shame however that it requires 3 applications (Illustrator/ESTK/Bridge) to all be running just to invoke and use a simple interval. I think using Applescript/VBscript would be less overhead and maybe a cleaner approach myself.

                    • 7. Re: Anything similar to javascript's setInterval in ExtendScript?
                      Matthias Guntrum Level 1

                      Hi,

                       

                      I needed setInterval functionality and emulated it with the following snippet using app.scheduleTask();

                      Please note that i programmed with Adobe After Effects as my target application.

                       

                      Gtlym = new Object();

                      Gtlym.CALL = new Object();

                       

                      Gtlym.guid = function() {

                           var s4 = function() { return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); };

                           return s4() + s4() + s4() + s4()  + s4()  + s4() + s4() + s4();

                      }

                       

                      Gtlym.setInterval = function(func,millis){

                           this.guid = Gtlym.guid();

                           Gtlym.CALL["interval_"+this.guid] = func;

                           app.scheduleTask('Gtlym.CALL["interval_'+this.guid+'"]();',millis,true);

                      }

                       

                       

                      //Example:

                      Gtlym.setInterval(function(){/*your code here*/},1000);

                       

                       

                      Cheers,

                      Matthias

                       

                      gentlymad.org - Matthias Guntrum

                      3 people found this helpful