10 Replies Latest reply on Mar 24, 2011 3:45 AM by Iain Robinson

    help with search and replace script please

    Iain Robinson

      Hi all

       

      I've written a javascript which uses various GREP and text find/changes to copy some text from the first two paragraphs of a document and use them elsewhere (in the running head and footers) and now I want to delete the first two paragraphs. Not sure how I would do that. If I was doing a manual find/change (with nothing selected) it would automatically select the first para in the first text box but when I run teh following;

       

      app.findGrepPreferences.findWhat = "^.+$";
      app.changeGrepPreferences.changeTo="";
      app.activeDocument.changeGrep();

       

      it finds all paragraphs and deletes them. I was going to include the code twice; once to delete the first paragraph and then again to delete the other (new first) paragraph. My JS skills are shaky but I'm quite comfortable with GREP. Can anyone point me in the right direction for how I acheive what I want? If I did a for loop surely I'd just be doing the same thing but more than once!

       

      Much to my surprise the rest of the script is working although it made my brain hurt!

       

      thanks for any suggestions,

      Iain

        • 1. Re: help with search and replace script please
          [Jongware] Most Valuable Participant

          The command "changeGrep" does a "change all" -- there is no equivalent of the 'find next' and 'change, find next' buttons.

           

          Ordinarily, all that's needed is to remove the first paragraph from an active story is this:

           

          myStory.paragraphs[0].remove();

           

          (and I'm pretty sure you can work it out, since you are already copying from it) but just for fun you could try this Pure GREP solution:

           

          app.findGrepPreferences.findWhat = "\A.*\r";

           

          again replacing with nothing.

           

          (It's better to use the 'remove' function, though.)

          • 2. Re: help with search and replace script please
            Green4ever Level 3

            If your first two paragraphs has unique paragraph style names then you can inlcude the following in your script,

             

            app.findGrepPreferences.appliedParagraphStyle = "parastyle name";

             

            Be sure that this style is applied only to the the paragraph that you want to remove.

            app.activeDocument.changeGrep();

            If you this command this is equal to "Change All " option in the find/change palette.

            • 3. Re: help with search and replace script please
              Iain Robinson Level 1

              Jongware - thanks, I think that's what I was looking for. I couldn't remember whether there was a 'find start of story' symbol (\A) in ID although I thought there was one - it's not on Mike Witherell's handy crib sheet (at least not the CS3 one). Thanks - I think you've saved me again! I couldn't think how to do it in JS but I've got your snippet to work on an isolated test document but when I try it on my main test doc I've still got a problem. If I add the following code

               

              myStory=app.activeDocument.stories.everyItem();
              myStory.paragraphs[0].remove();

               

              it removes my footer and running head paragraphs. If I do an alert(myStory.paragraphs[0].contents) before they are removed it doesn't just show the first paragraph in my document it shows the copy for the running heads three times and then the footer copy. I tried to get it to make myStory equal only the stories in the first text frame in the document but I couldn't get that to work. Any thoughts?

               

               

              Green4ever - unfortunately at the stage this script will be run nothing will be styled. In fact this script contains various GREPs that will apply styles throughout the file automatically. Mind you now I know how to find the first paragraph I can now apply that style here!

               

               

              cheers,

              Iain

              • 4. Re: help with search and replace script please
                Iain Robinson Level 1

                I can do it with GREP of course but I'd like to learn why Jongware's method isn't working in this situation.

                • 5. Re: help with search and replace script please
                  [Jongware] Most Valuable Participant

                  Oooh. After

                   

                  myStory=app.activeDocument.stories.everyItem();

                  the variable "myStory" won't point to just a single story, but ... to "every story". So that's why the next line removes more than you intended!

                  What the "everyItem" thingy internally does is still unclear to me, but that doesn't keep me from using it -- but only when you absolutely, positively want to perform an action on every item

                   

                  Now the usual examples say something like

                   

                  myStory = app.activeDocument.stories.item(0); // access first story

                   

                  but that's No Good. Even if your first story is the one you are targeting today, tomorrow, in a different document, it'll be story #1, or #5, or #255. There is no general rule that states the first story is always the largest one, or something like that.

                   

                  If this is going to be used with a document template, the best way to identify the target story is by giving the first text frame a unique label. Otherwise, you'll have to think of something else ... A couple of other strategies:

                   

                  1. You are already using GREP to find stuff. If one of those searches is guaranteed to return just text in the one story you are interested in, you can use this.

                  2. You mention headers and footers. If your main text is the only one that runs through multiple text frames, you can loop over all stories and check which one runs through than a single text frame.

                  3. ... (I'm pretty sure there are some more strategies ...)

                  1 person found this helpful
                  • 6. Re: help with search and replace script please
                    Iain Robinson Level 1

                    That's the trouble with cherry-picking bits of script I find from the net. It often works but can have unforeseen consequences if I don't fully understand what's going on. I need to spend a week in a dark room learning JS from the ground up but that's never going to happen! Just can't quite get my head round the object model, how different things relate and keep enough methods etc in my head for when I need them.

                     

                    Anyhoo, your code does find the first para but it is the running head copy on the master page. It will always be the first para on the first page (not a master page item) that I am trying to delete. Can we get it to look at the first page of the document so myStory is taken from there? I tried but I couldn't get the syntax (or more likely my understanding of the object model) correct. If not, I have done it with GREP and it works fine.

                     

                    thanks again,

                    Iain

                     

                    (like the Jackie Brown reference!)

                    • 7. Re: help with search and replace script please
                      [Jongware] Most Valuable Participant

                      That would be something like

                       

                      myStory = app.activeDocument.pages[0].textFrames[0].parentStory;

                       

                      function(){return A.apply(null,[this].concat($A(arguments)))}

                      (like the Jackie Brown reference!)

                       

                      Ooo you're sharp today. Bet most readers didn't pick that one up!

                       

                       

                       

                      • 8. Re: help with search and replace script please
                        Peter Kahrel Adobe Community Professional & MVP

                        Or just add this:

                         

                        app.findChangeGrepOptions.includeMasterpages = false;

                         

                        (off the top of my head, can't remember the exact formulation).

                         

                        Peter

                        • 9. Re: help with search and replace script please
                          Iain Robinson Level 1

                          That's it! It works now. I have got as far as myStory = app.activeDocument.pages[0].textFrames[0] but I didn't have parentStory.


                          Thanks Jongware - greatly appreciated.



                          I initially thought the reference was to Pulp Fiction but I watched JB again just a couple of months ago and it popped into my head. Great film - very underrated. What can I say - I'm a film geek!

                          • 10. Re: help with search and replace script please
                            Iain Robinson Level 1

                            Thanks Peter - I did use that when I did it with GREP and it works. I just wanted to try and get my head round the JS side of things.