Skip to main content
Timothy Bennett
Inspiring
February 26, 2021
Answered

Call afterOpen event from within CEP panel rather than startup scripts

  • February 26, 2021
  • 2 replies
  • 3063 views

Hiya,

I am attempting the pull some metadata from every file on open and use it to populate a list in a CEP panel. I have no problem getting the metadata, but I can't for the life of me get the eventlistener to work.

I thought I'd start from first principles and used the documentaion example code as a simplified test, but in reality the return value would be the metadata that was pulled from the file on open.

I have this in my main.js:

 

 

onLoad();
function onLoad(){
  csInterface.evalScript('afterOpenEvent()', function(retVal){
  console.log("afterOpen returns: " + retVal);
  });
};

 

and then in my .jsx I have:

 

function afterOpenEvent(){
    var myEventListener = app.addEventListener("afterOpen", myDisplayEventType);
    function myDisplayEventType(myEvent){
        var msg = "This event is the " + myEvent.eventType + " event."
        return msg;
    }
}

 

 

The onLoad() sucessfully calls afterOpenEvent(), but the script stops at the var myEventListener line in the .jsx.
I have found a load of other posts, but they're all talking about putting a headless script in the startup scripts folder, but I want it to run from my CEP panel.

 

Any help with this issue would be greatly appreciated, thanks!

This topic has been closed for replies.
Correct answer Loic.Aigon

Hi there,

 

Listening to afterOpen event with Extendscript requires a persistent engine. This has been explained here but setting those script engines in CEP are not always straightforward.

 

Luckily, CEP comes with it's own event handling layer and more luckily for you InDesign has one that reacts to opening documents. See here :

https://github.com/Adobe-CEP/CEP-Resources/blob/master/CEP_8.x/Documentation/CEP%208.0%20HTML%20Extension%20Cookbook.md

// This works in Illustrator, InCopy, and InDesign
new CSInterface().addEventListener(
    "documentAfterActivate", 
    function(event) {
        alert("Event type:" + event.type +
             "\nData: " + event.data );
    }
)

 

2 replies

Loic.Aigon
Loic.AigonCorrect answer
Legend
March 1, 2021

Hi there,

 

Listening to afterOpen event with Extendscript requires a persistent engine. This has been explained here but setting those script engines in CEP are not always straightforward.

 

Luckily, CEP comes with it's own event handling layer and more luckily for you InDesign has one that reacts to opening documents. See here :

https://github.com/Adobe-CEP/CEP-Resources/blob/master/CEP_8.x/Documentation/CEP%208.0%20HTML%20Extension%20Cookbook.md

// This works in Illustrator, InCopy, and InDesign
new CSInterface().addEventListener(
    "documentAfterActivate", 
    function(event) {
        alert("Event type:" + event.type +
             "\nData: " + event.data );
    }
)

 

Timothy Bennett
Inspiring
March 4, 2021

Thank you Loic.Aigon, that is exacly what I needed!

BarlaeDC
Community Expert
Community Expert
February 28, 2021

HI,

 

I think you need to re-organise your code a little, as I don't think that youw myDisplayEventType is being picked up correctly.

function myDisplayEventType(myEvent){
  var msg = "This event is the " + myEvent.eventType + " event."
  return msg;
}

functon afterOpenEvent(){
  var myEventListener = app.addEventListener("afterOpen", myDisplayEventType);
}

Malcolm

Timothy Bennett
Inspiring
February 28, 2021

Hi Malcolm,

yes, you are right.
I rewrote my code last night and it semi-works. I'm not sure if I should start a new thread?

 

var doc = app.activeDocument;
function afterOpen(){
    app.addEventListener ("afterOpen", myEventFunction, false);   

    function myEventFunction (myEvent) {    
        if ( myEvent.parent.constructor.name == 'LayoutWindow' ) 
        {             
            alert(doc.allObjectStyles.length);  
        }
    }
}

 

My issues with this though are:

  • only works if panel is reloaded beofre use - does not work if panel is opened when ID opens
  • it triggers twice
  • document[0] remains the active document - I've tried with 2 files already open and it still only reads the first open document as the activeDocument. 

To clarify, what I'm trying to achieve is to display in my panel the counts of different styles in the document, and a single custom metdata field. I would like this to update when a file is opened/selected, and as changes are made to the document.

Timothy Bennett
Inspiring
February 28, 2021

I have "finessed" this a bit more:

#targetengine "session"

main();
function main()
{
        var doc = app.activeDocument;
        var docXMP = app.activeDocument.metadataPreferences;
        var fileOpenEventListener = app.eventListeners.add("afterOpen", afterOpen);

    function afterOpen(fileOpenEvent)
    {
        var openDoc = fileOpenEvent.parent;
        if (openDoc.constructor.name == "Document")
        {
            alert("char styles: " + openDoc.allCharacterStyles.length);
            alert(stringTest());
            alert(getWhitelist());
        }

        function stringTest()
        {
            return "string test";
        };

        function getWhitelist() 
        {      
            if (docXMP.getProperty("http://ns.adobe.com/xap/1.0/", "whitelistConfig"))
            {       
                //get config
                var whitelistConfig = docXMP.getProperty("http://ns.adobe.com/xap/1.0/", "whitelistConfig");
                //pass config back to main.js - needs a callback adding in main.js
                return whitelistConfig;
            }
            else
            {            
                return "There is no metadata";
            };        
        };
    }
}

It now only triggers the alerts once (why? I have no idea!), but the panel needs to be reloaded twice before it'll work. The first time round it will successfully call all the functions, but on all subsequent file open events it will throw "an attached script generated the following error: object is invalid" error for getWhitelist().
I *think* this is all something to do with the event listener firing before the document is fully loaded or something along those lines?