Skip navigation
Currently Being Moderated

Passing a param to a JavaScript in Java using myApp.doScript

Jul 15, 2008 12:09 PM

Hi!

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
 
Replies
  • Currently Being Moderated
    Jul 15, 2008 12:56 PM   in reply to (Jennifer_Cross)
    You are generally going about this correct. You don't show the code for creating the argSeq so I don't know what is in there.

    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
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 15, 2008 2:03 PM   in reply to (Jennifer_Cross)
    Each argument should be a name, value pair. I think an array of 2. It looks like to me that the java code is not creating a name for each value.

    It should probably be something like this:

    String param1[2] = new String({"param1", "TestParam"});
    ...
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 18, 2008 1:08 PM   in reply to (Jennifer_Cross)
    There are two ways to access parameters passed to your script.

    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[i];
    }

    var theResult = "ScriptArgs = " + myScriptArgs + " arguments = " + myArguments;

    theResult; // returns value of theResult to caller
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 21, 2008 9:48 AM   in reply to (Jennifer_Cross)
    When calling a script from Java using doScript, your parameters are sent in an array of values - no names are associated with the parameters. To access the parameters in your script, you access the "arguments" array like this:

    var arg0 = arguments[0];
    var arg1 = arguments[1];

    You probably need to rewrite your script to use the "arguments" array instead of scriptArgs.

    Susan
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2011 10:32 PM   in reply to (Jennifer_Cross)

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 24, 2012 5:49 AM   in reply to (SusanDoan)

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)