7 Replies Latest reply on Feb 22, 2009 2:34 AM by sstanleyau

    Arguments in called scripts

    Level 1
      I have a notion that I want to write a script that is:

      (a) capable of being called by another script that passes it arguments
      (b) capable of running stand alone
      (c) is written as an anonymous function

      Why an anonymous function? Because when scripts are calling each other you have to protect their namespaces -- perhaps I could do that by putting each script into a different engine, but for the moment, let's stay with my premise.

      Why capable of running stand alone? Because it might do something useful that the user could choose to do separately from the calling script. And, it sure helps debugging if you can run it stand alone for testing.

      But I haven't been able to work out how to pass the script's arguments to the anonymous function. Is this impossible? Or am I missing something obvious. Here's where I am at this moment.
      //DESCRIPTION: Calling Script
      
      (function() {
        myScriptFile = File(getScriptFile().parent + "/calledScript2.jsx");
        result = app.doScript(myScriptFile, undefined, [3,4]);
        alert(result);
        function getScriptFile() {
          // This function returns the file of the active script, even when running ESTK
          try {
            return app.activeScript;
          } catch(e) {
            return File(e.fileName);
          }
        }
      }())

      //DESCRIPTION: Called Script
      
      (function (a){
        if (a == null) return 0
        var sum = 0;
        for (var j = 0; a.length > j; j++) {
          sum += a[j];
        }
      return  sum
      }(arguments != undefined ? arguments : null))
      This fails dismally. The "called script" returns a value of 0 when called by the other script but fails to run stand-alone with the error "arguments is undefined".

      If I change the called script to:
      //DESCRIPTION: Called Script
      
      sum = 0;
      for (var j = 0; arguments.length > j; j++) {
        sum += arguments[j];
      }
      sum
      It still doesn't work!!!! This whole mechanism only seems to function properly if both scripts are not anonymous functions. Change the calling script to:
      //DESCRIPTION: Calling Script
      
      myScriptFile = File(getScriptFile().parent + "/calledScript2.jsx");
      result = app.doScript(myScriptFile, undefined, [3,4]);
      alert(result);

      function getScriptFile() {
        // This function returns the file of the active script, even when running ESTK
        try {
          return app.activeScript;
        } catch(e) {
          return File(e.fileName);
        }
      }
      And now I can run the calling script and get an alert that says 7. But the called script can't be run stand-alone and their namespaces aren't protected from each other (not important in this trivial example, but definitely necessary in some of the more complex stuff I've done where I haven't bothered with trying to pass arguments in this fashion).

      Could it be that in the original calling script the problem is that the call is putting the arguments value into the arguments of the anonymous function that contains the doScript rather than passing them with the call to the other script?

      Hmm. I'm supposed to be getting ready to fly to Australia. This isn't helping!
        • 1. Re: Arguments in called scripts
          Larry G. Schneider Adobe Community Professional & MVP
          Dave,

          You'll have the better part of 24 hours on the plane to figure this out (that is if you make the plane).
          • 2. Re: Arguments in called scripts
            Level 1
            Hi Dave,

            This is a known ExtendScript problem for *any* function, not just anonymous functions. It even seems to me that I've written about it here before. Passing parameters to a script doesn't work inside functions--or, more exactly, the parameters passed are the same as those passed to the function itself. So it works fine from the global body of a script but not from inside a function.

            I don't like it very much, either.

            Thanks,

            Ole
            • 3. Re: Arguments in called scripts
              Level 1
              Thanks Ole.

              What about the second issue of detecting whether or not there are arguments there at the global level? Is there a way of detecting (other than try/catch)?

              Dave
              • 4. Re: Arguments in called scripts
                Harbs. Level 6
                > Is there a way of detecting (other than try/catch)?
                >

                if(typeof arguments == "undefined"){arguments=[3,4]}

                --
                Harbs
                http://www.in-tools.com
                • 5. Re: Arguments in called scripts
                  Level 1
                  Thanks Harbs,

                  I've done some more experimenting (on the plane heading for LA; at least there was room on that plane) and I've concluded that this argument passing feature of CS4 is more trouble than it is worth. There are lots of ways already for scripts to communicate with each other, so passing arguments in this restrictive fashion isn't worth it.

                  Dave
                  At Noosa, about 60 miles north of Brisbane
                  • 6. Re: Arguments in called scripts
                    sstanleyau Level 4
                    On 22/2/09 4:57 PM, "Dave Saunders" <member@adobeforums.com> wrote:<br /><br />> At Noosa, about 60 miles north of Brisbane<br /><br />Oooh, you are slumming it ;-)<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                    • 7. Re: Arguments in called scripts
                      Harbs. Level 6
                      > There are lots of ways already for scripts to communicate with each other, so passing arguments in this restrictive fashion isn't worth it.
                      >

                      Agreed.

                      My personal favorite is using APID dataStores (which of course assumes
                      APID is installed). It's a solution which works in all versions of
                      InDesign back to CS, and works across different scripting engines.
                      DataStores can accept any kind of data -- including object references.
                      The only limitation there is you can't pass custom js objects.

                      Using global variables in private scripting engines is another good
                      method. With using global variables if(typeof variable == "undefined")
                      is very useful...

                      --
                      Harbs
                      http://www.in-tools.com