0 Replies Latest reply on Nov 12, 2014 9:57 AM by mlavie

    Calling JSX from ActionScript: Problems with Returning InDesign Objects Back to ActionScript

    mlavie Level 1

      I have written an InDesign Extension using EB 2.1 for InDesign versions 5-6. That means Flex SDK 3.4. I need to call JSX from ActionScript, and for the JSX to return as a return value InDesign objects and regular (non-InDesign specific) Arrays as return variables.

       

      I am calling JSX using the HostObject as follows:

       

      public class AS2JSInDesign

      {

            [Embed(source="InDesign.jsx", mimeType= "application/octet-stream" )]

            private static var _inDesignJsxProxyClass:Class; 

            private static var _inDesignJsxInterface:HostObject;

            _inDesignJsxInterface = HostObject.getRoot(HostObject.extensions[0]);

            _inDesignJsxInterface.eval(new _inDesignJsxProxyClass().toString());

       

           function static doJSX():void

           {

                // Not passing the ActionScript InDesign.app

                var result1:* = _inDesignJsxInterface.myJSXFunction();

       

                // Passing the ActionScript InDesign.app as a parameter to JSX

                var InDesignApp:Application = InDesign.app;

                var result2:* = _inDesignJsxInterface.myJSXFunction(InDesignApp);

           }

      }

       

      1. When JavaScript creates InDesign objects using paradigm app.<CollectionName>.add() (e.g.,”app.documents.add()”), then the created object cannot be successfully returned. For example, for
        return app.documents.add()
        the returned value is not a Document, but rather a HostObject which contains various properties not at all relevant to an InDesign Document.
        However, if I pass the InDesign.app from ActionScript as a parameter (called "InDesignApp"), and the JavaScript uses that app object instead of its own default app object:
        return InDesignApp.documents.add(),
        then it works fine - a real InDesign Document object is returned.
      2. On the other hand, there are instances in which it seems I must use the JSX native InDesign app object. If I want to define a new Color for the active Document with the InDesign.app (passed as a parameter called "InDesignApp") using 
        var color = InDesignApp.activeDocument.colors.add(),
        then the attempt to assign
        color.space = ColorSpace.CMYK
        fails, as ColorSpace.CMYK is not recognized at run time.
        If I use the JSX app object to do this (var color = app.activeDocument.colors.add()), I don’t get an error, but if I return the color object to ActionScript, an empty HostObject is received.
      3. A native JSX Array ( var myArray = new Array(); ) cannot ever be returned to ActionScript – it appears in ActionScript as a HostObject. ActionScript doesn’t regard it as an Array. I came up with a hideous hack: I create an array using InDesignApp.documents.everyItem().getElements(), empty the array which is returned, and then use the empty area for whatever I need. Such an array, when returned to ActionsScript, is recognized as an Array.

       

      Could someone please explain what I'm doing wrong?

       

      TIA,

      mlavie