25 Replies Latest reply on Apr 15, 2013 7:24 AM by CarlosCanto

    deleting text from text frame

    DuanesSearchForKnowledge Level 1

      my question is does anyone now the best way or anyway to remove text from a text frame?

      say if you want to remove anything in () in a text frame or any * symbols.

       

      52(one on both sides) //you have this in a text frame

       

      52 // and you want to change it to this by deleting (.)

       

      or

       

      40* //you have this in a text frame

       

      40 // and you want to change it to this by deleting the *

       

      any help would be great.

        • 1. Re: deleting text from text frame
          CarlosCanto Adobe Community Professional & MVP

          use the replace method, for example to remove all asterisks

           

          var string = '*52* 53 54 *55';
          var newstring = string.replace (/\*/g, '');
          alert(newstring);
          

           

          homework topics

          - string.replace(what, with)

          - javascript regular expressions (/\*/g) (or RegExp Object)

          • 2. Re: deleting text from text frame
            pixxxel schubser Level 5

            Try this:

            var aDoc = app.activeDocument;
            var aTFrame = aDoc.textFrames;
            var aString = "50*";
            for (i=aTFrame.length-1; i>0; i--) {
                contentString = aTFrame[i].contents;
                if (contentString.indexOf(aString) != -1) { //if 50* present in the text frame
                    //aTFrame[i].selected = true;
                    //alert (contentString);
                    aTFrame[i].contents = contentString.replace (/50\*/g, '50');
                    }
                }
            
            • 3. Re: deleting text from text frame
              DuanesSearchForKnowledge Level 1

              Thanks Carlos and P.S. I have been able to change the string but for some reason I cant get it to actually change the textframe in the document.

              Below is what I had before. TF 0 is 50* and when you read the alerts the first says 50* and the last one says 50, but the text frame in the file does not change. Do you know how to get it to change the text frame?

               

              var myDoc = app.activeDocument;

              var myText = myDoc.textFrames[0].contents;

              alert(myText);

              myText = myText.replace("*", '')

              alert(myText);

              • 4. Re: deleting text from text frame
                CarlosCanto Adobe Community Professional & MVP

                now you need to put that string back to the text frame

                 

                myDoc.textFrames[0].contents = myText;
                
                • 5. Re: deleting text from text frame
                  DuanesSearchForKnowledge Level 1

                  Eureka! Thanks Carlos! I'm going to have to wrap my head around that.

                   

                  now to get rid of () and everything in them I taught this would work;

                  myText2 = myText2.replace(/\(\.\)/, '')

                   

                  I thought a period stood for anything or all. not sure about that now.

                  I was trying to say (all), replace with nothing.

                  • 6. Re: deleting text from text frame
                    CarlosCanto Adobe Community Professional & MVP

                    this should do

                     

                    myText2 = myText2.replace(/\((.+)\)/, '');
                    
                    • 7. Re: deleting text from text frame
                      pixxxel schubser Level 5

                      @CarlosCanto,

                      your RegEx is "greedy".

                       

                      Perhaps this is better:

                       

                       

                      myText2 = myText2.replace(/\((.+?)\)/g, '');
                      
                      • 8. Re: deleting text from text frame
                        DuanesSearchForKnowledge Level 1

                        Thanks again Carlos!

                        It works for me now.

                         

                        script ------

                        addMoneyToSingleTF();

                        removeAstrisPerens();

                        //      Adds $ sign to all single digit numbers ----------------------------------------------------------------------------------------- ------------------------------------

                        function addMoneyToSingleTF() {

                        var mydoc = app.activeDocument;

                        var mylayer = mydoc.activeLayer;

                        var mytext = mylayer.textFrames;

                        var myTF_ByI;

                         

                        for (zz = 0; zz < mytext.length; zz++) {

                            myTF_ByI = mytext[zz];

                            myTF_ByI.contents = "$" + myTF_ByI.contents;

                            }

                        }

                         

                        function removeAstrisPerens() {

                        var myDoc = app.activeDocument;

                        var myLayer = myDoc.activeLayer;

                        var myTextFrame = myLayer.textFrames;

                        var myTextContent;

                        var newContentTextFrame;

                         

                        for (u = 0; u < myTextFrame.length; u++) {

                               myTextContent = myLayer.textFrames[u].contents;

                            myTextContent = myTextContent.replace(/\((.+)\)/, '') // to delete ()

                            myTextContent = myTextContent.replace(/\*/g, '') // to delete *

                            myLayer.textFrames[u].contents = myTextContent;

                            myLayer.textFrames[u].contents = myTextContent;

                         

                            }

                        }

                         

                        Do you know how to change the font size of only the first character in a textframe? I can change the complete text frame but I have failed every time I try to get it to change only the first character.

                         

                        var mydoc = app.activeDocument;

                        var myStyle = mydoc.textFrames[0].textRange.characterAttributes.size = 2;

                         

                         

                        alert(myStyle);

                        • 9. Re: deleting text from text frame
                          CarlosCanto Adobe Community Professional & MVP

                          you're welcome

                           

                          yes, use

                           

                          mydoc.textFrames[0].characters[0].characterAttributes.size = 2;
                          
                          • 10. Re: deleting text from text frame
                            CarlosCanto Adobe Community Professional & MVP

                            hi pixxxel, what do you mean? for some reason it works with (55) (54) and with ((55)), without the /g parameter

                            • 11. Re: deleting text from text frame
                              DuanesSearchForKnowledge Level 1

                              You have helped me yet again Carlos. I thank you. You are the next link in the evolutionary chain!

                              Carlos.png

                              • 12. Re: deleting text from text frame
                                CarlosCanto Adobe Community Professional & MVP

                                hahaha, thanks that's funny

                                • 13. Re: deleting text from text frame
                                  pixxxel schubser Level 5

                                  CarlosCanto wrote:

                                   

                                  hi pixxxel, what do you mean? for some reason it works with (55) (54) and with ((55)), without the /g parameter

                                   

                                  Hi Carlos,

                                   

                                  your RegEx always is ok - but only, if not more than one source in a line exists.

                                   

                                  e.g.:

                                  your

                                  searchString = /\((.+)\)/;
                                  

                                   

                                  works fine, if

                                  Line1: 52(55)
                                  Line2: 52(54)
                                  Line3: and with 52((55))
                                  

                                   

                                  But it's extremly dangerous to use, if more than one sources in a line exists

                                   

                                  Line4: 52(55) 52(54) and with 52((55)) or
                                  Line5: Test 51(No.1) Test 52(No.2) Test 53(No.3)
                                  

                                   

                                  At first: Please test this with

                                  searchString = /\((.+?)\)/;
                                  

                                  (.+? without /g) is not "greedy", but will not find all sources

                                   

                                   

                                  Second: Please test this with

                                  searchString = /\((.+?)\)/g;
                                  

                                   

                                  Only ((55)) will be difficult. I havent't tested before.

                                   

                                  In this case use better:

                                  searchString = /\((.+?)\){1,2}/g;
                                  

                                   

                                  I think you have the same result.

                                   

                                  • 14. Re: deleting text from text frame
                                    CarlosCanto Adobe Community Professional & MVP

                                    I see what you're saying, my test only worked with the scenario I tried, one set of parenthesis

                                     

                                    this does not work

                                    '(52) 53 54 (55)'; // returns '' (nothing)
                                    

                                     

                                    it takes the first '(52...parenthesis and goes all the way to ....55)'

                                     

                                    you're correct this is the winner for every number of parenthesis in any position

                                    /\((.+?)\){1,2}/g
                                    

                                     

                                    thanks!!

                                    • 15. Re: deleting text from text frame
                                      pixxxel schubser Level 5

                                      CarlosCanto wrote:

                                       

                                      … you're correct this is the winner for every number of parenthesis in any position

                                      /\((.+?)\){1,2}/g
                                      

                                       

                                      thanks!!

                                       

                                      You're welcome.

                                       

                                      I'm almost of the same opinion.

                                       

                                      I think RegEx is complicated, especially for beginners (You are advanced in any case.)  Oh god, my bad english. Hope so, you understand me right.

                                       

                                      And the last //searchSting is IMHO the second-best way.

                                       

                                       

                                      Perhaps this is the"royal road"?

                                       

                                      searchString = /\([^)]+\)+/g;
                                      

                                       

                                       

                                      • 16. Re: deleting text from text frame
                                        DuanesSearchForKnowledge Level 1

                                        Thats really great info guys. Thanks.

                                        How about this. say you have a text frames like this

                                         

                                        480 (used 11 times)

                                         

                                        (used 11 times) 480

                                         

                                        and you wanted to add $ Signs around the 480 what method or property could you use to divide the string up and how would you call the placement? Im tring to understand this regex stuff.

                                        • 17. Re: deleting text from text frame
                                          pixxxel schubser Level 5

                                          (The easiest way? Take your text, go to InDesign and use GREP search and replace.)

                                           

                                          For RegEx or GREP one thing is very important. It takes several examples (with maximum and minimum) in order to establish a firm rule can.

                                           

                                          (The parenthesis with contents remain the same?)

                                          Would you like new order the 480 (before whitespace and the parenthesis) after the parenthesis and whitespace and add a $ sign?

                                          Before or after the 480?

                                          With or without spaces?

                                          Can a date or other numbers before a parenthesis happen?

                                          Can other numbers exist?

                                          With 1, 2, 3, or how many digits?

                                          Once or more per line?

                                          One or more times per paragraph?

                                           

                                          The more precise the statement, the better.

                                          The more solid rules are known, the safer working RegEx or GREP.

                                          • 18. Re: deleting text from text frame
                                            CarlosCanto Adobe Community Professional & MVP

                                            I agree with pixxxel, pretty much you need to provide samples of all possible scenarios

                                            • 19. Re: deleting text from text frame
                                              DuanesSearchForKnowledge Level 1

                                              Luckily the files I am working with have stricked standards. The number 480 can be any 1-4 digit number, it is always bold and 10 point, any other number that could be in a text frame will not be bold but will be 10 point, and this number will never be in the parenthesis(there will never be more that on set of parenthesis). I would not want to move the 480 to the left. It can look either way,

                                              480 (used 11 times), or (used 11 times) 480 and be correct.

                                               

                                              I was thinking if i could look for bold 10 point characters that had a blank space before it put $ sign and if it bold 10 point with a blank space behind it, put a $ sign. But I think it would have to go by font like Arial-Bold, because the bold value is in the font.

                                               

                                              Does that sound like it could work to you guys?

                                              • 20. Re: deleting text from text frame
                                                CarlosCanto Adobe Community Professional & MVP

                                                this works for your scenario

                                                 

                                                select ONE textframe before running

                                                var idoc = app.activeDocument;
                                                var sel = idoc.selection[0];
                                                var string = sel.contents;
                                                var num = Number(string.replace (/\((.+)\)/, '')); // get number outside parenthesis
                                                var numdollar = string.replace(num, "$"+num); // search for this number and replece with $ + iteself
                                                ///alert(numdollar);
                                                sel.contents = numdollar;
                                                

                                                 

                                                edit:

                                                ...but format is not dealt with, need to work on that.

                                                • 21. Re: deleting text from text frame
                                                  pixxxel schubser Level 5

                                                  Once again: Please, can you give us some real examples?

                                                   

                                                  If you have more text in every line with 480 (used), than try this.

                                                  No selection required.

                                                   

                                                  // Find4digitNumberAndPutADollarSign
                                                  // examples are "Test 123 test 480 (test) test 123." and/or  "Test 123 test (test) 480 test 123."
                                                  // http://forums.adobe.com/thread/1187681
                                                  var aDoc = app.activeDocument;
                                                  var aTFrame = aDoc.textFrames;
                                                  var searchString1 = /\s?\$?(\s\d{1,4}\s\()/;
                                                  var searchString2 = /(\))\s?\$?(\s\d{1,4}\s)/;
                                                  
                                                  for (i=aTFrame.length-1; i>=0; i--) {
                                                  for (j=0 ; j<aTFrame[i].lines.length; j++) {
                                                      contentString = aTFrame[i].lines[j].contents;
                                                      clearString = contentString.match (searchString1);
                                                      contentString = contentString.replace (searchString1, ' $'+RegExp.$1);
                                                      clearString = contentString.match (searchString2);
                                                      contentString = contentString.replace (searchString2, RegExp.$1+' $'+RegExp.$2);
                                                      //alert(contentString);
                                                      aTFrame[i].lines[j].contents = contentString;    
                                                      }
                                                  }
                                                  redraw();
                                                  

                                                   

                                                   

                                                  The formatting is lost here too.

                                                   


                                                  • 22. Re: deleting text from text frame
                                                    DuanesSearchForKnowledge Level 1

                                                    this works for your scenario

                                                     

                                                    select ONE textframe before running

                                                    var idoc = app.activeDocument; var sel = idoc.selection[0]; var string = sel.contents; var num = Number(string.replace (/\((.+)\)/, '')); // get number outside parenthesis var numdollar = string.replace(num, "$"+num); // search for this number and replece with $ + iteself ///alert(numdollar); sel.contents = numdollar; 

                                                     

                                                    edit:

                                                    ...but format is not dealt with, need to work on that.

                                                     

                                                    ------------------------------------------------------------------------------------------ ----------------------------------------------------

                                                    Thanks Carlos, I see that you are stripping away everything but the number in a string and putting $ sign. I don't understand how putting that back in the sel.contents works, but it does. I have a long ways to go when it comes to understanding all this. Yes formatting is a problem. It seems anytime I use add or replace on a text frame with different fonts and sizes it makes them all whatever the first font happens to be.

                                                    • 23. Re: deleting text from text frame
                                                      DuanesSearchForKnowledge Level 1

                                                      Once again: Please, can you give us some real examples?

                                                       

                                                      If you have more text in every line with 480 (used), than try this.

                                                      No selection required.

                                                       

                                                      It would be only on layer and the layer could look like this.

                                                      sample(1).jpg

                                                      I tried your script and I saw no change except for one text frame's font was made all Arial-BoldMT. Don't know why.

                                                       

                                                      Thanks for the info.

                                                      • 24. Re: deleting text from text frame
                                                        pixxxel schubser Level 5

                                                        pixxxel schubser wrote:

                                                         

                                                        (The easiest way? Take your text, go to InDesign and use GREP search and replace.)

                                                         

                                                        For RegEx or GREP one thing is very important. It takes several examples (with maximum and minimum) in order to establish a firm rule can.

                                                         

                                                        (The parenthesis with contents remain the same?)

                                                        Would you like new order the 480 (before whitespace and the parenthesis) after the parenthesis and whitespace and add a $ sign?

                                                        Before or after the 480?

                                                        With or without spaces?

                                                        Can a date or other numbers before a parenthesis happen?

                                                        Can other numbers exist?

                                                        With 1, 2, 3, or how many digits?

                                                        Once or more per line?

                                                        One or more times per paragraph?

                                                         

                                                        The more precise the statement, the better.

                                                        The more solid rules are known, the safer working RegEx or GREP.

                                                         

                                                        CarlosCanto wrote:

                                                         

                                                        I agree with pixxxel, pretty much you need to provide samples of all possible scenarios

                                                         

                                                        @DuanesSearchForKnowledge,

                                                        do you remember these two posts?

                                                         


                                                        Your document is completely constructed differently than expected.

                                                        For this reason can not work my script.

                                                         

                                                         

                                                        Please do the following:

                                                        - press Alt + Ctrl + I

                                                        - select all

                                                        - make a screenshot with visible layers palette

                                                        • 25. Re: deleting text from text frame
                                                          CarlosCanto Adobe Community Professional & MVP

                                                          try this one, select one text frame before running

                                                           

                                                          // http://forums.adobe.com/thread/1187681?tstart=0
                                                          // add $ sign to number outside parenthesis ie '$480 (used 11 times)'
                                                          
                                                          var idoc = app.activeDocument;
                                                          var sel = idoc.selection[0];
                                                          
                                                          
                                                          var string = sel.contents;
                                                          var numbers = string.match(/\d+/g); // get both numbers
                                                          
                                                          
                                                          var num = string.search(numbers[0]); // get the first number's index
                                                          var paren1 = string.search(/\(/); // get the opening parenthesis index
                                                          
                                                          
                                                          // if the first number's index is higer than the opening parenthesis' index, then the other number is what we need
                                                          idx = num<paren1 ? num : string.search(numbers[1]); 
                                                          sel.insertionPoints[idx].characters.add('$');