3 Replies Latest reply on Sep 30, 2013 7:12 AM by Jump_Over

    ToolBoxTools.addEventListener only works every second time

    AnkeAS

      Hi!

       

      Our layouts pretty often click on the "page tool" by mistake and it happened a few times, that they changes the size of the page without even noticing. Because there is no way to remove the tool from the toolbar (I guess?), I wanted to write a script, that at least shows a message, that the page tool ("Seitenwerkzeug" in german) was selected. Unfortunately the event listener only works every second time:

       

      1. indesign starts > selection tool is active

      2. click on page tool > message appears > page tool is active

      3. click on "another tool1" > "another tool1" is active

      4. click on "another tool2" > "another tool1" is active

      5. click on page tool > nothing happens, the icon of the page tool is active, but the icon of the "another tool2" is still active, too, and "another tool2" is still activated

      6. click on "another tool3" > "another tool3" is active

      7. click on page tool > message appears > page tool is active

      8. ......

       

      Any idea why that is?

       

      My script:

       

      #targetengine myToolListener

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

       

      function changeToolAlert() {

          if(app.toolBoxTools.currentToolName == "Seitenwerkzeug") {

              alert ("Message", "Title");

          }

          exit(); //i tried with and without this!

      }

       

       

      At the beginning I tried to use an confirm message instead of an alert, which had the same problem and I thought it might be because the changing doesn't work... but it seems I can't even work with the information without something "crashing" in the background?

       

      My previous confirm Function:

       

      function changeToolConfirm() {

          if(app.toolBoxTools.currentToolName == "Seitenwerkzeug") {

              var confirmDialog = confirm("Do you really want to activate the page tool?", true);

              if(!confirmDialog) {

                  app.toolBoxTools.currentTool = UITools.SELECTION_TOOL;

              }

          }

      }


       

      I really hope someone has an idea

       

      Best,

      Anke

        • 1. Re: ToolBoxTools.addEventListener only works every second time
          Marc Autret Level 4

          Hi Anke,

           

          The problem, I think, comes from the fact that the Page Tool selection event (which you listen to via afterAttributeChanged) needs some time to complete. Since the alert() or confirm() instruction gives the focus to a new modal window before the event life-cycle is finished, the GUI becomes instable, the PageTool icon state is not properly restored, and your event management goes wrong.

           

          A way to solve this might be to queue a temporary IdleEvent listener once the Page Tool selection event is caught. That is, we do not initiate any modal dialog as long as the GUI is refreshing.

           

          Something like this:

           

          //====================================================
          // PageToolDisclaimer.jsx
          //====================================================
          
          // Should be useable as a startup script, no #targetengine required
          // NB - MutationEvent is known to create a global 'evt' variable
          // so we don't seem to need a persistent session engine here :-)
          // That's why the active script File is used as the event handler
          
          (function(/*File*/EVENT_HANDLER, /*str*/TASK_NAME, /*uint*/TASK_TIME, /*str*/PAGE_TOOL_NAME)
          {
              var t;
          
              // Installer
              // ---
              if( !(t=app.toolBoxTools.eventListeners).length )
                  {
                  t.add(MutationEvent.AFTER_ATTRIBUTE_CHANGED, EVENT_HANDLER);
                  return;
                  }
          
              // IdleEvent handler (--> confirm)
              // ---
              if( (t=app.idleTasks.itemByName(TASK_NAME)).isValid )
                  {
                  t.eventListeners.everyItem().remove();
                  t.remove();
                  if( !confirm("***WARNING***\rDo you really want to activate the page tool?", true) )
                      {
                      app.toolBoxTools.currentTool = UITools.SELECTION_TOOL;
                      }
                  return;
                  }
          
              // PageTool event handler
              // ---
              if( ('evt' in $.global) && 'currentToolName'==evt.attributeName && PAGE_TOOL_NAME==evt.attributeValue )
                  {
                  evt.stopPropagation();
                  app.idleTasks.add({name:TASK_NAME, sleep:TASK_TIME})
                        .addEventListener(IdleEvent.ON_IDLE, EVENT_HANDLER);
                  }
          })(app.activeScript, 'WaitPageTool', 400, app.translateKeyString('$ID/Page Tool'));
          

           

          Hope that helps.

           

          @+

          Marc

          • 2. Re: ToolBoxTools.addEventListener only works every second time
            AnkeAS Level 1

            That's perfect, thank you so much!!!

            • 3. Re: ToolBoxTools.addEventListener only works every second time
              Jump_Over Level 5

              Hi Anke,

               

              mark Marc's post as "correct" pls,
              cause is so "PERFECT" that many will use it, I am sure.

               

              Jarek