0

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:

enter image description here

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?

Nicolai Kant
  • 1,391
  • 1
  • 9
  • 23
  • Is there a "Book closed" event you could listen to? – Tobias Kienzler May 26 '16 at 14:10
  • I don't think so. At least I can't find anything in the reference. – Nicolai Kant May 26 '16 at 17:28
  • 1
    That would have been to easy... I guess you could try using Windows' [inotify alikes](http://stackoverflow.com/q/3517460/321973) (if ExtendScript can access that somehow) to get notified when the file handle is actually released... – Tobias Kienzler May 26 '16 at 17:32
  • Thanks for helping, Tobias. My client is on a Mac which uses FSEvents and Indesign uses modified JavaScript ECMA3. I do not think the integration with system events is that good, but it is something I should probably check out. – Nicolai Kant May 26 '16 at 18:23
  • Maybe you can `doScript` on some AppleScript then if the JavaScript doesn't support system calls. But that's merely something I heard, I'm a pure Linux/Windows guy ;) – Tobias Kienzler May 26 '16 at 19:17
  • Yes, another good idea. Thanks, Tobias – Nicolai Kant May 26 '16 at 19:24

1 Answers1

0

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.

Christina
  • 1,349
  • 1
  • 12
  • 22
  • Thanks, Christina. I've tried something similar with while(app.books.length>0){}. This seems to get me into an eternal loop, or at least takes so long I had to force quit. I will try you version to be sure. – Nicolai Kant Feb 25 '15 at 16:56
  • Sorry, does not work. Scripts skips to the next step (alert) and I can still see the book open. I tried a few modifications, still no luck. Also tried to close all documents before saving book, does not seem tp make any difference. – Nicolai Kant Feb 25 '15 at 17:15
  • Also, using this approach, I am getting an eternal loop, same as I had when I tried while(app.books.length>0) – Nicolai Kant Feb 25 '15 at 17:32
  • This is probably not it, but are you setting the userInteractionLevel to Never_Interact at any point? I've run into issues in the past where I neglected to undo that and ID got stuck on some notification that I couldn't see. Shot in the dark. – Christina Feb 25 '15 at 19:30
  • No - that's another good suggestion, as I am creating and saving/closing files. Unfortunately, tried it as well (sorry). I tried to put it on and off and remove completely. Does not make any difference. Thanks for trying to help. – Nicolai Kant Feb 26 '15 at 00:32
  • Instead of the endless loop, you should use an [`IdleTask`](http://www.indesignjs.de/extendscriptAPI/indesign11/#IdleTask.html) so your loop doesn't block the whole system. (Also @NicolaiKant) – Tobias Kienzler Aug 10 '17 at 07:50