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

Call afterOpen event from within CEP panel rather than startup scripts

Explorer ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

1.7K

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

correct answers 1 Correct answer

People's Champ , Mar 01, 2021 Mar 01, 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
ne
...

Votes

Translate

Translate
Community Expert ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

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

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
Explorer ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

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.

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
Explorer ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

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?

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
People's Champ ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

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%20Exte...

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

 

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
Explorer ,
Mar 03, 2021 Mar 03, 2021

Copy link to clipboard

Copied

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

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
Advocate ,
Nov 08, 2023 Nov 08, 2023

Copy link to clipboard

Copied

Im a bit lost of how i can send cookie data from say panel A to panel B. Both panels are from the same extension and are in the same CEP folder. Ive tried looking at the CSevent but its not clear to me how to actually implement this. Im no developer. Ive made some CEP panels, but this is a bit above my knowledge.

Im lost of where i need to call the CSevent and how to call the cookie data and send it to the other panel 

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
People's Champ ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

To send messages between panels, you may need Vulcan.js library:
https://github.com/Adobe-CEP/CEP-Resources/blob/master/CEP_11.x/Vulcan.js

See

Vulcan.prototype.dispatchMessage for ex

You would need one Vulcan instance listening in Panel A and one Vulcan instance to dispatch event in panel B.

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
Advocate ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

LATEST

My need was 2 talk between 2 panels in the same extension. I just read that Vulcan let you talk between panels in 2 different host apps. Mine "live" in photoshop within the extension.

 

I'll try to explain it as clear as i can.

 

After looking lots of times at a link iv  found, i was able to get it to work doing lots of tests. I was about to give up, but then noticed the small detail about adding the eventName to the id in the type. So it goes as followed.

 

I will simply the names by panel A and panel B also the JS files accordingly

 

So in A.js i use this

> var event = new CSEvent();

  event.type = "com.sbaril.animdessin2.Panel.foo";

  // event.scope = "GLOBAL";

  event.scope = "APPLICATION";

  event.data = frameLength;

  csInterface.dispatchEvent(event);

 

Then in B.js from the other panel. i add the eventlistnere around the init stage. I first had it up in the script, but seems to have caused the issue.

> csInterface.addEventListener("com.sbaril.animdessin2.Panel.foo", function(e){

    console.log("event: "+e.data);

    localStorage.setItem("frameLength", e.data)

});

 

hope this helps other people as well

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