Passing a simple array using CSXSInterface

General Discussion of Scripting for Flex, Flash & CS SDK

Moderators: Tom, Kukurykus

Mike Hale

Passing a simple array using CSXSInterface

Post by Mike Hale »

Getting a simple array from Extendscript using CSXSInterface is something that I had trouble doing. The examples in the documentation where not helpful to me and a Google search was also unproductive. With the help of Tom Ruark I now know how and thought I would post an example for any one else who also is having trouble with arrays.

This example shows an easy way to send an array and display the contents of the array in a datagrid. The array is an array of strings. The Extendscript is below.Code: Select allvar layersNames = new Array();

function findLayersName( theParent ) {
    for ( var m = theParent.layers.length - 1; m >= 0; m-- ) {
        var theLayer = theParent.layers[ m ];
        if ( theLayer.typename != "LayerSet" ) {
            layersNames.push( theLayer.name );
        }else {
            layersNames.push( theLayer.name );
            findLayersName( theLayer );
        }
    }
    return layersNames;
};
function getNames(){
    var names = findLayersName( app.activeDocument );
    return '<object><property id = "names" ><string>'+names().toString()+'</string></property></object>';
};It just gets an array of layer names and wraps them in a XML string.

The mxml looks like this.Code: Select all<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="300" height="300" creationComplete="init()">
   <mx:Script>
      <![CDATA[
         import com.adobe.csxs.core.CSXSInterface;
         import com.adobe.csxs.events.*;
         import com.adobe.csxs.types.*;
         import mx.collections.*

           public function getNames():void{
            var reqResult:SyncRequestResult = CSXSInterface.instance.evalScript("getNames");
            if (SyncRequestResult.COMPLETE == reqResult.status) {
               var temp:String = reqResult.data.names
               var tmpArray:Array = temp.split(',');
               layerNames.dataProvider = tmpArray;
            }
         }

         public function init():void{
            getNames();
         }
      ]]>
   </mx:Script>
   <mx:DataGrid width="180" id="layerNames" editable="false" height="110">
      <mx:columns>
         <mx:DataGridColumn headerText="Layers" dataField="col1"/>
      </mx:columns>
   </mx:DataGrid>
</mx:Application>You can also send the array as a XML object if you escape the embedded XML and you can use arrayCollection as the dataProvider but I wanted to post a simple example.