ExtendScript CEP on ID 18.1: app.open() crashes when reopening file
For the purpose of templating, we have scripts that sometimes have to close and reopen the same file multiple times. When ran from the scripts panel, this works fine. When called from a CEP extension, the same procedure causes a crash. I discovered this while trying to port some aforementiond scripts to a CEP extension panel to improve usability.
I have been able to reproduce the bug, consistently and on multiple machines including a fresh install, with variations on this code:
var fileToReopen = new File(app.activeDocument.fullName);
// var fileToReopen = new File("path/to/some/file.indd"); //// makes no difference
app.activeDocument.close();
// $.gc() //// makes no difference
// $.sleep(1000 //or 5000 or 10000) //// makes no difference
// for (var i=0; i<someArrayOfOtherFiles.length; i++) {
// app.open(someArrayOfOtherFiles[i]);
// app.documents[0].close();
// } //// do a bunch of other stuff, makes no difference!
app.open(fileToReopen); <---- crashes every time with exception c0000005 ERROR_ACCESS_VIOLATION
So, in effect, it can be caused with just 3 lines:
var fileToReopen = new File(app.activeDocument.fullName);
app.activeDocument.close();
app.open(fileToReopen); <---- exception c0000005
These same 3 lines produce the intended result when called inside a script that's executed from the Scripts Panel. When called from the host (jsx) side of a CEP extension, it crashes InDesign. Doesn't matter if the call is automatic (one-time on load) or triggered by a UI or other event.
Other than inserting sleeps and running the gc to try and make sure it's not a race condition or stale memory, I haven't thought of any other bandaids yet. I have found other posts talking about this same issue going back to 2017, so it seems like it's been unresolved for over 5 years. Those other posts, such as this one had no working solutions or workarounds.
There is a workaround that does work, in case anyone needs it:
var file = new File("some/path");
// Instead of:
app.open(file);
// Do this:
file.execute();
I'm not sure why doing it this way works when the other way doesn't.
I hope this is able to help someone. In the meantime, consider this an open bug report until the app.open() call is fixed. That should be the proper way to open a InDesign file from CEP. Using File.execute() is a bit like climbing up the gutter to avoid taking the stairs.
