4 Replies Latest reply on Mar 3, 2011 8:10 PM by John Hawkinson

    [JS CS4] Alternative to doScript(): execute a script without returning

    PaulR72

      Hello all,

       

      I'm looking for an alternative to doScript() for executing another scipt. This is because I'll be calling many scripts, some of them more than once, in a long chain. When one script has done its bit, at the end it will execute the next one, and so on. The problem with doScript is after it executes the new script, command passes back to the original script. I imagine I'll be chaining about 30 scripts, some of them being called more than once, so I hoped there was a function that would execute a script and not pass control back to the calling script. This is because I suspect that would slow things down a bit, or at any rate be messy. Anyone know of such a thing? Or should I resort to making one large script by using lots of #include statements?

       

      Regards, Paul

        • 1. Re: [JS CS4] Alternative to doScript(): execute a script without returning
          John Hawkinson Level 5

          I don't believe there is a way to pass control in the fashion you specify, no.

           

          It also seems a strange way to structure programs -- it makes them hard to read and hard to debug.

          Make your subsidiary scripts functions and make one script that calls all of those functions sequentially (or in whatever order).

           

          One big script with #includes seems fine. Or just one big script.

           

          Though remember the standard caution about premature optimization. Don't worry about the speed until it becomes a problem. Try it first and see if you actually need to worry, of it is just fine as it is.

          1 person found this helpful
          • 2. Re: [JS CS4] Alternative to doScript(): execute a script without returning
            PaulR72 Level 1

            Thanks for your answer, John.

             

            There is a reason for structuring the code in (I agree) what appears to be a bizarre way. It's because the scripts will be functioning on different books that customers give me. I can't count on each script working properly from job to job. Often a script will terminate, selecting an object in the doc that's caused trouble and needs to be manually resolved, before I can continue applying that script and then the others. Having them as separate, self-contained scripts means I can apply each function to the script then see what it's done before proceeding.

             

            I think though that I'll use #includes and if the script stops, and I need to sort something out then run it again, I can comment out those #includes that have already successfully executed.

             

            Thanks again,

             

            Paul

            • 3. Re: [JS CS4] Alternative to doScript(): execute a script without returning
              John Hawkinson Level 5

              Well, I would probably do something like:

               

              function functionone() {
              ...
              }
              
              function functiontwo() {
              ...
              }
              ...
              
              function main() {
                  var
                      i,
                      dispatch = [
                         functionone,
                         functiontwo,
                         functionthree, // ...
                      ],
                      start = 0;
              
                  for (i=start; i<dispatch.length, i++) {
                      try {
                          dispatch[i]();
                      } catch (e) {
                          alert("Failed running function #"+i);
                          return false;
                      }
                  }
              }
              
              main();
              

               

              And then change start to the right number to restart the whole thing in the middle.

               

              But also, I think you'll find that if functionone() calls functiontwo() as the last thing it does, and likewise functiontwo() calls functionthree(), that while your call stack depth will increase, it will work just fine.

               

              Edit: added return and call to main().

              1 person found this helpful
              • 4. Re: [JS CS4] Alternative to doScript(): execute a script without returning
                PaulR72 Level 1

                Thanks John -- seems like a really good way to go. Will adopt it. There's a couple of thousand lines of code, so I'll still use #includes.

                 

                Cheers,

                 

                Paul