Skip to main content
bduffy323
Inspiring
September 15, 2011
Answered

Event Listener on an Asset

  • September 15, 2011
  • 1 reply
  • 7911 views

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;

   myAsset.addEventListener("afterPlace",assetEventHandler);

}

This topic has been closed for replies.
Correct answer Harbs.

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)

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:

1 reply

Harbs.
Legend
September 15, 2011

That will not work.

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

Harbs

bduffy323
bduffy323Author
Inspiring
September 16, 2011

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?

bduffy323
bduffy323Author
Inspiring
September 21, 2011

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


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