7 Replies Latest reply on Jul 13, 2010 9:51 AM by Muppet Mark-QAl63s

    find n replace pricing in paragraphs…

    Muppet Mark-QAl63s Level 4

      OK, I have a rather large catalogue that used to be a QuarkXPress Job recently converted by plug-in and cleaned up. I now have the need to run thru this and make some changes. (Please bare in mind Im using CS2 so my find and change may NOT be up to this?) Here is an example of the contents of the price listed text frames…

       

       

       

      Picture 1.png

       

      What I need to do is replace the last 2 prices with just the one e.g. '£00.00' in the black. I will then be moving the tab stop positions. Now the example above is NOT the best as my prices can be of varying length '£1.23', '£12.34' or '£123.45' so is there a way I can use find n change in the GUI or would script be better suited to this? I think I could do multi f&r to something unique then replace that but its going to be too time consuming. Is it possible to use any digit {n} times? In Quark I could have it include '£' and '.' in the definition of word then just remove the last word. I hope thats fairly clear if NOT let me know. Thanks in advance…

       

        • 1. Re: find n replace pricing in paragraphs…
          [Jongware] Most Valuable Participant

          Hi Muppet,

           

          CS2 does not have the super-multifunctional GREP search & replace, but fortunately, the common Find/Change box allows a 'wildcard' number: ^9. You cannot use GREP-like repeat specifications, but I don't think one replace per number of leading digits is too much work ;-)

           

          So search for £^9.^9^9 and replace with £00.00, then search for £^9^9.^9^9, and so on. Even if your prices run into the millions, it'll only take 6 replaces!

          • 2. Re: find n replace pricing in paragraphs…
            Harbs. Level 6

            Theun's method is probably best, but you could use text.paragraphs.everyItem().characters.itemByRange(-9,-2).remove() which is similar to the way you were looking for...

             

            Harbs

            • 3. Re: find n replace pricing in paragraphs…
              Muppet Mark-QAl63s Level 4

              Jong, I will go run a check again… Im sure I tried several combinations of this what I tried was more like…

               

              find:

              £^9^9.^9^9^t£^9^9.^9^9^p

               

              replace with:

              £00.00^p

               

              I need to replace both sets of pricing at once and remove the tab in between. (I may well have overcomplicated things in the process?)

              When I tried a second combination it screwed up could have just used the wrong order?

              The price pairs can also be of a different length so my thinking was some thing like:

               

              £^9.^9^9^t£^9.^9^9^p

              £^9^9.^9^9^t£^9.^9^9^p

              £^9^9.^9^9^t£^9^9.^9^9^p

              £^9^9^9.^9^9^t£^9^9.^9^9^p

              £^9^9^9.^9^9^t£^9^9^9.^9^9^p

               

              Doing this with the option of 'story' and a selected text frame can be up to 1-20 price frames on a page and 400 pages is still going to take a while…

              Could only find styles in the options so I don't know how to restrict to only certain text frames.

               

              I would however prefer to do this with script (from the learning point of view) as much this is to be replaced at a later date with data from CSV file. There are several other operations that I require the re-spacing out the remaining tabbed contents etc. I have pricing else where in these documents that I don't want to affect. I can go the routes of working with the selected object (text frame) or (all my text frames if Im happy) were all named by script when in Quark and this has transfered to 'Script Labels' (Did NOT expect that to have happened but a big bonus)…

               

              I will continue with some testing…

              • 4. Re: find n replace pricing in paragraphs…
                Harbs. Level 6

                You could use GREP expressions to find contents of text (which is in string form).

                 

                You can then use that to either do search & replace, or modify the text directly.

                 

                Peter Kahrel dedicated a section in his scripting e-book (for CS2) on this topic...

                 

                Harbs

                • 5. Re: find n replace pricing in paragraphs…
                  Muppet Mark-QAl63s Level 4

                  Harbs, I will see if your 'itemsByRange' is any better… Here is where I was at mean while. I can remove but not replace/add characters back the 00.00

                  It is a bit slow though… Do I change the content string to add back? I do need to retain formatting/styles.

                   

                  #target indesign
                  
                  function removePrices() {
                       if (app.documents.length == 0) {
                            alert('Please have an "InDesign" document open before running this script.');
                            return;
                       }
                       var docRef = app.activeDocument;
                       // Just using a selected box to test
                       with (docRef.selection[0]) {
                            // Missout first n last paras
                            for (var i = 1; i < paragraphs.length-1; i++) {
                                 var a = paragraphs[i].contents.indexOf('£');
                                 var b = paragraphs[i].contents.length-2;
                                 for (var c = b; c > a; c--) {
                                      paragraphs[i].characters[c].remove();
                                 }
                            }
                            // No return at end of this one
                            var a = paragraphs[i].contents.indexOf('£');
                            var b = paragraphs[i].contents.length-1;
                            for (var c = b; c > a; c--) {
                                 paragraphs[i].characters[c].remove();
                            }          
                       }
                  }
                  
                  removePrices()
                  

                  • 6. Re: find n replace pricing in paragraphs…
                    Muppet Mark-QAl63s Level 4

                    Replacing those for loops and going for itemByRange did speed up things quite a bit so thanks harbs…

                    The quoted book which Im sure is very good However…

                     

                    myScriptingBudget == myReplacementText.valueOF(); or something very close to that…

                    • 7. Re: find n replace pricing in paragraphs…
                      Muppet Mark-QAl63s Level 4

                      Well, I had a little time to dig some further and this is almost there even if a little ugly…

                       

                      #target indesign
                      
                      function removePrices() {
                           if (app.documents.length == 0) {
                                alert('Please have an "InDesign" document open before running this script.');
                                return;
                           }
                           var docRef = app.activeDocument;
                           // Just using a selected box to test
                           with (docRef.selection[0]) {
                                // Missout first n last paras
                                for (var i = 1; i < paragraphs.length-1; i++) {
                                     var a = paragraphs[i].contents.indexOf('£') + 1;
                                     var b = paragraphs[i].contents.length-2;               
                                     paragraphs[i].characters.itemByRange(a,b).remove()
                                     paragraphs[i].words.lastItem().contents = '£00.00';               
                                }
                                // No return at end of this one
                                var a = paragraphs[i].contents.indexOf('£') + 1;
                                var b = paragraphs[i].contents.length-1;          
                                paragraphs[i].characters.itemByRange(a,b).remove();
                                paragraphs[i].words.lastItem().contents = '£00.00';
                                // Remove my last tab
                                paragraphs.everyItem().tabStops.lastItem().remove();
                                // Move my tabs and respace content
                                paragraphs.everyItem().tabStops.lastItem().position = 77;
                                /*
                                This did NOT work but I was hardly surprised!!!
                                paragraphs.everyItem().tabStops.lastItem().previousItem().position = 67;
                                */
                                var c = paragraphs[1].tabStops.length-2;
                                paragraphs.everyItem().tabStops.item(c).position = 67;
                           }
                      }
                      
                      removePrices();