[ExtendScript] How to use "afterNew" event
Hi all, I'm trying to run some ExtendScript code when the user creates a new document but I'm not getting it to work. I've probably made a basic mistake, which someone can point out, but otherwise, could anyone test my script on their system?
I expect it to name the first Layer "TEST" but for me it throws an error (which the script handles and displays in an alert).
I'm using Indesign 19.0.1 and MacOS 14.1.2.
Here is my test case, whch I run from the Startup Scripts folder:
//@targetengine "test"
if (app.eventListeners.item("afterNew").isValid)
app.eventListeners.item("afterNew").remove();
app.eventListeners.add("afterNew", myHandler);
function myHandler(ev) {
var messages = [
'ev.parent.constructor.name = ' + ev.parent.constructor.name,
'app.layoutWindows.length = ' + app.layoutWindows.length,
'app.modalState = ' + app.modalState,
];
try {
// do something to the DOM
ev.parent.layers[0].name = 'TEST';
messages.unshift('Success');
}
catch (error) {
messages.unshift('Failed:');
messages.push('error: ' + error.message + ' (line: ' + error.line + ')');
}
finally {
alert(messages.join('\n'));
}
};
This is what I see every time I create a new document:

Thanks in advance.
- Mark
Edit 2023-12-05: added an alert even if the test was successful, added screenshot.



