🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #1194

Arguments in called scripts

#1194
From: Dave_Saunders
Date: Thu, 19 Feb 2009 13:00
72 lines
3593 bytes
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!
Technical Details
Message-ID: <59b7fbaf.-1@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newshub.sdsu.edu!webcrossing

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,6bc17fce89208641,start
X-Google-Attributes: gidf939a,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: Dave_Saunders@adobeforums.com
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newshub.sdsu.edu!webcrossing
Newsgroups: adobe.scripting.indesign
Subject: Arguments in called scripts
Message-ID: <59b7fbaf.-1@webcrossing.la2eafNXanI>
Date: Thu, 19 Feb 2009 05:00:17 -0800
Lines: 80
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit