7 Replies Latest reply on Aug 2, 2016 4:30 PM by oleh.melnyk

    Custom Event Listener (for Color Theme Tool)

    oleh.melnyk Level 3

      Problem description: While switching to "Color Theme Tool" selected text becomes deselected. I have already reported that to Adobe as a bug, and bug report received status "ToFix" which means that some day it probably will be fixed... Meanwhile, I thought that this might be fixed with a script.

       

      I already made a script that can save selected text and restore selection after text were deselected:

      function TextSelection(){

          this.storedSelection;

         

          function _isText(){

              return app.selection[0].hasOwnProperty("length");

          }   

         

          // Stores text selection in variable   

          this.store = function(){

              if(_isText() && app.selection[0].length !== 0){

                  this.storedSelection = app.selection[0].contents;   

              }       

          }

          

          // Deselects text

          this.deselect = function(){

              if(_isText() && app.selection[0].length !== 0){

                  app.selection[0].insertionPoints[-1].select();

              }       

          }

       

       

          // Restores stored text selection

          this.restore = function(){

              if(_isText() && this.storedSelection){

                  app.findGrepPreferences.findWhat = this.storedSelection;

                  app.select(app.findGrep());

                  app.findGrepPreferences = null;       

              }

          }

      }

       

       

      // init object

      dvar textSelection = new TextSelection();

       

      textSelection.store(); // store selection

      textSelection.deselect(); // deselect text

      textSelection.restore(); // restore selection


      And we can get currently selected tool like that:

      app.toolBoxTools.currentToolName

       

      In JavaScript, we can write custom event listeners - Creating and triggering events - Web developer guides | MDN
      And seems like Abobe allow us to do that since app.toolBoxTools contains events and eventListeners objects (they are empty by default)

       

      So, the idea is to create a custom event listener, that will listen for tool changes and call textSelection.store() method, then another eventListener - afterSelectionChanged - will check if currently selected tool is "Color Theme Tool" - then call textSelection.restore() method

       

      The problem is that I don't know how to implement custom event listeners in InDesign... Can anybody help?

        • 1. Re: Custom Event Listener (for Color Theme Tool)
          oleh.melnyk Level 3

          ToolBoxTools.addEventListener only works every second time seems to be what I was looking for, but still can't make everything work as expected

          • 2. Re: Custom Event Listener (for Color Theme Tool)
            oleh.melnyk Level 3

            Ok, it's almost working!

             

            It now remembers selection while switching between Type Tool and Color Theme Tool, but when switching between Eyedropper Tool and Color Theme Tool with the mouse - selection still gets lost...

            I made custom shortcuts, that works only for text - Eyedropper Tool - Alt+I, and Color Theme Tool - Ctrl + Shift + I and this allows me to switch with keyboard shortcuts without loosing text selection!

            But still would be nice to make it work with mouse tool switching

             

            #target indesign

            #targetengine myToolListener

             

             

            app.toolBoxTools.addEventListener ("afterAttributeChanged", storeSelection);

            app.toolBoxTools.addEventListener ("afterAttributeChanged", restoreSelection);

            app.addEventListener ("afterSelectionChanged", storeSelection);

            var storedSelection;

             

             

            function storeSelection(){

                if(app.selection[0] && app.selection[0].hasOwnProperty("length") && app.selection[0].length !== 0){

                    storedSelection = app.selection[0].contents;

                }

                if(app.toolBoxTools.currentToolName.toString() == "Eyedropper Tool"){

                    storedSelection = app.selection[0].contents;

                }

            }

             

             

            function restoreSelection() {  

                if(app.toolBoxTools.currentToolName == "Color Theme Tool") {

                    app.findTextPreferences.findWhat = storedSelection;

                    app.select(app.findText());

                    app.findTextPreferences = null;

                }

            }

             

            Also, I noticed, that app.toolBoxTools.currentTool for Color Theme Tool is NONE - so this is a bug

            • 3. Re: Custom Event Listener (for Color Theme Tool)
              Laubender Adobe Community Professional & MVP

              Hi Oleh,

              today I have no time discussing, but I know that currentTool is returning NONE for the Color Theme Tool.

              The same goes for the Content Collector Tool.

               

              So currently we have three instances where NONE can be returned:

              1. No tool selected ( that is documented with the DOM documentation, I cannot see that in the UI )

              2. Color Theme Tool *

              3. Content Collector Tool *


              * This is a known bug I already reported and is acknowledged by the developers.

              Cannot tell if and when this will be fixed.

               

              Regards,
              Uwe

              • 4. Re: Custom Event Listener (for Color Theme Tool)
                Pickory Level 3

                2. Color Theme Tool *

                3. Content Collector Tool *

                 

                These were introduced quite recently, so is it that the scripting interface is out of step with the application.

                 

                P.

                • 5. Re: Custom Event Listener (for Color Theme Tool)
                  Laubender Adobe Community Professional & MVP

                  Hi Pickory,

                  introduced "recently" ?
                  At least the Content Collector Tool is with us since InDesign CS6.

                   

                  Back to work now.

                  Uwe

                  • 6. Re: Custom Event Listener (for Color Theme Tool)
                    Pickory Level 3

                    I suspect that CS6 is when the inconsistencies between scripting and the application started.

                     

                    Back to wishing I had one of these.

                    Bristol Channel Pilot Cutter Owners Association

                    P.

                    • 7. Re: Custom Event Listener (for Color Theme Tool)
                      oleh.melnyk Level 3

                      All these inconsistencies between Adobe programs, versions, and OS reminds me of a similar problem with browsers a few years ago. This problem was solved with the creation of the most popular JavaScript library - jQuery - which meant to  solve the problems with inconsistencies between different browsers API.

                       

                      I think we need something similar for Adobe apps because it's really annoying to write hacks and fixes to make script work as expected, and do that all the time!

                       

                      Currently, I have some ideas, but not sure if I would be able to handle all that alone, so if anyone is interested - let me know!