0
Arguments in called scripts
Participant
,
/t5/indesign-discussions/arguments-in-called-scripts/td-p/1133633
Feb 19, 2009
Feb 19, 2009
Copy link to clipboard
Copied
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.
If I change the called script to:
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!
(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 ScriptThis 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".
(function (a){
if (a == null) return 0
var sum = 0;
for (var j = 0; a.length > j; j++) {
sum += a;
}
return sum
}(arguments != undefined ? arguments : null))
If I change the called script to:
//DESCRIPTION: Called ScriptIt still doesn't work!!!! This whole mechanism only seems to function properly if both scripts are not anonymous functions. Change the calling script to:
sum = 0;
for (var j = 0; arguments.length > j; j++) {
sum += arguments;
}
sum
//DESCRIPTION: Calling ScriptAnd 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).
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);
}
}
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!
TOPICS
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/arguments-in-called-scripts/m-p/1133634#M270016
Feb 19, 2009
Feb 19, 2009
Copy link to clipboard
Copied
Dave,
You'll have the better part of 24 hours on the plane to figure this out (that is if you make the plane).
You'll have the better part of 24 hours on the plane to figure this out (that is if you make the plane).
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Explorer
,
/t5/indesign-discussions/arguments-in-called-scripts/m-p/1133635#M270017
Feb 19, 2009
Feb 19, 2009
Copy link to clipboard
Copied
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
_Dave_Saunders_
AUTHOR
Participant
,
/t5/indesign-discussions/arguments-in-called-scripts/m-p/1133636#M270018
Feb 19, 2009
Feb 19, 2009
Copy link to clipboard
Copied
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/indesign-discussions/arguments-in-called-scripts/m-p/1133637#M270019
Feb 19, 2009
Feb 19, 2009
Copy link to clipboard
Copied
> Is there a way of detecting (other than try/catch)?
>
if(typeof arguments == "undefined"){arguments=[3,4]}
--
Harbs
http://www.in-tools.com
>
if(typeof arguments == "undefined"){arguments=[3,4]}
--
Harbs
http://www.in-tools.com
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
_Dave_Saunders_
AUTHOR
Participant
,
/t5/indesign-discussions/arguments-in-called-scripts/m-p/1133638#M270020
Feb 21, 2009
Feb 21, 2009
Copy link to clipboard
Copied
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
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
LATEST
/t5/indesign-discussions/arguments-in-called-scripts/m-p/1133640#M270022
Feb 22, 2009
Feb 22, 2009
Copy link to clipboard
Copied
> 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
>
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Advocate
,
/t5/indesign-discussions/arguments-in-called-scripts/m-p/1133639#M270021
Feb 22, 2009
Feb 22, 2009
Copy link to clipboard
Copied
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>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

