15 Replies Latest reply on Mar 15, 2012 10:58 AM by leowoosh

    Is there a script for applying a certain style to several thousands of words arranged in a list?

    leowoosh Level 1

      Hello, I have tried using FindChangeByList, but I'd pretty much do all the work of pasting every word on the brackets, so i could as well apply the styles by hand.

       

      So my project involves applying the same style to around 11 thousand words that appear in a book. Those words are mostly names and brands, and they appear throughout the whole text, so applying that style manually would be quite time-consuming.

      I have those words set aside in a list.txt

       

      Does anyone know if I could use that in any script and apply the style automatically?

      Like I said, pasting every single word into FindChangeByList is out of the question, just like applying the style word by word, it would take forever...

       

       

      ...thanks!

        • 1. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
          leowoosh Level 1

          Got a second question about Findchangebylist:

          If I wanted to find a specific word amongst ONLY the words that are in a certain paragraph style, and then change that word's CHARACTER style, would that be possible?

          • 2. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
            [Jongware] Most Valuable Participant

            Piece of cake. Since you didn't specify, this script assumes your text file is called "list.txt" and is located in your user documents folder (line 1), and your style is a Character Style called "yourStyle" (line 9).

             

            textFile = File(Folder.myDocuments+"/list.txt");
            if (textFile.open("r") == false)
            { alert ("yeah ... that file does not exist"); exit(0); }
            strs = textFile.read ();
            textFile.close();
            strs = strs.split("\n");
            app.findGrepPreferences = null;
            app.changeGrepPreferences = null;
            app.changeGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item("yourStyle");
            while (strs.length > 0)
            {
            app.findGrepPreferences.findWhat = "\\b"+strs.pop()+"\\b";
            app.activeDocument.changeGrep();
            }
            
            • 3. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
              leowoosh Level 1

              Thanks Jong, it works great. How about considering only words that are in list.txt and are under paragraph style "MyParaStyle" in the Indesign document? Any ideas?

               

              I can't thank you enough for saving me so much time!

              • 4. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                leowoosh Level 1

                nevermind, I found out how to do that by simple changing app.findGrepPreferences

                to

                app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item(MyParaStyle)

                 

                thanks again, Jong!

                • 5. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                  [Jongware] Most Valuable Participant

                  leowoosh wrote:

                   

                  [..] If I put a name on the list like "[name]" or "name," without the quotation marks, but with the comma and with the brackets, they are not recognized by the script, in a sense that the script doesn't apply the style to them. It works perfectly with just "name", but it doesn't if I need it to recognize some kind of punctuation along with the text.

                  Are there any sintaxes or simple workarounds to that?

                   

                  This script uses GREP to find the text because it has a few convenient shortcuts: adding word breaks \b before and after the to-search string ensures "entire word matching", and GREP is by default case sensitive.

                   

                  I can't test right now but I suspect the word breaks are interfering with your extra punctuation. Try it directly in InDesign, with and without punctuation and with and without the \b breaks.

                   

                  If it *is* because of this word breaks, then there is no one-line workaround.

                  1 person found this helpful
                  • 6. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                    leowoosh Level 1

                    Okay, tested it directly in Indesign.

                     

                    \btest did not apply to "test," but it did apply to "test"

                     

                    So I guess I'm screwed haha

                    But thanks for the help!

                    • 7. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                      [Jongware] Most Valuable Participant

                      Well, perhaps can you alter your word list?

                       

                      If a word starts with a letter, add \b before it. If not and there is some sort of punctuation, that should already be a safe "word break". Same at the end of each word.

                       

                      If you do this all you need to remove adding the \\b before and after the search string.

                      • 8. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                        leowoosh Level 1

                        Altering the list is no problem.

                        But when I simply change the "\\b" for "" it applies the style to the whole text, not only list items with \b on it or not.

                        Is that what you meant by removing \\b? Pretty sure I made a newbie mistake and didn't understand what I should have done to "remove \\b".

                        • 9. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                          [Jongware] Most Valuable Participant

                          Uh, you must have done something wrong there. Removing the "\\b" stuff from the find script does not change anything else EXCEPT it will not search for "entire words" anymore. That means that in its oriignal for, in the text "hellothere" the word "there" will not be found (if "there") is i your list. Without the word breaks it WILL be found -- and, I assume, you don't want that. Nothing there that makes it suddenly match complete paragraphs.

                           

                          A solution is to put the exact string to find into your word list: change the entry "there" to "\bthere\b" and it will look again for just that word and nothing more.

                           

                          Thinking: A possible source of confusion might be this: in a Javascript command, the backslash \ is a special Javascript command. Therefore to get one single backslash in a string (such as in the find what text), I needed TWO. That's still a special command, but in this case it merely means "Yeah I actually need one".

                           

                          In the TEXT file, however, there is nothing wrong with backslashes. Not even when it gets read into Javascript -- JS does not attempt to interpret any random text you read in that way. So in the TEXT file you can write just one single backslash before each 'b'.

                           

                          (More Thinking) It cannot be the cause for the inexplicable match-everything you are seeing now either.

                          • 10. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                            leowoosh Level 1

                            Just to get it clear, I'm supposed to change this:

                             

                            app.findGrepPreferences.findWhat = "\\b"+strs.pop()+"\\b";

                             

                            to this:

                             

                            app.findGrepPreferences.findWhat = strs.pop();

                             

                             

                             

                            Is that about right? Because I'm still getting all my text matched to the search, and I already have a list with only ONE word, like \btest\b

                             

                            Also, in a ideal scenario, would I be able to match punctuation and stuff using that resource you are trying to teach me how to use? Stuff like:  \b[test word, using brackets and commas!]\b

                            • 11. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                              [Jongware] Most Valuable Participant

                              Yeah your proposed change *ought* to work. Weird--can't think of any reason why it would not.

                               

                              You can put anything in the Find List file but *you have got to abide to GREP Syntax*. That is, \b would work as it does (scan for word breaks) but you might get a nasty suprise with this sample sting, because in GREP syntax "[...]" means "any single character in this list".

                              • 12. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                                leowoosh Level 1

                                Yeah well, avoiding the "[...]" is no biggie either... but adding commas to some words to be found would help a lot. I'll keep cracking my head trying to solve this issue with the 100% text matching... thanks, yet again.

                                • 13. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                                  [Jongware] Most Valuable Participant

                                  leowoosh wrote:

                                  nevermind, I found out how to do that by simple changing app.findGrepPreferences

                                  to

                                  app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item(MyParaStyle)

                                   

                                  Could it be that you also set the new paragraph style in the change?

                                  • 14. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                                    leowoosh Level 1

                                    Don't think so. This is the complete code:

                                     

                                        var descontinuados = "/list.txt"                                            //list file

                                        var CharStyleGroup = "bold and colored"                              //character style group

                                        var CharStyle = "bold03 color11"                                        //character style name

                                        var ParaStyleGroup = "basic paragraphs"                             //paragraph style group

                                        var ParaStyle = "basic003"                                                //paragraph style name

                                        textFile = File(Folder.myDocuments+descontinuados);

                                        if (textFile.open("r") == false)

                                        { alert ("file not found"); exit(0); }

                                        strs = textFile.read ();

                                        textFile.close();

                                        strs = strs.split("\n");

                                            app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.item(ParaStyleGroup).paragraphStyles.item(ParaSty le)

                                            app.changeGrepPreferences = null;

                                            app.changeGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyleGroups.item(CharStyleGroup).characterStyles.item(CharSty le);

                                                while (strs.length > 0)

                                            {

                                            app.findGrepPreferences.findWhat = strs.pop();

                                            app.activeDocument.changeGrep();

                                            }

                                        }

                                    • 15. Re: Is there a script for applying a certain style to several thousands of words arranged in a list?
                                      leowoosh Level 1

                                      Okay, for anyone that might be interested in this as I was, I found a solution.

                                       

                                      If you want to have a particular text expression on your .txt list to be used as a search parameter by your script, here are a few tips:

                                      Let's say you want to add the following 2 expressions on your list, without the quotation marks:

                                      a) "test, test."

                                      b)"[test] {test}"

                                       

                                      1 - Like Jongware said, you MUST abide by GREP syntax.

                                      2 - If you want to put an expression on your .txt list that DOES NOT uses characters that represent GREP syntax (example a) with regular text, commas and simple punctuation), you may simply add them to your list between parentheses:

                                           a) (test, test.)

                                      3 - If you want the script to search for an expression that DOES CONTAIN characters that are also used by GREP syntax (example b) you can try stuff like:

                                           b) (\p{Ps}test\p{Pe})

                                           note the parentheses, because it's a specific text expression

                                           then note the \p{Ps} and \p{Pe} that mark opening or (s)tarting and closing or (e)nding punctuation such as brackets, braces, parentheses...

                                       

                                      Here is a file with the GREP expressions that helped me solve my problems:

                                      http://www.kahrel.plus.com/indesign/grep_mapper.pdf

                                       

                                      Although this is a very specific matter and probably easily solvable by all the professional scripters here, I thought I'd share it since it took 3 whole days of studying for me to conclude everything.

                                       

                                      Thanks again, specially to Jongware