Hi Jules,
I appreciate that you are OK with it as it is, but it’s been bugging me that I couldn’t figure out why your InDesign was crashing. FWIW, and perhaps for anyone coming to this thread later . . .
Your crashes have predominantly occurred when using UDT. When developing with UDT, as opposed to when you deploy a plugin, you are repeatedly loading and unloading the plugin. It would seem that when you load a plugin, add an event listener, then unload the plugin (without removing the event listener), InDesign becomes unstable (the event listener is still there but the plugin isn’t). When I saw your code I thought what you were doing should work, but when loading and unloading through UDT it didn’t (InDesign crashes). Evidently it is not enough to test for an existing valid event listener and only create it if it doesn’t exist/isn’t valid. Instead you have to test for it, remove it if it exists, create a new one. In other words, you have to create a new event listener on each load (I guess you are remaking the connection between the event listener in InDesign and your plugin each time). I changed the code for your setHostEvents() function to this:
function setHostEvents() {
const existingListener = app.eventListeners.itemByName('bslpriceronactivate');
if (existingListener.isValid) existingListener.remove();
const newListener = app.addEventListener('afterActivate', (event) => {
if (event && event.target && event.target.constructor.name === 'Document') {
console.log('fired');
}
newListener.name = 'bslpriceronactivate';
});
}
The first time I loaded the plugin with this code, InDesign crashed, but subsequently it has not, so I am assuming that that first time InDesign was still in a tangle from all my previous loads/unloads and needed to clear itself. I am pretty confident that the logic of this code – testing for existence, removing, adding – is the best we can do. I have another plugin which is set up this way and does not crash.
Of course, this is very tiresome and essentially a workaround because Adobe have introduced the bug which means we can’t remove the event listener in the plugin destroy hook (the obvious place to do it).
As a minor point, and this doesn’t affect this particular issue in any way whatsoever, I noticed that line 2 of the code for main.js that you posted is:
const { app } = require('indesign');
That line comes from Adobe’s template, but it’s not what Adobe recommends. The recommended code (since InDesign v18.4) is this:
const indesign = require('indesign');
const app = indesign.app;
as documented on this page:
https://developer.adobe.com/indesign/uxp/resources/fundamentals/dom-versioning/
Evidently Adobe haven’t updated their starter template to bring it into line with their own recommendation.
You wrote that you
have made a mental note to avoid Adobe plugins!
I completely get where you are coming from.
For myself, I just wish that Adobe would fix the bugs. This particular bug was introduced last year, fixed, then reintroduced. I drew attention to the fact that it had been reintroduced at the end of October last year, but Adobe haven’t even acknowledged my posts about it let alone fixed the bug. You only have to scroll through the posts here in the InDesign category to see much older bugs that have been identified but not fixed. UXP could be great . . .
Philip