16 Replies Latest reply on Jul 23, 2010 12:01 PM by Anastasiy Safari

    Cannot call CS SDK function from javascript

    Anastasiy Safari Level 1

      Hi!

       

      I'm trying it in Illustrator. I'm trying to call the actionscript code from ExtendScript.

      I read this two articles: http://forums.adobe.com/thread/634947?tstart=0 and http://cookbooks.adobe.com/post_Communicating_between_JavaScript_and_the_CS_SDK_us-17383.h tml

       

      They all propose the same technique, like var jsxInterface:HostObject = HostObject.getRoot( HostObject.extensions[ 0 ] )

       

      I'm using the external script (not an embedded one) I added my script to manifest file:

      <ScriptPath>./script.jsx</ScriptPath>

       

      And then trying to call jsxInterface.init( this ) which is defined in my jsx file. But with no luck. I just got an exception.

      It seems like  jsxInterface.init is not defined after all (of course, I defined it in .jsx file)

       

      Is there a way to call a CS SDK function from external ExtendScript script in Illustrator? Or a way to pass "this" object

      to javascript for a later access?

       

      Thank you!

        • 1. Re: Cannot call CS SDK function from javascript
          LeeHuang Adobe Employee

          Anastasiy,

           

          Unfortunately, at least from what I've seen, we have only better 

          success with the embedded script so far.  The only app that allows me 

          to use an external script is InDesign, in that, I used CSXS's 

          evalScript to call into a ExtendScript method.  InDesign then loads 

          that script into a second extension where I can call all its methods.  

          But I just tried this technique in AI, and it didn't work.

           

          thanks!

          lee

          1 person found this helpful
          • 2. Re: Cannot call CS SDK function from javascript
            zak.nelson Level 3

            Hi Anastasiy,

             

            The HostObject technique described in the cookbook entry can be used with a script that is not embedded. The call to "jsxInterface.eval()" takes a string and you could get  this string from anywhere (e.g. reading from an external file). The example in the cookbook shows the script embedded in order to save time  reading the file (and to keep the code clean) at the expense of the SWF  size.

             

            Zak

            1 person found this helpful
            • 3. Re: Cannot call CS SDK function from javascript
              Anastasiy Safari Level 1

              Thank you for the answers!

               

              Zak, I couldn't manage to work it in Illustrator. It seems, like in Illustrator the jsxInterface.XXX is working only with embedded scripts.

              jsxInterface.eval doesn't work as well

              • 4. Re: Cannot call CS SDK function from javascript
                zak.nelson Level 3

                Can you explain what you're doing? There is nothing special about embedding the script; it's merely a way to get the script read into memory so it can be passed to jsxInterface.eval() as a string. Did you try reading in the script?

                • 5. Re: Cannot call CS SDK function from javascript
                  Anastasiy Safari Level 1

                  That's a good question, Zak

                   

                  Well, it seems you're right and the embedded way is better. I'll need to rewrite a big part of my code, but in the end it will be easier to deal with script.

                   

                  Thank you for the ideas and support!

                  • 6. Re: Cannot call CS SDK function from javascript
                    Anastasiy Safari Level 1

                    Zak, and the embedding does work only in Photoshop, Illustrator and InDesign, am I right?

                     

                    Thank you

                    • 7. Re: Cannot call CS SDK function from javascript
                      zak.nelson Level 3

                      The technique described in cookbook will work for InDesign, Illustrator, Photoshop, and Bridge. For the others, consider using CSXSInterface.

                      • 8. Re: Cannot call CS SDK function from javascript
                        Anastasiy Safari Level 1

                        I see, thank you, Zak!

                        • 9. Re: Cannot call CS SDK function from javascript
                          Anastasiy Safari Level 1

                          Zak, I rewrote my code with the new technique, so I'm embedding JSX scripts inside application.

                           

                          But now I faced (after 4 hours of trying to figure out, why my code isn't working at all)

                          a new issue that is preventing me from doing this on Photoshop CS5 on Mac OS 10.6.3 64 bit. / latest Photoshop 12.0.1 update

                           

                          When embedded, the JSX script doesn't support "app" object completely! It's the very strange issue - while there IS an app object, I cannot acess it. Every time I'm trying to get its property (app.appName, app["appName"] or even for (var s in app) { ... } ) I get an exception, which tells me "General Photoshop error occured. This functionality may not be available in this version of Photoshop".

                           

                          It works perfectly with the external script and gives an exception only when emebedded.

                           

                          Is this a known issue?

                          • 10. Re: Cannot call CS SDK function from javascript
                            Bob Stucky Adobe Employee

                            Anastasiy,

                             

                            Is the code that reliese on "app" being called from an event handler? Specifically and event handler that is fired from an OS event (not a Flex UI event)?

                             

                            Something like a network data received event, a timer event, something that hails from the OS.

                             

                            If so, that's a known issue for Mac OS (my preso at the Dev Summit touched on it briefly -  http://my.adobe.acrobat.com/p33908050/).

                             

                            There is a workaround of using Script Listener code to execute the needed functionality.

                             

                            Regards

                             

                            Bob

                            • 11. Re: Cannot call CS SDK function from javascript
                              Anastasiy Safari Level 1

                              Bob, thank you for the answer

                               

                                I got a large jsx script embedded into my app. It does a lot of work, including dealing with

                              documents, app object etc. It sets variables that are being used later on. Now, when I put

                              in this jsx a code that tries to access the "app" object, the script fails with an exception.

                               

                              Here's the example cut down to show what's happening.

                               

                              The code that evaluates the jsx code is called from CSXSWindowedApplication's applicationComplete event. The code is as follows:

                                  [  Embed (source=  "../assets/test.jsx" , mimeType=  "application/octet-stream" )]
                                          private static var _testJSX:Class;

                              .....

                              private function initApplication( e: Event ):void

                                          {

                                              var jsxManager: HostObject;

                                              jsxManager =
                              HostObject.getRoot(HostObject['extensions'][0]);

                                              jsxManager.eval( new _testJSX().toString() );

                                          }


                                 This one immediately fails on MAC 64 bit after "eval". Because it has the following code in the root:

                               

                                alert('SUCCESS! foregroundColor object = '+app.foregroundColor);

                               

                              The project files with ZXP that fails on Mac and works on Windows can be downloaded here: http://anastasiy.com/testApp.zip It's a actually a standard code with the above modifications.

                              Am I doing something wrong?

                              • 12. Re: Cannot call CS SDK function from javascript
                                Harbs. Adobe Community Professional & MVP

                                Hi Anastasiy,

                                 

                                Weird!

                                 

                                It looks like it does not work during a creationComplete event.

                                 

                                If you change your code to intitialize in a function, and call that function outside the creationComplete event, it works fine. Like so:

                                 

                                ExtendScript:

                                 

                                function initializeJSX(){
                                  try
                                  {
                                    alert('SUCCESS! foregroundColor object = '+app.foregroundColor);  } catch(e)
                                  {
                                    alert('ERROR: '+e.description);
                                  }
                                }
                                

                                 

                                MXML:

                                 

                                <?xml version="1.0" encoding="utf-8"?>
                                <csxs:CSXSWindowedApplication
                                    xmlns:mx="http://www.adobe.com/2006/mxml"
                                    xmlns:csxs="com.adobe.csxs.core.*"
                                    layout="absolute" historyManagementEnabled="false"
                                    creationComplete="initApplication(event)">
                                    <mx:Script>
                                        <![CDATA[
                                            [  Embed (source=  "../assets/test.jsx" , mimeType=  "application/octet-stream" )]
                                            private static var _testJSX:Class;
                                            [Bindable]
                                            private var hostName:String = HostObject.mainExtension;
                                            private var jsxManager: HostObject;
                                            private function initApplication( e: Event ):void
                                            {
                                                jsxManager = HostObject.getRoot(HostObject['extensions'][0]);
                                                jsxManager.eval( new _testJSX().toString() );
                                            }
                                        ]]>
                                    </mx:Script>
                                    <mx:VBox height="100%" width="100%" verticalAlign="middle" horizontalAlign="center">
                                        <mx:Button label="Run PS code" click="jsxManager.initializeJSX()" enabled="{hostName.indexOf('photoshop') > -1}"/>
                                    </mx:VBox>
                                </csxs:CSXSWindowedApplication>
                                

                                 

                                I presume you can get what you what by calling the function via a timer or some other event...

                                 

                                Harbs

                                • 13. Re: Cannot call CS SDK function from javascript
                                  Anastasiy Safari Level 1

                                  Thank you Harbs! It's a very tricky solution It will work, but I'll have to rewrite a lot of code again.

                                  After that call I got a very big piece of code that is needed to be synchronized with other parts, and if I make it a timeout-call... a complexity of the code will rise enormousely

                                   

                                  Anyway, as I can see it's a bug. I'll add it to the list

                                  • 14. Re: Cannot call CS SDK function from javascript
                                    Harbs. Adobe Community Professional & MVP

                                    What happens if you finish the initializing on a PANEL_OPEN event?

                                     

                                    The other question is if it's a problem on creationComplete per se, or it's a problem accessing app right after the script is evaled. Maybe try eval() in an init event, and seeing if that helps?

                                     

                                    Good luck!

                                    Harbs

                                    • 16. Re: Cannot call CS SDK function from javascript
                                      Anastasiy Safari Level 1

                                      Well, Harbs, if I'm using PANEL_OPEN the initialization of the interface is seen when panel is opened

                                      - buttons are changing, text changin etc. With creationComplete the panel opens just as smoothly as it can be.

                                       

                                      The  eval doesn't work either. The app object is completely unaccesible in  the embedded script in the creationComplete event (on Mac only). As Bob  said, there are other events that make it unaccesible as well.

                                       

                                      The  workaround you mentioned is cool, but I won't be able to understand my  code after this and other workarounds. That will be so cool, if Adobe  fixes this and other issues with the next updates.

                                       

                                      Thank you!