Thread View: adobe.scripting.indesign
4 messages
4 total messages
Started by Dave_Saunders
Thu, 19 Feb 2009 13:00
Arguments in called scripts
Author: Dave_Saunders
Date: Thu, 19 Feb 2009 13:00
Date: Thu, 19 Feb 2009 13:00
72 lines
3593 bytes
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!
Re: Arguments in called scripts
Author: Dave_Saunders
Date: Sun, 22 Feb 2009 05:57
Date: Sun, 22 Feb 2009 05:57
6 lines
986 bytes
986 bytes
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
Re: Arguments in called scripts
Author: Shane_Stanley
Date: Sun, 22 Feb 2009 10:34
Date: Sun, 22 Feb 2009 10:34
9 lines
1470 bytes
1470 bytes
On 22/2/09 4:57 PM, "Dave Saunders" <member@adobeforums.com> wrote: > At Noosa, about 60 miles north of Brisbane Oooh, you are slumming it ;-) -- Shane Stanley <sstanley@myriad-com.com.au> AppleScript Pro Florida, April 2009 <http://scriptingmatters.com/aspro>
Re: Arguments in called scripts
Author: Harbs
Date: Sun, 22 Feb 2009 11:55
Date: Sun, 22 Feb 2009 11:55
19 lines
1386 bytes
1386 bytes
> 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads