Copy link to clipboard
Copied
Hi, what I'm trying to do is add an eventListener to a document so when I place an image in a box it will check the image name against an applied script label. What I have so far works on documents that have been saved but not on documents that haven't been. However if I open a document that has been saved and then click to the document that hasn't been saved the script then starts working. Can anyone explain what is happening here please?
#targetengine 'ImageCheck'
var doc
var el = app.eventListeners.add("afterActivate", getActive);
function getActive(e){
if (e.target.constructor.name == "Document") {
doc = e.target
try {
if (!doc.eventListeners.itemByName("afterImportItem").isValid) {
var afterImportItem = doc.eventListeners.add("afterPlace", checkScriptLabels)
}
} catch(e) {}
}
}
Copy link to clipboard
Copied
Add some logging, e.g. using $.writeln() when your debug the script in VSCode or ESTK.
One way to describe the document is doc.toSpecifier() which produces a string regardless from the document being saved.
One guess is that your new document is not seen by the afterActivate event handler so it does not receive its own afterPlace handler. Maybe use the afterNew event for that. Be prepared that it also happens for Link objects.
Even better – avoid to listen to every document, afterPlace bubbles so one listener at the application should be sufficient.