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,
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
...Copy link to clipboard
Copied
perhaps soem nore information is needed.
Have you tried Find Font? ("Type" menu-> "Find font")?
Copy link to clipboard
Copied
Thanks for your replay Vamitul!
Yes I tried ("Type" menu-> "Find font") but it only works on when the paragraph styles applied to any texts in the document but i need to change the font of the paragarphstyles when it is not applied to any texts
Say for example a and b are paragraph styles applied "Times" font these styles applied font should be changed to some other font.
Copy link to clipboard
Copied
Hope this will help you..
var myDoc=app.activeDocument;
var allStyles = new Array('parastyle1', 'parastyle2', 'parastyle3');//style name here
for (a=0; a<allStyles.length; a++)
{
var testStyle = myDoc.paragraphStyles.itemByName(allStyles);
testStyle.appliedFont = "Minion";//font name here
}
Vandy
Copy link to clipboard
Copied
Thanks for your reply Vandy,
Here is my code. Minion Pro font need to be changed as Avenir LT Std in all the paragraph styles.
var myDoc=app.activeDocument;
var pstyles = myDoc.paragraphStyles.everyItem().name;
var allStyles = new Array(pstyles);//style name here
var minion = myDoc.fonts.item("Minion Pro");
var Avenir = myDoc.fonts.item("Avenir LT Std");
for (a=0; a<allStyles.length; a++)
{
if (allStyles==minion){
var testStyle = allStyles;
testStyle.appliedFont = Avenir;//font name here
}
}
Copy link to clipboard
Copied
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 the first one is "no paragraph style" and it's read only. trying to change it will cause a error.
using a regex to match the name of the font because appliedFont.name returns a string like this: font_name {tab character} font_style eg: "Minion Pro\tSemibold Subhead"
hope it helps.
Copy link to clipboard
Copied
Thank you so much vamitul!
it works fine for me.
Copy link to clipboard
Copied
Hi Vamitul,
The same is not working with Character styles. Definitely I made a mistake but i unable to find it. Please help...
var dlg = app.dialogs.add({name:"Changing Fonts Globally in all Styles"});
var dlc1 = dlg.dialogColumns.add();
var dlr =dlc1.borderPanels.add();
dlr.staticTexts.add({staticLabel:"From font"});
var fromfont = dlr.textEditboxes.add({minWidth:100});
dlr.staticTexts.add({staticLabel:"To font"});
var tofont = dlr.textEditboxes.add({minWidth:100});
dlg.show();
var myDoc=app.activeDocument;
var pstyles = myDoc.allParagraphStyles;
var cstyles = myDoc.allCharacterStyles;
var ffont = fromfont.editContents;
var tfont = tofont.editContents;
for (var a=1; a<pstyles.length; a++)
{
if (pstyles.appliedFont.name.match(ffont)){
pstyles.appliedFont=tfont;
}
};
for (var i=1; i<cstyles.length; i++)
{
if (cstyles.appliedStyle.name.match(ffont)){
cstyles.appliedStyle=tfont;
}
};
Thanks in advance,
Copy link to clipboard
Copied
Hi Chinna,
Try this
if (cstyles.fontStyle.match(ffont)){
cstyles.fontStyle=tfont;
}
Copy link to clipboard
Copied
try this (sorry about the ugly interface):
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()) {
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.name==ffont) {
cstyles.appliedFont = tfont;
}
};
}
}app.doScript("main()",undefined,undefined,UndoModes.fastEntireScript,'Global Change Font');
Copy link to clipboard
Copied
Thanks Vamitul,
I will try this.
Copy link to clipboard
Copied
Hi Vamitul,
Thanks for your script it is only working fine on the paragraph styles but it is not working on the character styles.
Copy link to clipboard
Copied
strange, it works on my test documents. can you send me a sample document to see why/what doesn't work?
Copy link to clipboard
Copied
Hi Vamitul,
How do i send document i dont find any option here
Note: im using CS5.5
Copy link to clipboard
Copied
Hi Chinna
Ignore this if it is wrong.
"cstyles.appliedFont.name==ffont" will return the font family("Minion Pro") and font style("Regular").
If font family of your character style is not specifid (only font style is specified in the style) then above condition will fail.
If my suggestion is wrong, I apologize for that.
Copy link to clipboard
Copied
Hi Suresh (hope i don't say something wrong/wierd if i short your name like that).
if the font family is not defined in the charater style, cstyles.appliedFont.name will return "undefined". and since undefined does not equal the font name, the script will just ignore the char style and move on.
Chinna, if you click on my profile pic you shoud get my e-mail address (vamitulATgmailDOTcom----)
Copy link to clipboard
Copied
Hi Vamitul
Thanks for your reply.
Regards
Suresh
Copy link to clipboard
Copied
Hi Vamitul,
I sent.
Copy link to clipboard
Copied
well.. i found the problem. a wierd one even. apparently characterStyle.appliedFont does not return a font, but a string. will try to fix it and get back to you later today, i hope.
Copy link to clipboard
Copied
Thanks Vamitul.
Copy link to clipboard
Copied
ok.. this should work for most cases:
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()) {
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');
When i'll have the time i'll probably do a more advanced version, to change font families or just font variants etc.
However, for your example, it won't change the character style "three". I'm curious about how did you defined that style because while in the interface it shows that the applied font is "Minion Pro" and the font style is "Regular", when i get the font Style property from the script it reports as "Roman". Very very wierd.
Copy link to clipboard
Copied
Thanks Vamitul...
Copy link to clipboard
Copied
Hi Vamitul,
I found the solution. Here is my script.
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 docfonts=doc.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r");
var appfonts=app.fonts.everyItem().name.join("\r").replace(/\t/g,":").split("\r");
var arrayofdocfont=panel1.add("dropdownlist",undefined,docfonts);
var arrayofappfont=panel2.add("dropdownlist",undefined,appfonts);
myDialog.add("button",undefined,"Ok",{name:"ok"});
var cancel = myDialog.add("button",undefined,"Cancel",{name:"cancel"});
cancel.onClick = function (){
myDialog.close ();
}
arrayofdocfont.selection=arrayofdocfont.items[0];
arrayofappfont.selection=arrayofappfont.items[0];
if(myDialog.show()) {
var pstyles = doc.allParagraphStyles;
var cstyles = doc.allCharacterStyles;
var pstylefont = arrayofdocfont.selection.text.replace(/:/g,"\t").toString();
var cstylefont = arrayofdocfont.selection.text.replace(/:.*/g,"");
var tfont = arrayofappfont.selection.text.replace(/:/g,"\t");
for (var a = 1; a < pstyles.length; a++) {
if (pstyles.appliedFont.name==pstylefont) {
pstyles.appliedFont = tfont;
}
};
for (var i = 1; i < cstyles.length; i++) {
if (cstyles.appliedFont ==cstylefont) {
cstyles.appliedFont =tfont;
}
}
}
Regards,
Chinna
Copy link to clipboard
Copied
Hello Vamitul.
i hope you are doing well
kindly please can you edit your code to be applied on all opened documents
Thanks a lot
Copy link to clipboard
Copied
Hi Susan.
No!