Edited shorter version:
I am trying to close an open book in InDesign and run some functionality after. It looks like the next step starts before the book is fully closed
myBook.close();
alert("Book closed");
I have the alert displaying with the open book still on the background. If I try any operations on files from the book it crashes InDesign. More details in the original postbelow.
===============================
Sorry for a long post.
I am building a book in InDesign. MAC OSX 10.9.5 Indesign CS5.5 First I create and populate around 30 chapters, add TOC chapter at the beginning and another one at the end, create a book, add all chapters to the book, close and save the book. After that I am trying to open the last TOC chapter to insert TOC. After the file open Indesign crashes.
Without the file open statement everything works fine.
The code sample:
after the book is created:
// myBook is referenced through the app.open(myBookFile);  
// with myBookFile is a string - path to the file
// also tried to re-reference book after open file by using app.books[0]
myBook.updateAllNumbers(); // tried to disable, did not help
//$.sleep(5000);  //tried this, did not help
app.activeBook.close();  
// also tried myBook.close(); no change
// tried myBook.close(SaveOptions.YES); no change
//$.sleep(5000); //tried this, did not help
      //$.sleep(5000);
      // add toc to the last chapter
      alert("Before TOC Insert");
      indexAddTOC();
      alert("Build is complete");
When the first alert comes up I can see the book is still open. Also stepping with EST debugger is closing the book before the first alert (and does not crash InDesign).
I can see the last message and after this InDesign is crashing. On re-open it opens every chapter from the book.
 function indexAddTOC(){
// re-open index file
var myFolder = new Folder("~/Desktop/"  + publicationName.replace(/ /g, "_").replace(/:/g, ""));
var myFileName =  "000".substr( 1,3 - selectionNumber.toString().length) + selectionNumber  + "_Index.indd";
var myFile = File(myFolder.fsName + "/" + myFileName);
app.open(myFile); // disabling this eradicates the error
}
The error message:

The error report dump has this:
Thread 16:
0   libsystem_kernel.dylib          0x98b4e046 __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x947d4dcf _pthread_wqthread + 372
2   libsystem_pthread.dylib         0x947d8cce start_wqthread + 30
Thread 0 crashed with X86 Thread State (32-bit):
  eax: 0x00000040  ebx: 0x00000040  ecx: 0x00000040  edx: 0x00000000
  edi: 0xbfffac54  esi: 0x00000040  ebp: 0xbfffab88  esp: 0xbfffab70
   ss: 0x00000023  efl: 0x00010282  eip: 0x947d6bd5   cs: 0x0000001b
   ds: 0x00000023   es: 0x00000023   fs: 0x00000000   gs: 0x0000000f
  cr2: 0x00000040
Logical CPU:     0
Error Code:      0x00000004
Trap Number:     14
It feels that the issue is in the book not closing properly. Anyone had a similar problem before and any idea how to get around the problem?
Perhaps something like
while(app.books.itemByName(myBookFile).isValid){}
in place of your sleep? I can't test at the moment but that's one idea.
User contributions licensed under CC BY-SA 3.0