11 Replies Latest reply: Apr 27, 2012 10:24 PM by Green4ever RSS

    replacing text that maintains styling issue

    bduffy323 Community Member

      I am trying to replace text in a textframe and have it maintain the styling that is applied to the text that I am replacing. For example I want to replace:

       

      <text1>

      <text2>

       

      with

       

      this text is bold

      this text is italic

       

      The code I am using is just a simple javascript regex to replace the text:

       

      var myContents = new String(myPageItem.parentStory.contents);
      
      
      var re1 = new RegExp("<copy1>","gi");
      myContents = myContents.replace(re1,"this text is bold");
      
      
      var re2 = new RegExp("<copy2>","gi");
      myContents = myContents.replace(re2,"this text is italic");
      
      
      myPageItem.contents = myContents;
      
      

       

      However, after running the code above I get:

       

      this text is bold

      this text is italic

       

      It seems that only seven characters (which coincidently <copy1> also has seven characters) gets the first style, and the rest of the text after the 7 characters gets the second style. ANY help would be greatly appreciated!

       

       

      Thanks in advanced.

        • 1. Re: replacing text that maintains styling issue
          [Jongware] CommunityMVP

          Messing with contents is tricky, because this changes just the characters "inside" the text -- it has no effect on formatting.

           

          See http://forums.adobe.com/thread/791512?tstart=0

          • 2. Re: replacing text that maintains styling issue
            bduffy323 Community Member

            An excellent referral! I will post my code that worked for me so if somebody finds this forum post before they find the other it might be a quicker read! This is my code that is working!

             

             

            for (var j=0;j<currentItems[i].paragraphs.length;j++)

                                                    {

             

                                                              var thisParagraph = currentItems[i].parentStory.paragraphs[j];

                                                              var mySearch = "<"+vAliasObj[myAlias]+">";

                                                              var myReplace = unescape(contentObj[myAlias]);

                                                              p = thisParagraph.texts[0].contents.indexOf(mySearch);

                                                              q = 0 <= p && (-1 + p + mySearch.length);

                                                              if( q ) thisParagraph.characters.itemByRange(p, q).texts[0].contents = myReplace;

             

                                                    }

            • 3. Re: replacing text that maintains styling issue
              bduffy323 Community Member

              Another question for you Jongware.

               

              I have this working fine and would like to tag the text that I am replacing. I have it working fine if the text is all on one line, however, when the text is on multiple lines I get an error. Do you see a correct way of doing this?

               

              works great if replacing <text1> with "this is text 1"

              for (var j=0;j<currentItems[i].paragraphs.length;j++)
               {
              
                         var thisParagraph = currentItems[i].parentStory.paragraphs[j];
                         var mySearch = "<"+vAliasObj[myAlias]+">";
                         var myReplace = unescape(contentObj[myAlias]);
                         p = thisParagraph.texts[0].contents.indexOf(mySearch);
                        q = 0 <= p && (-1 + p + mySearch.length);
                        if( q ) thisParagraph.characters.itemByRange(p, q).texts[0].contents = myReplace;
                        
                         temp = parseInt(myReplace.length);                                                        
                      app.activeDocument.xmlElements[0].xmlElements.add(myAlias, thisParagraph.characters.itemByRange(p,temp-1).texts[0]);
                   
              
                 }
              

               

              does not work if replacing <text1> with "this is on \r two lines"

              • 4. Re: replacing text that maintains styling issue
                [Jongware] CommunityMVP

                >Another question for you Jongware.

                 

                Just for me? Oh come on, let the others have a bit of fun as well.

                 

                I cannot try your code because it contains too many errors (vAliasObj? myAlias? etc.). Please write a minimal piece of code that shows the behaviour you mention.

                • 5. Re: replacing text that maintains styling issue
                  bduffy323 Community Member

                  By all means I hope the entire community participates! I've learned so much from you guys already.

                   

                   

                  Here is the code and an example of how it should be used:

                   

                  The document should have only one text frame on it that looks like this:

                  example.PNG

                   

                  When you run the script both of those lines will be tagged correctly. However that is only because they are on separate lines and the inserted text is only one line. Ultimately i'm looking for a way to tag the text that is replacing each of my <***> searches no matter how the <***> appear in the textframe and no matter how the inserted text is formated (ie. multiple lines).

                   

                   

                   

                  var myDoc = app.activeDocument;
                  
                  
                  var myTextFrame = app.selection;
                  var vAliasObj = [];
                  vAliasObj["text1"] = "overline";
                  vAliasObj["text2"] = "mainline";
                  
                  
                  var contentObj = {text1:"this  is  the first  text",text2:"this is the second text"};
                  
                  
                  var myTextFrame =myDoc.allPageItems[0];
                  
                  
                  var origPara = myTextFrame.paragraphs.length;
                  for (var myAlias in vAliasObj)
                  {
                    
                            for (var j=0;j<myTextFrame.paragraphs.length;j++)
                            {
                    
                                      var thisParagraph = myTextFrame.parentStory.paragraphs[j];
                                      var mySearch = "<"+vAliasObj[myAlias]+">";
                                      var myReplace = unescape(contentObj[myAlias]);
                                      p = thisParagraph.texts[0].contents.indexOf(mySearch);
                    
                                      q = 0 <= p && (-1 + p + mySearch.length);
                  
                  
                                      if(q) 
                                      {
                    
                                                thisParagraph.characters.itemByRange(p, q).texts[0].contents = myReplace;
                                                temp = parseInt(myReplace.length);
                                                app.activeDocument.xmlElements[0].xmlElements.add(myAlias, thisParagraph.characters.itemByRange(p,temp-1).texts[0]);
                                      }
                    
                            }
                  }
                  
                  

                   

                   

                  Any ideas?

                  • 6. Re: replacing text that maintains styling issue
                    bduffy323 Community Member

                    Well I figured it out! Instead of traversing the content in paragraphs I use Words. That gives me finer control. For anyone who might be trying to do the same thing, here is the code!

                     

                     

                    var myDoc = app.activeDocument;
                    
                    
                    var myTextFrame = app.selection;
                    var vAliasObj = [];
                    vAliasObj["text1"] = "overline";
                    vAliasObj["text2"] = "mainline";
                    
                    
                    var contentObj = {text1:"this \r is \r the first  text",text2:"this is the second text"};
                    var myTextFrame =myDoc.allPageItems[0];
                    
                    newWay();
                    
                    function newWay()
                    {
                      
                              var start =0;
                              var end   =0;
                              var aliasCheck = "";
                              var replace = false;
                              var deleteWordCount = -1;
                    
                    
                              for (var j=0;j<myTextFrame.words.length;j++)
                              {
                      
                                        if (myTextFrame.words[j].contents.indexOf("<")>-1)
                                        {
                      
                                                  start=j;
                    
                    
                                                  if (myTextFrame.words[j].contents.indexOf(">")>-1) 
                                                  {
                                                            deleteWordCount++;
                                                            end = j;
                                                            aliasCheck = myTextFrame.words[j].contents.substring(1,myTextFrame.words[j].contents.length-1);
                                                            replace=true;
                                                  }
                                                  else
                                                  {
                    
                    
                                                            for (var i = j; i < myTextFrame.words.length; i++) 
                                                            {
                                                                      aliasCheck+=myTextFrame.words[i].contents+" ";
                                                                      deleteWordCount++;
                                                                      if (myTextFrame.words[i].contents.indexOf(">")>-1) 
                                                                      {
                                                                                end = i;
                                                                                aliasCheck = aliasCheck.substring(1,aliasCheck.length-2);
                                                                                replace=true;
                                                                                break;
                                                                      }
                                                            }
                                                  }
                                        }
                    
                    
                                        if (replace)
                                        {
                                                  for (var myAlias in vAliasObj)
                                                  {
                                                            if (aliasCheck==vAliasObj[myAlias]) 
                                                            {
                      
                    
                                                                      var oldWords = myTextFrame.words.length - deleteWordCount;
                                                                      myTextFrame.words.itemByRange(start,end).texts[0].contents =  unescape(contentObj[myAlias]);
                                                                      var newWords = myTextFrame.words.length;
                    
                    
                    
                    
                                                                      app.activeDocument.xmlElements[0].xmlElements.add(myAlias, myTextFrame.words.itemByRange(j,(j+(newWords-oldWords))).texts[0]);
                    
                    
                                                                      break;
                                                            }
                                                  }
                    
                    
                                                  replace=false;
                                                  start=0;
                                                  end=0;
                                                  aliasCheck="";
                                                  deleteWordCount = -1;
                      
                                        }
                      
                              }
                    }
                    
                    
                    
                    • 7. Re: replacing text that maintains styling issue
                      Green4ever Community Member

                      Hi,

                       

                      I have one simple doubt, could clarify it.

                       

                      q = 0 <= p && (-1 + p + mySearch.length);

                       

                      How this line worrks, what it means. I hope this is related to biwise operators.

                      • 8. Re: replacing text that maintains styling issue
                        bduffy323 Community Member

                        To be honest Green4ever, that segment of code was what I copied from Jongwares link that he posted. I was also confused by how that works which is why the final code I wrote went about the replacement in a different way. If you would still like some clarification I would suggest posting it on the other forum.

                        • 9. Re: replacing text that maintains styling issue
                          Green4ever Community Member

                          Hi Jongware,

                          I have one simple doubt, could clarify it.

                           

                          q = 0 <= p && (-1 + p + mySearch.length);

                           

                          How this line works, what it means. I hope this is related to bitwise operators.

                          Could you please explain what it does actually?

                           

                          Thanks,

                          Green4ever

                          • 10. Re: replacing text that maintains styling issue
                            John Hawkinson CommunityMVP

                            Green4ever wrote:

                            Could you please explain what it does actually?

                            Please don't post the same thing twice.

                            Especially without trying!

                            See my answer in http://forums.adobe.com/message/4366682#4366682

                            • 11. Re: replacing text that maintains styling issue
                              Green4ever Community Member

                              Hi John,

                               

                              First I posted here, Since it is a answered question i'm not getting the response from anyone.

                              Apr 27, 2012 7:06 AM (in response to bduffy323)

                              After that only I posted it as a new dicussion.

                               

                              Apr 27, 2012 10:09 PM

                              Please don't post the same thing twice.

                              Especially without trying!

                              See my answer in http://forums.adobe.com/message/4366682#4366682

                              I am already following your answer in the thread you mentioned. Please check the timing if you any doubt.

                               

                              ---

                              Edit:

                              I need know how the below line works,See this thread http://forums.adobe.com/message/4364545#4364545

                              I already metioned the link of this thread. Did you clicked on it?

                               

                              Thanks,

                              Green4ever