1 2 Previous Next 40 Replies Latest reply on Mar 25, 2017 3:02 PM by thaero17058

    Find text then add a character style script

    thaero17058

      Hello

      I am trying to make a script to find any text between two glyphes ( ﴾ ﴿ ) and create a new character style for the selected text .

      note : i do not want to apply a character style on the selected text , but i want to create a new character style for the selected text .

      note : i do "find what "correctly but i do not know how to complete the script .

      i am start but i do not know how to complete  :

       

       

      function main()      

          { 

              var myDoc = app.activeDocument, 

              FindWhat = "(?<=\﴿).+?(?=\﴾)";  // The 2 glyphes are " ﴿ " and " ﴾ "  

        • 1. Re: Find text then add a character style script
          Ananth@desgin Level 2

          Hi,

          Just one clarification. Your requirement is create new Cstyles on selected text..

          Do you give inputs of style properties? or script will create the new Cstlyes..?

          1 person found this helpful
          • 2. Re: Find text then add a character style script
            thaero17058 Level 1

            Hi

            No , I want script create the new Character Style ... no inputs for style properties .

            • 3. Re: Find text then add a character style script
              Ananth@desgin Level 2

              Hi,

              Try this code..

              app.findGrepPreferences = app.changeGrepPreferences =null;
              app.findGrepPreferences.findWhat = "" // apply your grep code 
              var found = app.documents[0].findGrep();
                  var new_Style = app.documents[0].characterStyles.add()
                  new_Style.name = "my_style"; // your style name 
              //~     new_Style.fontStyle = "Italic" // create your style properties
              //~     new_Style.pointSize = 28;
              //~     // give ur inputs
                   for(i=0; i<found.length; i++)
              {
                  if(found[i].contents)
                  {
                   found[i].select();
                   app.changeGrepPreferences.appliedCharacterStyle=""; // style name
                  found[i].changeGrep();
                  }  
              }
              
                  app.findGrepPreferences = app.changeGrepPreferences =null;
                  
              
              1 person found this helpful
              • 4. Re: Find text then add a character style script
                thaero17058 Level 1

                Hi

                it's not  working ... because : i do not try to apply a character style for the selected texts .

                i want to create a character style for each selected text ...

                • 5. Re: Find text then add a character style script
                  tpk1982 Level 4

                  I hope you meant to create style based on text selected.. i edited the script what Ananth provided

                   

                  app.findGrepPreferences = app.changeGrepPreferences =null; 
                  app.findGrepPreferences.findWhat = "(?<=\)).+?(?=\()" // apply your grep code 
                  var found = app.documents[0].findGrep(); 
                    for(i=0; i<found.length; i++) 
                  { 
                      if(found[i].contents) 
                      { 
                      found[i].select(); 
                          var new_Style = app.documents[0].characterStyles.add() 
                      new_Style.name = "my_style"; // your style name 
                      new_Style.fontStyle=found[i].fontStyle;
                      new_Style.fillColor=found[i].fillColor;
                      }   
                  } 
                  
                      app.findGrepPreferences = app.changeGrepPreferences =null; 
                  

                   

                   

                  HTH,

                  K

                  1 person found this helpful
                  • 6. Re: Find text then add a character style script
                    tpk1982 Level 4

                    Not sure why the above my post being moderated?

                    • 7. Re: Find text then add a character style script
                      Ananth@desgin Level 2

                      Hi,

                      I think you need to create multiple character styles using script for each selected text.. Am i correct???

                       

                      Thanks,

                      Prabu

                      1 person found this helpful
                      • 8. Re: Find text then add a character style script
                        thaero17058 Level 1

                        Hi

                        thank you for try . but :

                        when i run your script tell me :

                        "A style with this name already exist "

                        • 9. Re: Find text then add a character style script
                          thaero17058 Level 1

                          Yes , you correct 100%   ... this is what i need .

                          • 10. Re: Find text then add a character style script
                            tpk1982 Level 4

                            Delete the my_style character style if you have already and then rerun the script..

                            • 11. Re: Find text then add a character style script
                              thaero17058 Level 1

                              I do this , but the same result

                              • 12. Re: Find text then add a character style script
                                tpk1982 Level 4

                                try this

                                 

                                app.findGrepPreferences = app.changeGrepPreferences =null;   
                                app.findGrepPreferences.findWhat = "(?<=\)).+?(?=\()" // apply your grep code   
                                var found = app.documents[0].findGrep();   
                                try
                                {
                                var new_Style = app.documents[0].characterStyles.item("my_style");
                                new_Style.name;
                                }
                                catch (myError){
                                var new_Style = app.documents[0].characterStyles.add()   
                                    new_Style.name = "my_style";  }
                                
                                
                                for(i=0; i<found.length; i++)   
                                {   
                                    if(found[i].contents)   
                                    {   
                                    found[i].select();   
                                        
                                    new_Style.fontStyle=found[i].fontStyle;  
                                    new_Style.fillColor=found[i].fillColor;  
                                    }     
                                }   
                                
                                
                                    app.findGrepPreferences = app.changeGrepPreferences =null;
                                
                                • 13. Re: Find text then add a character style script
                                  tpk1982 Level 4

                                  Why the hell always keep the answers in moderated? Any one please answer?

                                  • 14. Re: Find text then add a character style script
                                    tpk1982 Level 4

                                    try this..

                                     

                                    app.findGrepPreferences = app.changeGrepPreferences =null;   
                                    app.findGrepPreferences.findWhat = "(?<=\)).+?(?=\()" // apply your grep code   
                                    var found = app.documents[0].findGrep();   
                                    try
                                    {
                                    var new_Style = app.documents[0].characterStyles.item("my_style");
                                    }
                                    catch (myError){
                                    var new_Style = app.documents[0].characterStyles.add()   
                                        new_Style.name = "my_style";  }
                                    
                                    
                                    for(i=0; i<found.length; i++)   
                                    {   
                                        if(found[i].contents)   
                                        {   
                                        found[i].select();   
                                            
                                        new_Style.fontStyle=found[i].fontStyle;  
                                        new_Style.fillColor=found[i].fillColor;  
                                        }     
                                    }   
                                    
                                    • 15. Re: Find text then add a character style script
                                      thaero17058 Level 1

                                      Hi

                                      i try it ... it's tell me :

                                      " Execution finished . Result : [ object Color ] "

                                      and it's  not work what i need 

                                      • 16. Re: Find text then add a character style script
                                        tpk1982 Level 4

                                        Is it your grep code is correct? Do you the texts within brackets like that? Also please share the screenshots. so we can know better

                                        • 17. Re: Find text then add a character style script
                                          thaero17058 Level 1

                                          Yes , the GREP code is correct .

                                          ghhvub.png

                                          • 18. Re: Find text then add a character style script
                                            tpk1982 Level 4

                                            Are you running the script from Indesign or Extendscript editor?

                                            • 19. Re: Find text then add a character style script
                                              tpk1982 Level 4

                                              use this as grep code..

                                               

                                              (?<=\\)).+?(?=\\()

                                              • 20. Re: Find text then add a character style script
                                                thaero17058 Level 1

                                                I try run the script from InDesign and from Extendscript editor .... but the same result >>> the script create just 1 character style does not based on any of the selected texts and not applied on any text .

                                                I am using a GREP code like what you say but with different glyph :    (?<=\﴿).+?(?=\﴾)     ... and it is correct .

                                                • 21. Re: Find text then add a character style script
                                                  tpk1982 Level 4

                                                  The bracket looks weird.. and in JavaScript using a single slash will get omitted.. so we need to use double slashes..

                                                  • 22. Re: Find text then add a character style script
                                                    tpk1982 Level 4

                                                    You done wrong with grep code.. try this.. this will work

                                                     

                                                    app.findGrepPreferences = app.changeGrepPreferences =null;     
                                                    app.findGrepPreferences.findWhat = "(?<=\\().+?(?=\\))" // apply your grep code     
                                                    var found = app.documents[0].findGrep();     
                                                    try  
                                                    {  
                                                    var new_Style = app.documents[0].characterStyles.item("my_style");  
                                                    new_Style.name;
                                                    }  
                                                    catch (myError){  
                                                    var new_Style = app.documents[0].characterStyles.add()     
                                                        new_Style.name = "my_style";  }  
                                                      
                                                      
                                                    for(i=0; i<found.length; i++)     
                                                    {     
                                                        if(found[i].contents)     
                                                        {     
                                                        found[i].select();     
                                                              
                                                        new_Style.fontStyle=found[i].fontStyle;    
                                                        new_Style.fillColor=found[i].fillColor;    
                                                        }       
                                                    }     
                                                    
                                                    1 person found this helpful
                                                    • 23. Re: Find text then add a character style script
                                                      thaero17058 Level 1

                                                      i try what you say ... but the same result .

                                                      I try the GREP code in find/change in InDesign and it is good and select what i need to select .

                                                      i think the problem in the script , but i do not know it .

                                                       

                                                      • 24. Re: Find text then add a character style script
                                                        tpk1982 Level 4

                                                        Untitled.jpg

                                                         

                                                        any one in the forum leaders let me know why always the reply getting moderated?

                                                        • 25. Re: Find text then add a character style script
                                                          tpk1982 Level 4

                                                          I hope you see the revised script i sent.. if that also not worked..just change you line

                                                           

                                                          app.findGrepPreferences.findWhat = "(?<=\\().+?(?=\\))"

                                                           

                                                          to

                                                           

                                                          app.findGrepPreferences.findWhat = "volupta" //this is your first word in screenshot

                                                           

                                                          and change the color and style of that text and run the script..

                                                          • 26. Re: Find text then add a character style script
                                                            thaero17058 Level 1

                                                            I am sorry ... but the same result

                                                            • 27. Re: Find text then add a character style script
                                                              thaero17058 Level 1

                                                              If you want this is a sample text ... you can try the script on it .... this is maybe help to fine the problem :

                                                              In precabo. Occaborem reptate pe plabor ﴿ ﭞﭟﭠ﴾ assitat ut eaquid magnis ea inciunt labo. Ut verciis doloriatquis restiatusa consequi desequam dolut voluptat fugia voluptatus abo. Rovid magnis dolorro excerna tecatatur, conseque ﴿ ﮦﮧﮨﮩ﴾ explignistia nem vel est, vitate di optatestium que pel eumquas volessim adicipit volorro vidictur sin num nonet eum eturehe nihillenim quiatiatium consequo blaccum nam ea vitatium ut alia nes veliquatur, sitio modisqu iatem. Et endit maio blab iur ﴿ ﭑﭒﭓ﴾ andaeri squundus que cuptas quiatqui id ea doluptius suntotatus il incia commolo eos doluptatusam venis nulparibusci omni omni dis arcipsaest atquam quisqua tiumquis enduciis nonecte porerore num dolupiciae niet alit omnisque natium qui reperciat audistis mintem aut endanis tibus.

                                                              • 28. Re: Find text then add a character style script
                                                                thaero17058 Level 1

                                                                If you want this is a sample text ... you can try the script on it .... this is maybe help to fine the problem :

                                                                In precabo. Occaborem reptate pe plabor ﴿ ﭞﭟﭠ﴾ assitat ut eaquid magnis ea inciunt labo. Ut verciis doloriatquis restiatusa consequi desequam dolut voluptat fugia voluptatus abo. Rovid magnis dolorro excerna tecatatur, conseque ﴿ ﮦﮧﮨﮩ﴾ explignistia nem vel est, vitate di optatestium que pel eumquas volessim adicipit volorro vidictur sin num nonet eum eturehe nihillenim quiatiatium consequo blaccum nam ea vitatium ut alia nes veliquatur, sitio modisqu iatem. Et endit maio blab iur ﴿ ﭑﭒﭓ﴾ andaeri squundus que cuptas quiatqui id ea doluptius suntotatus il incia commolo eos doluptatusam venis nulparibusci omni omni dis arcipsaest atquam quisqua tiumquis enduciis nonecte porerore num dolupiciae niet alit omnisque natium qui reperciat audistis mintem aut endanis tibus.

                                                                • 29. Re: Find text then add a character style script
                                                                  tpk1982 Level 4

                                                                  Yes i worked with your sample texts but I missed with fonts anyhow i put bracket for other word and it is working fine..

                                                                   

                                                                  app.findGrepPreferences = app.changeGrepPreferences =null;       
                                                                  app.findGrepPreferences.findWhat = "(?<=\\)).+?(?=\\()" // apply your grep code       
                                                                  var found = app.documents[0].findGrep();       
                                                                  try    
                                                                  {    
                                                                  var new_Style = app.documents[0].characterStyles.item("my_style");    
                                                                  new_Style.name;  
                                                                  }    
                                                                  catch (myError){    
                                                                  var new_Style = app.documents[0].characterStyles.add()       
                                                                      new_Style.name = "my_style";  }    
                                                                      
                                                                      
                                                                  for(i=0; i<found.length; i++)       
                                                                  {       
                                                                      if(found[i].contents)       
                                                                      {       
                                                                      found[i].select();       
                                                                              
                                                                      new_Style.fontStyle=found[i].fontStyle;      
                                                                      new_Style.fillColor=found[i].fillColor;      
                                                                      }         
                                                                  }       
                                                                  

                                                                   

                                                                   

                                                                  Screen Shot 2017-03-24 at 1.33.42 PM.png

                                                                   

                                                                  Screen Shot 2017-03-24 at 1.36.44 PM.png

                                                                   

                                                                  If you feel again it is not working, then just record your work as video and send to me.. or post here..

                                                                   

                                                                  In my end it is perfectly working for all languages..

                                                                  1 person found this helpful
                                                                  • 30. Re: Find text then add a character style script
                                                                    tpk1982 Level 4

                                                                    Screen Shot 2017-03-24 at 1.37.59 PM.png

                                                                    1 person found this helpful
                                                                    • 31. Re: Find text then add a character style script
                                                                      thaero17058 Level 1

                                                                      Hi

                                                                      i will send a video to you

                                                                      • 32. Re: Find text then add a character style script
                                                                        tpk1982 Level 4

                                                                        Hi.. Saw your file.. and the below script working fine.. eben thouth the font is missing with my side the style created successfully.. please let me know if it is working for you..

                                                                         

                                                                        app.findGrepPreferences = app.changeGrepPreferences =null;         
                                                                        app.findGrepPreferences.findWhat = "(?<=\﴿).+?(?=\﴾)" // apply your grep code         
                                                                        var found = app.documents[0].findGrep();         
                                                                        try      
                                                                        {      
                                                                        var new_Style = app.documents[0].characterStyles.item("my_style");      
                                                                        new_Style.name;    
                                                                        }      
                                                                        catch (myError){      
                                                                        var new_Style = app.documents[0].characterStyles.add()         
                                                                            new_Style.name = "my_style";  }      
                                                                              
                                                                              
                                                                        for(i=0; i<found.length; i++)         
                                                                        {         
                                                                            if(found[i].contents)         
                                                                            {         
                                                                            found[i].select();         
                                                                                      
                                                                            new_Style.fontStyle=found[i].fontStyle;        
                                                                            new_Style.fillColor=found[i].fillColor;        
                                                                            }           
                                                                        }         
                                                                        
                                                                        1 person found this helpful
                                                                        • 33. Re: Find text then add a character style script
                                                                          tpk1982 Level 4

                                                                          I hope all the bracket texts have same color and font style..

                                                                          1 person found this helpful
                                                                          • 34. Re: Find text then add a character style script
                                                                            tpk1982 Level 4

                                                                            use this for better result..

                                                                             

                                                                            app.findGrepPreferences = app.changeGrepPreferences =null;         
                                                                            app.findGrepPreferences.findWhat = "(?<=\﴿).+?(?=\﴾)" // apply your grep code         
                                                                            var found = app.documents[0].findGrep();     
                                                                            
                                                                            
                                                                            if(found.length>=1){
                                                                            try      
                                                                            {      
                                                                            var new_Style = app.documents[0].characterStyles.item("my_style");      
                                                                            new_Style.name;    
                                                                            }      
                                                                            catch (myError){      
                                                                            var new_Style = app.documents[0].characterStyles.add()         
                                                                                new_Style.name = "my_style";  }      
                                                                                  
                                                                                  
                                                                            for(i=0; i<found.length; i++)         
                                                                            {         
                                                                                if(found[i].contents)         
                                                                                {         
                                                                                found[i].select();         
                                                                                          
                                                                                new_Style.fontStyle=found[i].fontStyle;        
                                                                                new_Style.fillColor=found[i].fillColor;        
                                                                                }           
                                                                            }         
                                                                            }
                                                                            else{alert("No bracket text found")}
                                                                            
                                                                            1 person found this helpful
                                                                            • 35. Re: Find text then add a character style script
                                                                              thaero17058 Level 1

                                                                              Yes , all bracket text have same color and font style ... but different font type .

                                                                              this script also do not work ... i am sorry

                                                                               

                                                                              • 36. Re: Find text then add a character style script
                                                                                tpk1982 Level 4

                                                                                Hmm.. try this..

                                                                                 

                                                                                app.findGrepPreferences = app.changeGrepPreferences =null;         
                                                                                app.findGrepPreferences.findWhat = "(?<=\﴿).+?(?=\﴾)"        
                                                                                var found = app.documents[0].findGrep();     
                                                                                if(found.length==0){alert("No bracket text found"); exit(0);}
                                                                                cstyle = [];    
                                                                                for(j=0; j<found.length; j++)         
                                                                                {   
                                                                                  cstyle.push(found[j].contents);
                                                                                }
                                                                                for(k=0; k<cstyle.length; k++)         
                                                                                {   
                                                                                  
                                                                                app.findGrepPreferences = app.changeGrepPreferences =null;         
                                                                                app.findGrepPreferences.findWhat = cstyle[k].toString ();          
                                                                                var myfound = app.documents[0].findGrep();     
                                                                                for(i=0; i<myfound.length; i++)         
                                                                                {         
                                                                                    myfound[i].select();         
                                                                                    var new_Style = app.documents[0].characterStyles.add();
                                                                                    new_Style.name = cstyle[k].toString ();
                                                                                    new_Style.fontStyle=myfound[i].fontStyle;        
                                                                                    new_Style.fillColor=myfound[i].fillColor;        
                                                                                              
                                                                                }         
                                                                                }
                                                                                
                                                                                1 person found this helpful
                                                                                • 37. Re: Find text then add a character style script
                                                                                  tpk1982 Level 4

                                                                                  PLEASE FOLLOW THIS STEP...

                                                                                   

                                                                                   

                                                                                  1) CHANGE COLOR OF ALL BRACKET TEXTS

                                                                                   

                                                                                  2) CHANGE THE FONT STYLE DIFFERENTLY (MAYBE BOLD, ITAL, BOLDITALIC)

                                                                                   

                                                                                  3) RUN THE BELOW SCRIPT..

                                                                                   

                                                                                   

                                                                                   

                                                                                  app.findGrepPreferences = app.changeGrepPreferences =null;        

                                                                                  app.findGrepPreferences.findWhat = "(?<=\﴿).+?(?=\﴾)"       

                                                                                  var found = app.documents[0].findGrep();    

                                                                                  if(found.length==0){alert("No bracket text found"); exit(0);}

                                                                                  cstyle = [];   

                                                                                  for(j=0; j<found.length; j++)        

                                                                                  {  

                                                                                    cstyle.push(found[j].contents);

                                                                                  }

                                                                                  for(k=0; k<cstyle.length; k++)        

                                                                                  {  

                                                                                   

                                                                                  app.findGrepPreferences = app.changeGrepPreferences =null;        

                                                                                  app.findGrepPreferences.findWhat = cstyle[k].toString();         

                                                                                  var myfound = app.documents[0].findGrep();    

                                                                                  for(i=0; i<myfound.length; i++)        

                                                                                  {        

                                                                                      myfound[i].select();        

                                                                                      var new_Style = app.documents[0].characterStyles.add();

                                                                                      var myname=new_Style.name;

                                                                                      new_Style.fontStyle=myfound[i].fontStyle;       

                                                                                      new_Style.fillColor=myfound[i].fillColor;       

                                                                                      app.changeGrepPreferences.appliedCharacterStyle = myname;       

                                                                                      app.changeGrep();

                                                                                  }        

                                                                                  }

                                                                                  1 person found this helpful
                                                                                  • 38. Re: Find text then add a character style script
                                                                                    tpk1982 Level 4

                                                                                    SELECT TEXT WITH IN BRACKET.. YOU CAN SEE THE STYLES APPLIED..

                                                                                    • 39. Re: Find text then add a character style script
                                                                                      tpk1982 Level 4

                                                                                      Moreover the below script completes with all character style properties..

                                                                                       

                                                                                      app.findGrepPreferences = app.changeGrepPreferences =null;         
                                                                                      app.findGrepPreferences.findWhat = "(?<=\﴿).+?(?=\﴾)"        
                                                                                      var found = app.documents[0].findGrep();     
                                                                                      if(found.length==0){alert("No bracket text found"); exit(0);}
                                                                                      cstyle = [];    
                                                                                      for(j=0; j<found.length; j++)         
                                                                                      {   
                                                                                        cstyle.push(found[j].contents);
                                                                                      }
                                                                                      for(k=0; k<cstyle.length; k++)         
                                                                                      {   
                                                                                        
                                                                                      app.findGrepPreferences = app.changeGrepPreferences =null;         
                                                                                      app.findGrepPreferences.findWhat = cstyle[k].toString();          
                                                                                      var myfound = app.documents[0].findGrep();     
                                                                                      for(i=0; i<myfound.length; i++)         
                                                                                      {         
                                                                                          myfound[i].select();         
                                                                                          var new_Style = app.documents[0].characterStyles.add();
                                                                                          var myname=new_Style.name;
                                                                                          new_Style.appliedFont = myfound[i].appliedFont;
                                                                                        new_Style.fontStyle = myfound[i].fontStyle;
                                                                                        new_Style.pointSize = myfound[i].pointSize;
                                                                                        new_Style.leading = myfound[i].leading;
                                                                                        new_Style.appliedLanguage = myfound[i].appliedLanguage;
                                                                                        new_Style.kerningMethod = myfound[i].kerningMethod;
                                                                                        new_Style.tracking = myfound[i].tracking;
                                                                                        new_Style.capitalization = myfound[i].capitalization;
                                                                                        new_Style.position = myfound[i].position;
                                                                                        new_Style.ligatures = myfound[i].ligatures;
                                                                                        new_Style.noBreak = myfound[i].noBreak;
                                                                                        new_Style.horizontalScale = myfound[i].horizontalScale;
                                                                                        new_Style.verticalScale = myfound[i].verticalScale;
                                                                                        new_Style.baselineShift = myfound[i].baselineShift;
                                                                                        new_Style.skew = myfound[i].skew;
                                                                                        new_Style.fillColor = myfound[i].fillColor;
                                                                                        new_Style.fillTint = myfound[i].fillTint;
                                                                                        new_Style.strokeTint = myfound[i].strokeTint;
                                                                                        new_Style.strokeWeight = myfound[i].strokeWeight;
                                                                                        new_Style.overprintStroke = myfound[i].overprintStroke;
                                                                                        new_Style.overprintFill = myfound[i].overprintFill;
                                                                                        new_Style.otfFigureStyle = myfound[i].otfFigureStyle;
                                                                                        new_Style.otfOrdinal = myfound[i].otfOrdinal;
                                                                                        new_Style.otfFraction = myfound[i].otfFraction;
                                                                                        new_Style.otfDiscretionaryLigature = myfound[i].otfDiscretionaryLigature;
                                                                                        new_Style.otfTitling = myfound[i].otfTitling;
                                                                                        new_Style.otfContextualAlternate = myfound[i].otfContextualAlternate;
                                                                                        new_Style.otfSwash = myfound[i].otfSwash;
                                                                                        new_Style.otfSlashedZero = myfound[i].otfSlashedZero;
                                                                                        new_Style.otfHistorical = myfound[i].otfHistorical;
                                                                                        new_Style.otfStylisticSets = myfound[i].otfStylisticSets;
                                                                                        new_Style.strikeThru = myfound[i].strikeThru;
                                                                                          
                                                                                          
                                                                                          app.changeGrepPreferences.appliedCharacterStyle = myname;        
                                                                                          app.changeGrep();
                                                                                      }         
                                                                                      }
                                                                                      
                                                                                      2 people found this helpful
                                                                                      1 2 Previous Next