5 Replies Latest reply on Jul 19, 2017 10:34 AM by lf.corullon

    Extract words with specific style applied

    Ribnog Level 1

      Hi,

       

      Is possible extract words with a specific character style applied to other indesign document or a txt file using a script?

       

      Thank you in advance!

        • 1. Re: Extract words with specific style applied
          lf.corullon Level 3
          var doc = app.activeDocument;
          var list = [];
          var sel = app.selection[0].parentStory;
          
          app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
          app.findGrepPreferences.appliedCharacterStyle = doc.characterStyles.itemByName("test");
          var found = sel.findGrep();
          app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
          
          for (var i=found.length-1; i>=0; i--) {
              list.push(found[i].contents);
              }
          alert(list.join("\r"));
          

           

          Try this.

          1 person found this helpful
          • 2. Re: Extract words with specific style applied
            Ribnog Level 1

            Hi!

             

            Your code works only if the text frame is selected. I need to export all words of the document.

             

            And instead of a list I want a txt file with the words. Maybe using the "ExportFormat.textType" command.

             

            Can you help me? Thank you for your attention.

            • 3. Re: Extract words with specific style applied
              lf.corullon Level 3

              Try this:

               

              var doc = app.activeDocument;
              var list = [];
              //~ var sel = app.selection[0].parentStory;
              
              
              app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
              
              
              app.findGrepPreferences.appliedCharacterStyle = doc.characterStyles.itemByName("test");
              //~ var found = sel.findGrep();
              var found = doc.findGrep();
              
              
              app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
              
              
              for (var i=0; i<found.length; i++) {
                  list.push(found[i].contents);
                  }
              //~ alert(list.join("\r"));
              
              
              var myFile = new File(Folder.desktop + "/" + app.activeDocument.name.replace(/\.indd$/i, ".txt"));
              if (File.fs == "Windows")
              var listFile = myFile.saveDlg("Save list", "Plain text file: *.txt" );
              else
              listFile = myFile.saveDlg("Save list");
              if (listFile != null)
              {
              if (listFile.open("w"))
              {
              listFile.encoding = "utf8"; //ENCONDING
              listFile.write(list.join("\r") + "\r"); //WRITE THE ARRAY TO TXT FILE
              listFile.close(); //CLOSE TXT FILE
              listFile.execute(); //OPEN TXT FILE
              }
              }
              
              1 person found this helpful
              • 4. Re: Extract words with specific style applied
                Ribnog Level 1

                Thank you so much! Perfect!

                 

                I have a last wish if you could help.

                 

                I have some words that are duplicated across the document and your script get all of them. There's a way to get only one instance of each word?

                 

                I promise not to bother you anymore.

                 

                lf.corullon

                • 5. Re: Extract words with specific style applied
                  lf.corullon Level 3
                  var doc = app.activeDocument;
                  var list = [];
                  if (app.selection.length != 1) {
                      var sel = doc;
                      }
                  else {
                      var sel = app.selection[0].parentStory;
                      }
                  
                  
                  app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                  app.findGrepPreferences.appliedCharacterStyle = doc.characterStyles.itemByName("test");
                  var found = sel.findGrep();
                  app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                  
                  
                  for (var j=0; j<found.length; j++) {
                      list.push(found[j].contents);
                      }
                  //~ alert(list.join("\r"));
                  
                  
                  function unique(list) {
                      var o = {}, i, l = list.length, r = [];
                      for(i=0; i<l;i++) o[list[i]] = list[i];
                      for(i in o) r.push(o[i]);
                      return r;
                      };
                  var listFinal = unique(list).join("\r");
                  alert(listFinal);
                  
                  
                  var myFile = new File(String(app.activeDocument.fullName).replace(/\.indd$/i, ".txt"));
                  if (File.fs == "Windows")
                  var listFile = myFile/*.saveDlg("Save list", "Plain text file: *.txt" )*/;
                  else
                  listFile = myFile/*.saveDlg("Save list")*/;
                  if (listFile != null)
                  {
                  if (listFile.open("w"))
                  {
                  listFile.encoding = "utf8"; //ENCONDING
                  listFile.write(listFinal + "\r"); //WRITE THE ARRAY TO TXT FILE
                  listFile.close(); //CLOSE TXT FILE
                  //~ listFile.execute(); //OPEN TXT FILE
                  }
                  }
                  
                  1 person found this helpful