title: JavaScript + InDesign: Find a word in a .indd document and write a .txt file with page number locations
body: <p>I'm running into a problem while trying to execute a script to find a specific word — in this case the name of a project within a book — and write the page numbers of all its occurrences in my InDesign document to a .txt file.</p>
  
  <p>Problem: For whatever reason, the returned "parentPage.name" values are listed out of order (i.e.: its occurrence on p. 184 appears in the list before p. 11, for example, etc.)</p>
  
  <p>I'm not sure if it's related to this code (below) or elsewhere:</p>
  
  <pre><code>pg_nr = found_txt.parentTextFrames[0].parentPage.name;
  </code></pre>
  
  <p>If anyone has any ideas for how to resolve this, that'd be great. Thank you!</p>
  
  <p>A full version of my code is below:</p>
  
  <pre><code>main ();
  function main() {
  
  var file_name = new Date() + "-Title-Project_Name-Page_Location";
  var filepath = "~/Desktop/" + file_name + ".txt";
  var write_file = File(filepath);
      write_file = new File(filepath);
      var write_text;
          write_text = write_file.open('w', undefined, undefined);
          write_file.encoding = "UTF-8";
          write_file.lineFeed = "Macintosh";
          #target indesign;
          var doc = app.activeDocument;
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
              app.findChangeTextOptions.includeLockedLayersForFind = true;
              app.findChangeTextOptions.includeLockedStoriesForFind = true;
              app.findChangeTextOptions.includeHiddenLayers = false;
              app.findChangeTextOptions.includeMasterPages = false;
              app.findChangeTextOptions.includeFootnotes = true;
              app.findChangeTextOptions.caseSensitive = false;
              app.findChangeTextOptions.wholeWord = false;
              project_name = "Project Name";
              app.findTextPreferences.findWhat = project_name;
                  find_txt = doc.findText();
                  for (var i = 0; i &lt; find_txt.length; i++) {
                      if (find_txt[i].contents == project_name) {
                          found_txt = find_txt[i];
                          pg_nr = found_txt.parentTextFrames[0].parentPage.name;
                          write_file.writeln(project_name + " : p. " + pg_nr + "\r\r");
                          }
                      }
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
      write_file.close();
  return;
  }
  </code></pre>