Copy link to clipboard
Copied
Hi,
I have to detect a change in the xml structure of my Indesign document (like node deleted, moved and so on).
How can I do that in a cep extension ?
I haven't seen any event for that in Indesign
Thanks in advance
Well, I am guessing that you should either listen at AFTER_SELECTION_CHANGED EVENT or IDLE_EVENT and constantly monitor structural changes.
AFTER_SELECTION_CHANGED may be preferable but could suffer from latencies dues to User UI manipulation (the event will be fired only when user will select anything else or deselect everything).
IDLE_EVENT will be more "realtime" but could suffer from performance issues as xml files would be generated a lot of times per second.
Of course one can argue that expor
...Copy link to clipboard
Copied
Well, I am guessing that you should either listen at AFTER_SELECTION_CHANGED EVENT or IDLE_EVENT and constantly monitor structural changes.
AFTER_SELECTION_CHANGED may be preferable but could suffer from latencies dues to User UI manipulation (the event will be fired only when user will select anything else or deselect everything).
IDLE_EVENT will be more "realtime" but could suffer from performance issues as xml files would be generated a lot of times per second.
Of course one can argue that exporting XML could be unnecessary but I think this methods is better than looping through the DOM (but that's personal opinion and also a bit of measure)
//inDesign.jsx
#targetengine "xmlChanges"
var main = function(){
var evName = "onAfterSelectionChangedEvent",
ev = app.eventListeners.itemByName (evName),
db = {},
externalObjectName = "PlugPlugExternalObject",
mylib = new ExternalObject( "lib:" + externalObjectName ),
eventObj = new CSXSEvent(),
evHandler = function(evt) {
var doc = app.properties.activeDocument, docId, docStr, xmlFile;
if ( !doc ) {
db = {};
return;
};
docId = doc.id;
xmlFile = File ( Folder.temp+"/"+docId+".xml" );
doc.exportFile ( ExportFormat.XML, xmlFile );
if ( !xmlFile.exists ) return;
if ( !xmlFile.open('r') ) return;
docStr = XML ( xmlFile.read() );
xmlFile.close();
xmlFile.remove();
if ( !db[docId] ) {
db[docId] = docStr;
eventObj.type="xmlChanged";
eventObj.data = "";
eventObj.dispatch();
}
else {
if ( docStr!= db[docId] ) {
eventObj.type="xmlChanged";
eventObj.data = "Something changed !";
eventObj.dispatch();
db[docId] = docStr;
}
else {
eventObj.type="xmlChanged";
eventObj.data = "Nothing new under the sun !";
eventObj.dispatch();
}
}
};
//="blahblah";
if ( !ev.isValid ) app.eventListeners.add("afterSelectionChanged",evHandler).name = evName;
eventObj.type="xmlChanged";
eventObj.data = "Nothing new under the sun !";
eventObj.dispatch();
}
main();
In main.js
$(function() {
var csInterface = new CSInterface();
csInterface.addEventListener ( "xmlChanged", function(evt) {
$("#result").text ( evt.data );
});
});
in html
<body>
<p>Listening at xml changes…</p>
<p id="result"></p>
<button onclick="window.location.reload()">RL</button>
</body>
And voilà :
Copy link to clipboard
Copied
Thanks for you answer, it works well !
Copy link to clipboard
Copied
glad it helped…