9 Replies Latest reply on Oct 3, 2015 2:45 PM by Silly-V

    ScriptUI Custom Events

    Loic.Aigon Adobe Community Professional

      Hi all,

       

      Is there any chance I can propagate custom events in ScriptUI ? I tried this sample in ESTK and it works fine. However I tried to run it in several CS apps and all my attempts raised errors. My intenttion is to create custom components that would throw events listened by the parent window. Am I asking too much ?

       

       

      function test()
      {
      
                var w = new Window('dialog');
                var btn = w.add('button');
      
                w.addEventListener ('custom', tata);
      
                btn.onClick = function()
                {
                          w.close()
                          var e = new UIEvent('custom', true, true, w, null);
                          e.data= {test:"hello"};
                          w.dispatchEvent(e)
                }
      
      
                function tata(e)
                {
                          alert(e.data.test);
                }
                w.show();
      }
      
      
      test();
      

       

      Thanks for any hint/advice,

       

      Loic

        • 1. Re: ScriptUI Custom Events
          Vamitul Level 4

          Hi loic!

          I think i remember from an ancient failed experiment i did a long time ago that Indesign does not work with custom uiEvents. Don't know about the rest of the apps.

          • 2. Re: ScriptUI Custom Events
            Bob Stucky Adobe Employee

            That whole ScriptUI event thing isn't meant to be extended. That it works in ESTK is likely an anomaly. With ScriptUI events, you can only make 1 of 3 types (keyboard events, mouse events, and...? uh...).

             

            And you do that via ScriptUI.events.Event.createEvent() (or some syntax close to that - this is from memories in the CS4 timeframe).

             

            But what you can do is write your own event system...

             

            Something like this - of course you could write an event class, pass data, you name it.

             

            See the attached file for one primitive way to do it....

            • 3. Re: ScriptUI Custom Events
              Loic.Aigon Adobe Community Professional

              Hi Bob,

               

              Thank you so much. It's a very nice solution and indeed functional. Thanks again for this one.

               

              For the record, I developed a scriptUI/SWF application for Bridge. It's ok in CS5.1 but it seems ScriptUI in Bridge CS6 doesn't support flash objects any longer

               

              So I have to translate all the swf panel into pure ScriptUI. Your solution was the first stone I needed

               

              Loic

              • 4. Re: ScriptUI Custom Events
                Loic.Aigon Adobe Community Professional

                Thanks Vamitul for your remark

                • 5. Re: ScriptUI Custom Events
                  Marc Autret Level 4

                  Hi Loïc,

                   

                  To my knowledge new UIEvent(…) works fine in InDesign (CS4/CS5/CS6) provided that you let the view parameter undefined.

                   

                  So just try to replace:

                      var e = new UIEvent('custom', true, true, w, null);

                  by:

                      var e = new UIEvent('custom', true, true);

                   

                   

                  Note: Usually a custom UI event should be dispatched from a specific target—in your case: btn.dispatchEvent(e)—rather than from the top window. This way you can have multiple listeners that the event will hit during the bubbling phase:

                   

                  // Propagation of a custom UI event (which bubbles)
                  // Tested in ID CS4/CS5/CS6
                  
                  const EV_TYPE = 'MyCustomEventType';
                  
                  var u,
                      w = new Window('dialog'),
                      p = w.add('panel'),
                      b = p.add('button',u,"Test"),
                      // ---
                      evHandler = function(ev)
                      {
                          alert( this + " is listening: " + ev.data );
                      },
                      myDispatcher = function(data)
                      {
                          var ev = new UIEvent(EV_TYPE, true, true);
                          ev.data = data;
                          this.dispatchEvent(ev);
                      };
                  
                  w.addEventListener(EV_TYPE, evHandler);
                  p.addEventListener(EV_TYPE, evHandler);
                  
                  b.onClick = function(){ myDispatcher.call(this, "Some data"); }
                  
                  w.show();
                  

                   

                  @+

                  Marc

                  2 people found this helpful
                  • 6. Re: ScriptUI Custom Events
                    Loic.Aigon Adobe Community Professional

                    Hi Marc,

                     

                    So I wasn' that far after all Thanks a lot. It's also very helpful and functional indeed. I like the idea on getting stuck to the event bubbling process.

                     

                    Best to all of you guys,

                     

                    Loic

                    • 7. Re: ScriptUI Custom Events
                      Bob Stucky Adobe Employee

                      Hey Marc, thanks for teaching me something new to me!

                       

                      If my recollection is correct, that's not supposed to work - but I'm very happy it does!!!

                      • 8. Re: ScriptUI Custom Events
                        DBarranca Level 4

                        Hi,

                        I've happened to post something along these lines in the Photoshop forum - I've troubles with dispatchEvent() and a particular component.

                        If you're willing to spread your knowledge there too, the thread is this one: http://forums.adobe.com/message/5547956#5547956

                         

                        Thank you!

                         

                        Davide

                        • 9. Re: ScriptUI Custom Events
                          Silly-V Adobe Community Professional

                          Amazing piece of vital information!