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!
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 :
// This works in Illustrator, InCopy, and InDesign
ne
...
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
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:
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.
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?
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 :
// This works in Illustrator, InCopy, and InDesign
new CSInterface().addEventListener(
"documentAfterActivate",
function(event) {
alert("Event type:" + event.type +
"\nData: " + event.data );
}
)
Copy link to clipboard
Copied
Thank you Loic.Aigon, that is exacly what I needed!
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
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.
Copy link to clipboard
Copied
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