34 Replies Latest reply on Sep 21, 2011 5:04 PM by bduffy323

    Event Listener on an Asset

    bduffy323 Level 1

      I am trying to have a start up script that gives an event listener to every asset in a library that will fire whenever an asset is placed on the page. However I'm not capturing any events that are being fired. Here is the code I am using in the start up script. I am using CS 5.5.

       

      function assetEventHandler()

      {

          alert("Placed Asset.");

      }

       

       

      var myLibs = app.libraries;

      var myLib = myLibs.item("testLibrary.indl");

       

      var myAssets = myLib.assets;

      for (var i=0;i<myAssets.length;i++)

      {

          var myAsset = myAssets[i];

         myAsset.addEventListener("afterPlace",assetEventHandler);

      }

        • 1. Re: Event Listener on an Asset
          Harbs. Level 6

          That will not work.

           

          You can only attach afterPlace on the document level or higher...

           

          Harbs

          1 person found this helpful
          • 2. Re: Event Listener on an Asset
            bduffy323 Level 1

            Okay, I'm new to indesigns hierarchy. What event types are available to me at the level in which assets fall?

            • 3. Re: Event Listener on an Asset
              Harbs. Level 6

              none.

               

              Harbs

              • 4. Re: Event Listener on an Asset
                Harbs. Level 6

                If you need to attach events on the object level, I suggest you check out APID http://www.rorohiko.com/wordpress/indesign-downloads/active-page-item-developer/

                 

                Harbs

                1 person found this helpful
                • 5. Re: Event Listener on an Asset
                  bduffy323 Level 1

                  If none are accepted why would they put eventlisteners as a property on an asset object? Not sure if you know the answer to this I just find it strange as to why it would be there. Is there a different way to use the addEventListener() that would make sense for an asset that cant take any event types?

                  • 6. Re: Event Listener on an Asset
                    Harbs. Level 6

                    Probably because it was a feature that was never finished...

                     

                    Harbs

                    • 7. Re: Event Listener on an Asset
                      bduffy323 Level 1

                      I tried placing an afterPlace event listener on my active document but can't seem to get it to fire.

                       

                       

                      app.activeDocument.addEventListener("afterPlace",testMethod);

                       

                      function testMethod()

                      {

                      alert("here");

                      }

                       

                      Am I doing something wrong here?

                      • 8. Re: Event Listener on an Asset
                        Harbs. Level 6

                        Are you using a persistent engine?

                         

                        #targetengine "bduffy323"

                        • 9. Re: Event Listener on an Asset
                          bduffy323 Level 1

                          No, I just copied that from someone else code segment and forgot to get rid of it. Would that be messing up my eventlisteners?

                          • 10. Re: Event Listener on an Asset
                            Harbs. Level 6

                            For event listeners to work, your code must be run in a persistent engine.

                            • 11. Re: Event Listener on an Asset
                              bduffy323 Level 1

                              Well I guess I was running it in a persistent engine with or without that statement right? because indesign will create one if the #targetengine "bduffy323" declaration doesn't exist? The ultimate goal for me is to execute some code when an asset is placed on the page via the Library window. So I figured that I would need an event listener that would run while indesign was open that would fire when an action like that took place, which is why I thought after_place was a good starting point. I'm sure its possible...just have to be clever enough to figure out how

                              • 12. Re: Event Listener on an Asset
                                Harbs. Level 6

                                No. Without a #targetengine directive, InDesign runs scripts in the "Main" non-persistent engine.

                                • 13. Re: Event Listener on an Asset
                                  bduffy323 Level 1

                                  Your right, sorry I miss read the post about memory leaks dealing with the persistent engines. This would make it possible to have an event listener active the entire time indesign is open correct? Do you think there is an event listener that can accomplish what I am trying to do or should I a different approach?

                                  • 14. Re: Event Listener on an Asset
                                    Harbs. Level 6

                                    Persistent engines last until InDesign closes.

                                     

                                    If you need a script to run when InDesign launches, you need to place it in a startup scripts folder.

                                     

                                    Harbs

                                    • 15. Re: Event Listener on an Asset
                                      bduffy323 Level 1

                                      Right, do you know if there is an event listener, attached to any object (app, doc, etc...) that while running in the persistent engine which gets created by a start up script, that would be capable of firing when an asset is placed on the page?

                                      • 16. Re: Event Listener on an Asset
                                        Harbs. Level 6

                                        Yes.

                                         

                                        Harbs

                                        • 17. Re: Event Listener on an Asset
                                          bduffy323 Level 1

                                          So I have this code working just to make sure that my Event Listeners are now working:

                                           

                                          #targetengine "MyEngine"

                                           

                                           

                                          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

                                          function doThis()

                                          {

                                           

                                           

                                                    alert("here");

                                          }

                                           

                                           

                                          app.addEventListener("afterSelectionChanged",doThis);

                                           

                                           

                                          After my selection changes I do indeed get an alert. SO,

                                           

                                          I'm using http://jongware.mit.edu/idcs5/pc_Library.html as an API and after doing some research it seems that there are no eventTypes that are suitable at the Application and document level for what I am trying to do. And there are No event types listed for a Library or an Asset. Are you seeing something I'm not?

                                           

                                          APLICATION:

                                          AFTER_ACTIVATEstringreadonlyDispatched after the Application becomes active. This event bubbles. This event is not cancelable. (default: afterActivate)
                                          AFTER_CLOSEstringreadonlyDispatched after a Document is closed. This event bubbles. This event is not cancelable. (default: afterClose)
                                          AFTER_CONTEXT_CHANGEDstringreadonlyDispatched after the active context changes. This event bubbles. This event is not cancelable. (default: afterContextChanged)
                                          AFTER_QUITstringreadonlyDispatched when the Application is quitting. Since the quit has been committed, it can not be canceled. This event bubbles. This event is not cancelable. (default: afterQuit)
                                          AFTER_SELECTION_ATTRIBUTE_CHANGEDstringreadonlyDispatched after an attribute on the active selection changes. This event bubbles. This event is not cancelable. (default: afterSelectionAttributeChanged)
                                          AFTER_SELECTION_CHANGEDstringreadonlyDispatched after the active selection changes. This event bubbles. This event is not cancelable. (default: afterSelectionChanged)
                                          BEFORE_DEACTIVATEstringreadonlyDispatched before the Application becomes inactive. This event bubbles. This event is not cancelable. (default: beforeDeactivate)
                                          BEFORE_NEWstringreadonlyDispatched before a Document is created. This event bubbles. This event is cancelable. (default: beforeNew)
                                          BEFORE_OPENstringreadonlyDispatched before a Document is opened. This event bubbles. This event is cancelable. (default: beforeOpen)
                                          BEFORE_QUIT

                                           

                                          DOCUMENT:

                                           

                                          PropertyTypeAccessDescription
                                          AFTER_ACTIVATEstringreadonlyDispatched after the Document becomes active. This event bubbles. This event is not cancelable. (default: afterActivate)
                                          AFTER_EXPORTstringreadonlyDispatched after a Document is exported. This event bubbles. This event is not cancelable. (default: afterExport)
                                          AFTER_IMPORTstringreadonlyDispatched after importing a file into a Document. This event bubbles. This event is not cancelable. (default: afterImport)
                                          AFTER_LINKS_CHANGEDstringreadonlyDispatched after one or more links in the Document have been added, deleted, or modified. This event bubbles. This event is not cancelable. (default: afterLinksChanged)
                                          AFTER_NEWstringreadonlyDispatched after a Document is created. This event bubbles. This event is not cancelable. (default: afterNew)
                                          AFTER_OPENstringreadonlyDispatched after a Document is opened. This event bubbles. This event is not cancelable. (default: afterOpen)
                                          AFTER_PRINTstringreadonlyDispatched after a Document is printed. This event bubbles. This event is not cancelable. (default: afterPrint)
                                          AFTER_REVERTstringreadonlyDispatched after a Document is reverted. This event bubbles. This event is not cancelable. (default: afterRevert)
                                          AFTER_SAVEstringreadonlyDispatched after a Document is saved. This event bubbles. This event is not cancelable. (default: afterSave)
                                          AFTER_SAVE_ASstringreadonlyDispatched after a Document is saved under a new name. This event bubbles. This event is not cancelable. (default: afterSaveAs)
                                          AFTER_SAVE_A_COPYstringreadonlyDispatched after a copy of a Document is saved. This event bubbles. This event is not cancelable. (default: afterSaveACopy)
                                          BEFORE_CLOSEstringreadonlyDispatched before a Document is closed. This event bubbles. This event is not cancelable. (default: beforeClose)
                                          BEFORE_DEACTIVATEstringreadonlyDispatched before the Document becomes inactive. This event bubbles. This event is not cancelable. (default: beforeDeactivate)
                                          BEFORE_EXPORTstringreadonlyDispatched before a Document is exported. This event bubbles. This event is cancelable. (default: beforeExport)
                                          BEFORE_IMPORTstringreadonlyDispatched before importing a file into a Document. This event bubbles. This event is cancelable. (default: beforeImport)
                                          BEFORE_PRINTstringreadonlyDispatched before a Document is printed. This event bubbles. This event is cancelable. (default: beforePrint)
                                          BEFORE_REVERTstringreadonlyDispatched before a Document is reverted. This event bubbles. This event is cancelable. (default: beforeRevert)
                                          BEFORE_SAVEstringreadonlyDispatched before a Document is saved. This event bubbles. This event is cancelable. (default: beforeSave)
                                          BEFORE_SAVE_ASstringreadonlyDispatched before a Document is saved under a new name. This event bubbles. This event is cancelable. (default: beforeSaveAs)
                                          BEFORE_SAVE_A_COPYstringreadonlyDispatched before a copy of a Document is saved. This event bubbles. This event is cancelable. (default: beforeSaveACopy)
                                          FAILED_EXPORTstringreadonlyDispatched after a Document export is canceled or fails. This event bubbles. This event is not cancelable. (default: failedExport)
                                          • 18. Re: Event Listener on an Asset
                                            Harbs. Level 6

                                            There's an extension included in the CS SDK which gives great feedback on what events are triggered when -- and on which objects.

                                             

                                            You can thank Bob Stucky (the author of the extension) for the suggestion to post it here.

                                             

                                            Here's an installable version of the extension:

                                            http://in-tools.com/downloads/asst/EventWatcher.zxp

                                             

                                            And here's a couple of screenshots:

                                            2011-09-20_2023.png

                                            2011-09-20_2024.png

                                            • 19. Re: Event Listener on an Asset
                                              Harbs. Level 6

                                              Also: events bubble, so you can catch a document event on the application level as well.

                                              • 20. Re: Event Listener on an Asset
                                                bduffy323 Level 1

                                                WOW, that extension is awesome. Thanks goes out to Bob Stucky he did a great job and to you for posting it on the forum. Unfortunately the only events that fire when an asset is placed are afterSelectionChanged and afterSelectionAttributeChanged and I can't see a way of differentiating between placing assets and just freely clicking on and off page items so I believe there actually is no a way to determine if an asset was placed using event listeners. Thanks for all your help, if something sparks and you think of an idea just post it!

                                                • 21. Re: Event Listener on an Asset
                                                  absqua Level 4

                                                  Yeah, that extension would've saved me banging my head against a wall a number of times. Maybe something about it—as well as links to the event types documentation and maybe some info about which events are known to be buggy or not implemented—on your new documents page?

                                                   

                                                  Jeff

                                                  • 22. Re: Event Listener on an Asset

                                                    You're not seeing events fire on beforePlace and afterPlace??

                                                     

                                                    I see both of those events firing, both at the asset level (beforePlace ->Rectangle, afterPlace->Image

                                                    • 23. Re: Event Listener on an Asset
                                                      bduffy323 Level 1

                                                      This is what I see after I drag or right click and select place an asset from the library window.

                                                       

                                                      After.png

                                                      • 24. Re: Event Listener on an Asset
                                                        Bob Stucky Adobe Employee

                                                        Process: Click "Configure", Click "All", scroll to bottom, unclick "On Idle", Click "Back", Drag & Drop asset from a Library.

                                                         

                                                         

                                                        Here's what I see...

                                                         

                                                        EventWatcher.png

                                                         

                                                        I've attached another copy of the extension installer in case Harbs' version was a bit older than what I have.

                                                         

                                                        Regards

                                                        • 25. Re: Event Listener on an Asset
                                                          bduffy323 Level 1

                                                          I just installed the eventWatcher you gave me and followed the same process and still got the same results as I did the first time. I am not sure what the problem could be?

                                                          • 26. Re: Event Listener on an Asset
                                                            Bob Stucky Adobe Employee

                                                            What's your ID update status?

                                                            • 27. Re: Event Listener on an Asset
                                                              bduffy323 Level 1

                                                              I'm not sure what that is, where would I find it? I went to help, then updates and updated everything. Then eventWatcher disappeared from Window->extensions. So I removed it from extension manager and redownloaded it and it says it is enabled in extensions manager but still isn't visible under Window->extensions

                                                              • 28. Re: Event Listener on an Asset
                                                                Harbs. Level 6

                                                                Bob,

                                                                 

                                                                I see the same thing as bduffy. Placing a file triggers the events, but placing an asset from a library doesn't.

                                                                 

                                                                I seem to remember this coming up. It's not "placed", just "pasted". What version did you try it in?

                                                                • 29. Re: Event Listener on an Asset
                                                                  absqua Level 4

                                                                  If the library asset you're dragging out has a placed image, you get place events. If it doesn't, you get only the selectionChanged events.

                                                                  • 30. Re: Event Listener on an Asset
                                                                    Harbs. Level 6

                                                                    Bob,

                                                                     

                                                                    I think I know what bduffy is talking about. If I'm not mistaken, there's a bug in the latest InDesign update which causes extensions which has the version set to Version=[7.0,7.5] to not appear in CS5.5. Extension which have it set to Version="7.0" appear in ID CS5.5 fine.

                                                                     

                                                                    I have not yet gotten around to filing this bug -- I just discovered it yesterday...

                                                                    • 31. Re: Event Listener on an Asset
                                                                      Harbs. Level 6

                                                                      Ah. Good point. When the frame is pasted, it "places" the contained image.

                                                                       

                                                                      Snippets on the other hand, produce placed events for all object types (on the frame level).

                                                                      • 32. Re: Event Listener on an Asset
                                                                        bduffy323 Level 1

                                                                        Ah, I wish I would have known about this bug, I really like using the eventWatcher extension.

                                                                        • 33. Re: Event Listener on an Asset
                                                                          Bob Stucky Adobe Employee

                                                                          Does this fix it for you?

                                                                          • 34. Re: Event Listener on an Asset
                                                                            bduffy323 Level 1

                                                                            Yes it did, Thank you. Its a great extension. I just wish Indesign made more use of their event listeners on assets.