adobe-indesignrun-scriptindesign-server

How to write inresponse in indesign scripts


I am running this script for getting all form fields of a indd file.

var _ALL_FIELDS = "";

var allFields = myDocument.formFields;

for(var i=0;i<allFields.length;i++){

     var tf = allFields[i];

     alert(tf.id);

     alert(tf.label);

     alert(tf.name);

     alert(_ALL_FIELDS = _ALL_FIELDS +\",\"+ tf.name);

}

What i have done is, created a soap-java based client and calling the runscript method. Now i am able to get these fields but how to send these fields back to the client i.e. how to write this in response and then at client side how to read it from response.

Code for calling runscript method is:-

Service inDesignService = new ServiceLocator();

ServicePortType inDesignServer = inDesignService.getService(new URL(parsedArgs.getHost()));

IntHolder errorNumber = new IntHolder(0);

StringHolder errorString = new StringHolder();

DataHolder results = new DataHolder();

inDesignServer.runScript(runScriptParams, errorNumber, errorString, results);

Also I have found in docs that runScript method returns RunScriptResponse but in my case it's returning void.

http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/indesign/sdk/cs6/server/ids-solutions.pdf


Solution

  • It looks like you want to return an array of formField names. You could take advantage of the everyItem() collection interface method in your javascript:

    var result = myDocument.formFields.everyItem().name;
    result;
    

    The javascript will return the last value in the called script, so just to make it completely obvious, the last line is just the value to be returned.

    On the Java side, the runScript method is passing the results variable as the 4th parameter, and that's where you'll find your response. So, after your code snippet, you might have something like this:

    List<String> formFieldNames = new ArrayList<String>();
    if (results.value.getData() != null) {
        Data[] resultsArray = (Data[]) results.value.getData();
        for (int i = 0; i < resultsArray.length; i++) {
            formFieldNames.add(resultsArray[i].getData().toString()); 
        }
    }