9 Replies Latest reply: Jan 24, 2012 5:49 AM by hxkris-dev RSS

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

      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
        • 1. Re: Passing a param to a JavaScript in Java using myApp.doScript
          Community Member
          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
          • 2. Re: Passing a param to a JavaScript in Java using myApp.doScript
            Mike,

            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
            • 3. Re: Passing a param to a JavaScript in Java using myApp.doScript
              Community Member
              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"});
              ...
              • 4. Re: Passing a param to a JavaScript in Java using myApp.doScript
                Michael,

                Thanks for your reply!

                Jennifer
                • 5. Re: Passing a param to a JavaScript in Java using myApp.doScript
                  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
                  • 6. Re: Passing a param to a JavaScript in Java using myApp.doScript
                    Susan,

                    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
                    • 7. Re: Passing a param to a JavaScript in Java using myApp.doScript
                      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
                      • 8. Re: Passing a param to a JavaScript in Java using myApp.doScript
                        sivaciti

                        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

                        • 9. Re: Passing a param to a JavaScript in Java using myApp.doScript
                          hxkris-dev

                          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.