Passing a param to a JavaScript in Java using myApp.doScript
Copy link to clipboard
Copied
I'm trying to pass a very simple string parameter to a JavaScript within my Java code. I am using the example DoRunScript.java that was given in the CS3 SDK:
... (here's a snippet of the code)
//call the javascript, passing in the arguments
VariableType returnValue = myApp.doScript (
VariableTypeUtils.createFile (scriptFilePath),
OptArg.makeScriptLanguageEnum (kScriptLanguageJavascript.value), argSeq);
My question is how do I retrieve the value of the parameter in my Javascript? I gather that I must use something like the following, but I'm not sure what to use as the name.
var myArg = app.scriptArgs.get ("name");
Am I going about this the wrong way? Eventually, I would like to pass an array or an ArrayList. Could I do this?
Thanks for your help!
Jennifer
Copy link to clipboard
Copied
I created the following script:
name = app.scriptArgs.get("name");
app.consoleout("Hello, " + name);
and then ran it using the following command line:
sampleclient -host localhost:5000 ~/Desktop/test.js name=mike
Copy link to clipboard
Copied
Here's what I have in "argSeq".
String param1 = new String ("TestParam");
vtArgs[0] = VariableTypeUtils.createString(param1);
argSeq = OptArg.makeVariableTypeSeq(vtArgs);
In my JavaScript, I tried to retrieve the value using the following, but it doesn't work.
var myArg1 = app.scriptArgs.getValue ("param1");
Thanks again for your help.
Jennifer
Copy link to clipboard
Copied
It should probably be something like this:
String param1[2] = new String({"param1", "TestParam"});
...
Copy link to clipboard
Copied
Thanks for your reply!
Jennifer
Copy link to clipboard
Copied
1) If your script is run by calling RunScript (via SOAP), parameters are stored in app.scriptArgs, and are passed to the script in an array of name/value pairs.
Called using SampleClient:
sampleclient -host localhost:12345 c:\myScript.jsx arg0=5
Accessing params in the script:
var myVal = app.scriptArgs.getValue("arg0");
2) If you are calling your script using doScript (via Scripting or Java), your parameters are accessed using the variable "arguments", and are passed to the script in a flat array.
Called from Java:
VariableType [] vtArgs = new VariableType[1];
vtArgs[0] = VariableTypeUtils.createString("5");
OptVariableTypeSeq argSeq = OptArg.makeVariableTypeSeq(vtArgs);
VariableType returnValue = myApp.doScript(
VariableTypeUtils.createFile("c:\myScript.jsx"),
OptArg.makeScriptLanguageEnum(kScriptLanguageJavascript.value),
argSeq,
OptArg.noUndoModesEnum(),
OptArg.noString());
Accessing params in the script:
var myVal = arguments[0];
Here is a script you can use to test where your parameters are stored (note that arguments is undefined if script is run via SOAP):
var myScriptArgs = "";
// replace "arg0" with the name of your parameter
if (app.scriptArgs.isDefined("arg0")) {
myScriptArgs = "arg0 = " + app.scriptArgs.getValue("arg0");
}
var myArguments = "";
for ( i = 0; i < arguments.length; i++) {
myArguments = myArguments + " arguments[" + i + "] = " + arguments;
}
var theResult = "ScriptArgs = " + myScriptArgs + " arguments = " + myArguments;
theResult; // returns value of theResult to caller
Copy link to clipboard
Copied
Thanks for your response. I'm trying to do what you describe in item 2; however, my main problem is I do not know how to specify the name of the parameter in my Java class in order to use it to retrieve the value of the param in my JavaScript. For example, you say to replace arg0 with the name of my parameter. My question is I don't know how to give it a name in the Java class before passing it to the JavaScript via doScript. I'm probably missing something!
Thanks,
Jennifer
Copy link to clipboard
Copied
var arg0 = arguments[0];
var arg1 = arguments[1];
You probably need to rewrite your script to use the "arguments" array instead of scriptArgs.
Susan
Copy link to clipboard
Copied
Susan, this doesn't work. I have the same problem. described here http://forums.adobe.com/thread/953607?tstart=0.
Problem seems to be trivial, but nobody can give us right answer...
Please help.
K.
Copy link to clipboard
Copied
When i am running the following code i am getting the exception as
com.adobe.ids.IdsException: IDL:com/adobe/ids/IdsException:1.0
VariableType[] vtArgs = new VariableType[1];
vtArgs[0] = VariableTypeUtils.createString("5");
OptVariableTypeSeq argSeq = OptArg.makeVariableTypeSeq(vtArgs);
if (myApp != null) {
try {
myApp.doScript(
VariableTypeUtils.createFile("C:/Program Files/Adobe/Adobe InDesign CS5.5/Scripts/Scripts Panel/Samples/JavaScript/CreateImage.jsx"),
OptArg.makeScriptLanguageEnum(kScriptLanguageJavascript.value),
argSeq,
OptArg.noUndoModesEnum(),
OptArg.noString());
}
catch (IdsException exc) {
System.err.println("Exception #" + exc.errorCode + ": "
+ exc.errorMsg);
exc.printStackTrace();
}
Please solve my problem
Thanks,
Siva

