5 Replies Latest reply: Jul 9, 2012 1:21 AM by PhotoSì RSS

    Accessing Photoshop object model from event listeners

    Anatoly Paraev Community Member
      I need to solve a very basic problem - I need to know which document is currently selected in Photoshop at any given time.
      There are a number of ways to do this, namely:
      1) Subscribe for Photoshop events using CSXSInterface(i.e. using CSXSInterface::addEventListener) and then, in the event handler, access the active document property using CSAW object model (i.e. com.adobe.csawlib.photoshop.Photoshop.app.activeDocument).
      2) Subscribe for PS events using ExtendScript interface(i.e. evalScript("PhotoshopRegisterEvent")) and then, in the event handler, access the Photoshop object model using the ExtendScript interface (by calling app.activeDocument.name from ExtendScript).
      3)  Subscribe for Photoshop events using CSXSInterface(i.e. using CSXSInterface::addEventListener) and then, in the event handler, access the Photoshop API using ActionDescriptors API (executeAction and executeActionGet APIs).

      Unfortunately there is a major bug in Photoshop CS5 on Mac that makes Photoshop document model inaccessible from all OS-initiated event handlers. This bug seems to affect all the methods listed above. As a result, none of them works with Photoshop CS5 on Mac.

       

      If I subscribe for CSXSInterface, I am able to access the activeDocument property of the com.adobe.photoshop.Application object, but the event comes in before the active document has changed, not after, so I cannot find out which document was selected.
      I tried adding a timer in ActionScript and checking the active document a second after the event, but from the activeDocument property is not accessible from the timer handler.

       

      If I try to use extendScript to access Photoshop DOM  inside an event handler, I get the following error: “Error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.”.
      The same error occurs when I try to use executeActionGet API. I suspect that executeActionGet calls ExtendScript code under the hood, so I get the same result.

       


      So, is there any way to use Photoshop API from an event listener on Mac CS5? Can some Adobe employee please comment on this situation?
      Thank you in advance,
      Anatoly
        • 1. Re: Accessing Photoshop object model from event listeners
          Bob Stucky Adobe Employee

          Anatoly,

           

          Your timing is impeccable. We have managed to come up with a viable workaround for the PS Mac event issue. We literally have it ready as of today.

           

          I have attached both an SWC that contains the workaround and a sample project to show you how to use it.

           

          There are a couple of caveats -

          • This workaround has minimally tested. Use at your own risk. We have not stress tested at all.
          • Each time the workaround is invoked it requires roughly 0.02 seconds. While not significant by itself, they can add up, depending on how you use it.

           

           

          The sample project should be enough to show you how to use it.

           

          Feel free to post questions as they come up.

           

          Regards

           

           

          Bob

          • 2. Re: Accessing Photoshop object model from event listeners
            Anatoly Paraev Community Member

            Bob,

            Thanks a lot for this workaround.

            It seems to work, so thank you very much!

             

            The only problem I've found so far - the second approach (with PsEventScrubber.getInstance().forceClean(...)) only works once.

            If I try to handle the same event twice using forceClean, I get the same "General Photoshop error occurred. This functionality may not be available in this version of Photoshop." error as before.

             

            To reproduce the problem, you can just invoke the timer in the secondDemo function twice:

            public function secondDemo( evt: TimerEvent ): void

            {

            //instead of invoking the timer below just once, try to invoke it twice

            var timer: Timer = new Timer( 1000, 2);

            timer.addEventListener( TimerEvent.TIMER, secondHandler );

            timer.start();

            trace( "timer started for second demo" );

            }

             

            Do I need to call some cleanup code after I use forceClean?

             

            However the first approach seems to work fine.

             

            Also, will it be possible for you to briefly explain what PsEventScrubber library does?

            Thank you once again,

            Anatoly

            • 3. Re: Accessing Photoshop object model from event listeners
              Bob Stucky Adobe Employee

              Your post confused me as I was seeing nothing like that here. I had a "versioning problem". The code on my Mac (where I did most of the testing) had one more (very important) line of code. Sorry. The attached library should fix the problem you're having with forceClean.

               

              As for what the workaround does:

               

              Ps DOM calls worked just fine when called from the context of a UI event. The only issue was when the event was a system-type event. What we were looking for was a way to end the event chain of the system event and yet continue executing the handler.

               

              Early on we found that you could execute a script using CSXSInterface.getInstance().evalScript - and that script could call PS DOM functions.

               

              The happy thought was to call into evalScript, to a script that used a HostObject to call back to ActionScript.

               

              Regards

               

              Bob

              • 4. Re: Accessing Photoshop object model from event listeners
                Anatoly Paraev Community Member

                Thanks Bob,

                The second approach works now.

                 

                Anatoly

                • 5. Re: Accessing Photoshop object model from event listeners
                  PhotoSì

                  I'm just writing here to know if the PsMacDom project still open.....we have few problems with it....and i don't know where ask help....

                  Thanks.

                  Diego