close book in event handler is out of sync
function closeBooks() {
var books = app.books.everyItem().getElements();
var i, ilen = books.length;
for (i = 0; i < ilen; i++) {
if (books[i].modified) {
books[i].close(SaveOptions.ASK);
}
}
alert('done');
}
if you call the closeBooks function, everything works fine.
But if the function is used as a handler, the "done" message appears immediately, before the for loop complete.
try adding a beforeQuit eventListener:
app.addEventListener('beforeQuit', closeBooks);
and close InDesign having one or more modified books to see this (strange) behaviour.
The same function works fine if you target documents.
InDesign CC2021 16.2.1 on MacOs Catalina 10.15.7
1
vote
![](https://secure.gravatar.com/avatar/fb6d02fa6cfd4dad61482167c4f0b15e?size=40&default=https%3A%2F%2Fassets.uvcdn.com%2Fpkg%2Fadmin%2Ficons%2Fuser_70-6bcf9e08938533adb9bac95c3e487cb2a6d4a32f890ca6fdc82e3072e0ea0368.png)