1 Reply Latest reply on Sep 18, 2008 9:41 AM by (eykd)

    [CS3 JS] Working with two documents of the same name

      I work with sets of documents that use standardized names. I'm trying to write a script that compares two documents from different sets. Because the source and the target documents both have the same name, I've encountered some disturbing behavior that reminds me of the app.activeWindow bug that Dave has written about. ( Dave Saunders, "Scripting FAQ as Wiki" #14, 15 Oct 2007 12:19 pm)

      To perform the comparison, I have to grab a reference to the same region of text in both documents, as offset from regular landmarks. I was just patting myself on the back for figuring out the "story.characters.itemByRange(begin, end).texts[0]" acrobatics required, when I discovered that my text objects were magically "dissolving" into invalid objects.

      After a lot of debugging and attempts to hack around the problem (i.e. storing the text begin/end indices and not grabbing the text object until the last possible moment), I've narrowed it down to the references to the source and target documents. I've watched the reference variable to the source document *change* to instead reference the target document when I start accessing the target document object. Once this happens, any text objects belonging to the source document become invalid, as if the source document had been closed.

      Is there any way around this? I've thought about temporarily saving the files to different names, but I haven't tried it yet.

      Here's a script that demonstrates the problem (you'll have to find your own same-named documents if you want to run it):

      var source = "/Users/deyk/Desktop/test/source";

      var target = "/Users/deyk/Desktop/test/target";

      function findChapters(in_doc) {
      app.findGrepPreferences = NothingEnum.nothing;
      app.findGrepPreferences.appliedCharacterStyle = "chapter-num";
      app.findGrepPreferences.findWhat = "\\d+";
      return in_doc.findGrep();

      var sdoc = app.open(File(source+"/01.Genesis.indd"));
      var s_file_start = sdoc.fullName;

      var s_chs = findChapters(sdoc);
      var s_story = s_chs[0].parentStory;
      var s_txt = s_story.characters.itemByRange(s_chs[0].index,

      var tdoc = app.open(File(target+"/01.Genesis.indd"));
      // At this point, sdoc now references tdoc instead.
      // s_txt, s_story, and s_chs become invalid objects.
      var t_file_start = tdoc.fullName;

      var t_chs = findChapters(tdoc);
      var t_story = t_chs[0].parentStory;
      var t_txt = t_story.characters.itemByRange(t_chs[0].index,

      var s_file_end = sdoc.fullName;

      if (s_file_end.name != s_file_start.name) {
      var result = "What a nasty bug!";