• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

Specified font need to be changed in all paragraph styles

Enthusiast ,
Apr 23, 2013 Apr 23, 2013

Copy link to clipboard

Copied

Hi All,

Is there any option to change a font which is used in many paragarph styles to some other font.

Thanks in advance,

TOPICS
Scripting

Views

5.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Apr 23, 2013 Apr 23, 2013

Try this:

var myDoc=app.activeDocument;

var pstyles = myDoc.allParagraphStyles;

for (var a=1; a<allStyles.length; a++)

{

    if (pstyles.appliedFont.name.match(/minion/i){

         pstyles.appliedFont="Avenir LT Std";

       }

}

explanations:

allParagraphsStyles gets an array of every paragraph style in the doc, regardless of the group it might be in. myDoc.paragraphStyles.everyItem() gets a collection of all the paragraphs styles outside groups.

running the array of paragraph styles for 1, not 0, because

...

Votes

Translate

Translate
Participant ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Thanks anyway !!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Hi!

Why?

(^/)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Why? Why not?

Because I don't want to should suffice, but for a longer explanation:
Because I'm not here to provide ready-made scripts, to order.
Because it's a trivial change to be made by anyone who has the least bit of interest in scripting and it is here on this forum to learn more, not just ask for freebies.

Because I don't have the time, the energy or the passion to help anyone who is not interested.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

well then i think you can just say that you don't have time and i won't be shocked from your response like previous one,

by the way i'm so interested and i did many tries on your code to be just like i requested (politely)  but i failed, i'm already add more features to it like multiple find/change processes but about that point i didn't get lucky ..

thanks anyway as i said

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Suzan,

Applying to all open document is not a big task.. take the documents length and use for loop to acheive...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

tpk1982

i think that is my bad luck

how come i have gone further with this code and i can't do that bloody small thing šŸ˜• šŸ˜•

thanks for your hint, but still failed.

i succeeded to add multiple find / replace processes and panels but failed with this LOL

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Which code you need to apply to all documents? Is it the below one?

var myDoc=app.activeDocument; 

var pstyles = myDoc.allParagraphStyles; 

for (var a=1; a<allStyles.length; a++) 

    if (pstyles.appliedFont.name.match(/minion/i){ 

         pstyles.appliedFont="Avenir LT Std"; 

       } 

or some other by Chinna's one?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

actually that code after i did editing to be a multiple find/replace

function main(){ 

          var doc=app.activeDocument; 

         

          var myDialog= new Window("dialog","Font Find/Change"); 

          var grp1=myDialog.add("group"); 

          var panel1=grp1.add("panel",undefined,"Find Font:"); 

          var panel2=grp1.add("panel",undefined,"Change Font:");

         

         

          var findFonts1=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts1=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var myFindDrop1=panel1.add("dropdownlist",undefined,findFonts1); 

          var myChDrop1=panel2.add("dropdownlist",undefined,chFonts1); 

         

         

          var grp2=myDialog.add("group"); 

          var panel3=grp2.add("panel",undefined,"Find Font:"); 

          var panel4=grp2.add("panel",undefined,"Change Font:");         

         

          var findFonts2=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts2=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r");                  

          var myFindDrop2=panel3.add("dropdownlist",undefined,findFonts2); 

          var myChDrop2=panel4.add("dropdownlist",undefined,chFonts2);          

         

           var grp3=myDialog.add("group"); 

          var panel5=grp3.add("panel",undefined,"Find Font:"); 

          var panel6=grp3.add("panel",undefined,"Change Font:");         

         

          var findFonts3=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts3=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r");                  

          var myFindDrop3=panel5.add("dropdownlist",undefined,findFonts3); 

          var myChDrop3=panel6.add("dropdownlist",undefined,chFonts3);             

         

           var grp4=myDialog.add("group"); 

          var panel7=grp4.add("panel",undefined,"Find Font:"); 

          var panel8=grp4.add("panel",undefined,"Change Font:");         

         

          var findFonts4=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts4=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r");                  

          var myFindDrop4=panel7.add("dropdownlist",undefined,findFonts4); 

          var myChDrop4=panel8.add("dropdownlist",undefined,chFonts4);     

         

            var grp5=myDialog.add("group"); 

          var panel9=grp5.add("panel",undefined,"Find Font:"); 

          var panel10=grp5.add("panel",undefined,"Change Font:");         

         

          var findFonts5=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts5=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r");                  

          var myFindDrop5=panel9.add("dropdownlist",undefined,findFonts5); 

          var myChDrop5=panel10.add("dropdownlist",undefined,chFonts5);             

         

           var grp6=myDialog.add("group"); 

          var panel11=grp6.add("panel",undefined,"Find Font:"); 

          var panel12=grp6.add("panel",undefined,"Change Font:");         

         

          var findFonts6=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts6=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r");                  

          var myFindDrop6=panel11.add("dropdownlist",undefined,findFonts6); 

          var myChDrop6=panel12.add("dropdownlist",undefined,chFonts6);               

         

         

         

         

         

         

          myDialog.add("button",undefined,"Ok",{name:"ok"}); 

          myDialog.add("button",undefined,"Cancel",{name:"cancel"}); 

         

         

         

         

          myFindDrop1.selection=myFindDrop1.items[0]; 

          myChDrop1.selection=myChDrop1.items[0]; 

          myFindDrop2.selection=myFindDrop2.items[0]; 

          myChDrop2.selection=myChDrop2.items[0]; 

           myFindDrop3.selection=myFindDrop3.items[0]; 

          myChDrop3.selection=myChDrop3.items[0];

           myFindDrop4.selection=myFindDrop4.items[0]; 

          myChDrop4.selection=myChDrop4.items[0];

           myFindDrop5.selection=myFindDrop5.items[0]; 

          myChDrop5.selection=myChDrop5.items[0];          

           myFindDrop6.selection=myFindDrop6.items[0]; 

          myChDrop6.selection=myChDrop6.items[0];          

         

         

         

         

          if (myDialog.show()) { 

                    var pstyles = doc.allParagraphStyles; 

                    var cstyles = doc.allCharacterStyles; 

                    var ffont = myFindDrop2.selection.text.replace(/:/g,"\t"); 

                    var tfont = myChDrop2.selection.text.replace(/:/g,"\t"); 

                    for (var a = 1; a < pstyles.length; a++) { 

                              if (pstyles.appliedFont.name==ffont) { 

                                        pstyles.appliedFont = tfont; 

                                       

                    var pstyles = doc.allParagraphStyles; 

                    var cstyles = doc.allCharacterStyles; 

                    var ffont = myFindDrop1.selection.text.replace(/:/g,"\t"); 

                    var tfont = myChDrop1.selection.text.replace(/:/g,"\t"); 

                    for (var a = 1; a < pstyles.length; a++) { 

                              if (pstyles.appliedFont.name==ffont) { 

                                        pstyles.appliedFont = tfont; 

                                       

                     var pstyles = doc.allParagraphStyles; 

                    var cstyles = doc.allCharacterStyles; 

                    var ffont = myFindDrop3.selection.text.replace(/:/g,"\t"); 

                    var tfont = myChDrop3.selection.text.replace(/:/g,"\t"); 

                    for (var a = 1; a < pstyles.length; a++) { 

                              if (pstyles.appliedFont.name==ffont) { 

                                        pstyles.appliedFont = tfont; 

                                  

                      var pstyles = doc.allParagraphStyles; 

                    var cstyles = doc.allCharacterStyles; 

                    var ffont = myFindDrop4.selection.text.replace(/:/g,"\t"); 

                    var tfont = myChDrop4.selection.text.replace(/:/g,"\t"); 

                    for (var a = 1; a < pstyles.length; a++) { 

                              if (pstyles.appliedFont.name==ffont) { 

                                        pstyles.appliedFont = tfont;                                   

                                       

                       var pstyles = doc.allParagraphStyles; 

                    var cstyles = doc.allCharacterStyles; 

                    var ffont = myFindDrop5.selection.text.replace(/:/g,"\t"); 

                    var tfont = myChDrop5.selection.text.replace(/:/g,"\t"); 

                    for (var a = 1; a < pstyles.length; a++) { 

                              if (pstyles.appliedFont.name==ffont) { 

                                        pstyles.appliedFont = tfont;                                       

                                       

                      var pstyles = doc.allParagraphStyles; 

                    var cstyles = doc.allCharacterStyles; 

                    var ffont = myFindDrop6.selection.text.replace(/:/g,"\t"); 

                    var tfont = myChDrop6.selection.text.replace(/:/g,"\t"); 

                    for (var a = 1; a < pstyles.length; a++) { 

                              if (pstyles.appliedFont.name==ffont) { 

                                        pstyles.appliedFont = tfont;

                                       

       }                                

  }                                     

}                                       

                                        }

                                   }

                              } 

                          }

                    }; 

                    for (var i = 1; i < cstyles.length; i++) { 

                              if ((cstyles.appliedFont+'\t'+cstyles.fontStyle==ffont)||(cstyles.appliedFont.name==ffont)) { 

                                        cstyles.appliedFont = tfont; 

                              } 

                    }; 

          } 

 

  }

  }

}

      }

 

}app.doScript("main()",undefined,undefined,UndoModes.fastEntireScript,'Global Change Font'); 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

ok try this..

simple find and replace for all opened documents.. this will give you the hint

for(i=0; i< app.documents.length; i++){

app.findGrepPreferences = null;     app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat="a";

app.changeGrepPreferences.changeTo  = "b";

app.documents.changeGrep(); 

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Based on Vlad's clever script, this version loops through all open docs and demands what to do after having extracted the list of fonts used in the current doc:

function main(){ 

  var docs = app.documents;

  for (var d = docs.length-1; d >= 0; d--) {

          var doc=docs

          var myDialog= new Window("dialog","Font replacer"); 

          var grp1=myDialog.add("group"); 

          var panel1=grp1.add("panel",undefined,"Find font:"); 

          var panel2=grp1.add("panel",undefined,"Change font:"); 

          var findFonts=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var myFindDrop=panel1.add("dropdownlist",undefined,findFonts); 

          var myChDrop=panel2.add("dropdownlist",undefined,chFonts); 

          myDialog.add("button",undefined,"Ok",{name:"ok"}); 

          myDialog.add("button",undefined,"Cancel",{name:"cancel"}); 

          myFindDrop.selection=myFindDrop.items[0]; 

          myChDrop.selection=myChDrop.items[0];

          if (myDialog.show()) { 

                    var pstyles = doc.allParagraphStyles; 

                    var cstyles = doc.allCharacterStyles; 

                    var ffont = myFindDrop.selection.text.replace(/:/g,"\t"); 

                    var tfont = myChDrop.selection.text.replace(/:/g,"\t"); 

                    for (var a = 1; a < pstyles.length; a++) { 

                              if (pstyles.appliedFont.name==ffont) { 

                                        pstyles.appliedFont = tfont; 

                              } 

                    }; 

                    for (var i = 1; i < cstyles.length; i++) { 

                              if ((cstyles.appliedFont+'\t'+cstyles.fontStyle==ffont)||(cstyles.appliedFont.name==ffont)) { 

                                        cstyles.appliedFont = tfont; 

                              } 

                    }; 

          }

      }

}app.doScript("main()",undefined,undefined,UndoModes.fastEntireScript,'Global Change Font'); 

(^/)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Dear Obi-wan, thanks a lot for your generous help but your code asks what do to for each document, i was seeking to apply first modification to all other files without asking again and again for each file

sorry guys for bothering you all, i'm intending to get it with your help or with my efforts as possible i can

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

I don't think it's a good way to do like you want, especially if the chosen font(s) don't exist in a doc! 

In this case, we need more scripting: play the script until it doesn't find the font(s)! ā€¦ and, in this situation, what does the script do?

(^/)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

I don't think it's a good way to do like you want, especially if the chosen font(s) don't exist in a doc!

i think that is not a problem because i'm working on a large project with many files contain same fonts on each file

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Vlad,

Maybe your answer was a little bit angry but I totally agree with you!

(^/) 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 21, 2016 Sep 21, 2016

Copy link to clipboard

Copied

LATEST

nice interface, you can just add multiple find change option.

I think this not working. I tried but did not change anything.

-Sumit

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Suzan.Vā€‹ā€‹,

Obi-Wan has given you all the information you need to modify the script the way you want it.. simply moving lines 4 and 5 will change the starting point of the for() loop If that's what you really want to do.. using that you can choose what you want to repeat for each document.. just make sure you move line 34 to the appropriate ending point of the loop.

Vamitulā€‹ and Obi-wan Kenobiā€‹,

clicking the cancel button in your scripts still executes the script.

if (myDialog.show()) {

Should be changed to:

if(myDialog.show() != 1){

    exit()

}else{

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Hi Skemicle!

You're right! 

I've just modified my previous version:

function main(){ 

          var doc = app.activeDocument; 

          var myDialog= new Window("dialog","Font replacer"); 

          var grp1=myDialog.add("group"); 

          var panel1=grp1.add("panel",undefined,"Find font:"); 

          var panel2=grp1.add("panel",undefined,"Change font:"); 

          var findFonts=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var myFindDrop=panel1.add("dropdownlist",undefined,findFonts); 

          var myChDrop=panel2.add("dropdownlist",undefined,chFonts); 

          myDialog.add("button",undefined,"Ok",{name:"ok"}); 

          myDialog.add("button",undefined,"Cancel",{name:"cancel"}); 

          myFindDrop.selection=myFindDrop.items[0]; 

          myChDrop.selection=myChDrop.items[0];

         

          if(myDialog.show() != 1){ 

              exit() 

            }else{

               var docs = app.documents;

               for (var d = docs.length-1; d >= 0; d--) {

                    var doc=docs

                    var pstyles = doc.allParagraphStyles; 

                    var cstyles = doc.allCharacterStyles; 

                    var ffont = myFindDrop.selection.text.replace(/:/g,"\t"); 

                    var tfont = myChDrop.selection.text.replace(/:/g,"\t"); 

                    for (var a = 1; a < pstyles.length; a++) { 

                              if (pstyles.appliedFont.name==ffont) { 

                                        pstyles.appliedFont = tfont; 

                              } 

                    }; 

                    for (var i = 1; i < cstyles.length; i++) { 

                              if ((cstyles.appliedFont+'\t'+cstyles.fontStyle==ffont)||(cstyles.appliedFont.name==ffont)) { 

                                        cstyles.appliedFont = tfont; 

                              } 

                    }; 

               }

           }

}app.doScript("main()",undefined,undefined,UndoModes.fastEntireScript,'Global Change Font');

(^/) 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Skemicle,

Good catch, but then:

  • You don't need to use an else branch:

     if (myDialog.show() !==1){

          exit()

       }

     //rest of the code, since it will only be executed if the user clicked OK

  • Don't use exit(). Use return.    

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Hi Vlad,

So, for you, this is correct:

function main(){ 

          var doc = app.activeDocument; 

          var myDialog= new Window("dialog","Font replacer"); 

          var grp1=myDialog.add("group"); 

          var panel1=grp1.add("panel",undefined,"Find font:"); 

          var panel2=grp1.add("panel",undefined,"Change font:"); 

          var findFonts=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var chFonts=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r"); 

          var myFindDrop=panel1.add("dropdownlist",undefined,findFonts); 

          var myChDrop=panel2.add("dropdownlist",undefined,chFonts); 

          myDialog.add("button",undefined,"Ok",{name:"ok"}); 

          myDialog.add("button",undefined,"Cancel",{name:"cancel"}); 

          myFindDrop.selection=myFindDrop.items[0]; 

          myChDrop.selection=myChDrop.items[0];

         

          if(myDialog.show() !== 1){ 

              return 

            }

          var docs = app.documents;

              

          for (var d = docs.length-1; d >= 0; d--) {

                  var doc=docs

                  var pstyles = doc.allParagraphStyles; 

                  var cstyles = doc.allCharacterStyles; 

                  var ffont = myFindDrop.selection.text.replace(/:/g,"\t"); 

                  var tfont = myChDrop.selection.text.replace(/:/g,"\t"); 

                  for (var a = 1; a < pstyles.length; a++) { 

                          if (pstyles.appliedFont.name==ffont) { 

                               pstyles.appliedFont = tfont; 

                             } 

                       }; 

                  for (var i = 1; i < cstyles.length; i++) { 

                          if ((cstyles.appliedFont+'\t'+cstyles.fontStyle==ffont)||(cstyles.appliedFont.name==ffont)) { 

                                 cstyles.appliedFont = tfont; 

                             } 

                        }; 

                }

}app.doScript("main()",undefined,undefined,UndoModes.fastEntireScript,'Global Change Font');

I totally understand about the "if ā€¦"!

What is the real difference between "!=" and "!=="?

Why use "return" instead of exist()?

Thanks! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

Use "return" instead of exist() because exist isn't a function . Kidding, but I would like to know the answers to those two questions as well.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 21, 2016 Sep 21, 2016

Copy link to clipboard

Copied

Dear Obi, thanks a lot for your generous help your modifying is great

sorry guys for bothering you all i'm just getting my first steps on scripting

Dear Vlad, thanks for your original code it helped me a lot and next time when you find my request is annoying you can just give me a hint for answer

  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines