• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Can I add eventListeners to unsaved documents

Explorer ,
Jan 14, 2025 Jan 14, 2025

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) {}
    }
}
TOPICS
Scripting

Views

50

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 14, 2025 Jan 14, 2025

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines