Copy link to clipboard
Copied
I tried to adjust the script below in cc 2017. But there are a lot of errors encoutered. The root cause I guess is the the script below was developed in 2010 which was using on a very old version of illustrator. Now, a bunch of the API, function in class don't exist any more.
//get unique Array elements
Array.prototype.unique =
function
(){
var
r =
new
Array();
o:
for
(
var
i = 0, n =
this
.length; i < n; i++){
for
(
var
x = 0, y = r.length; x < y; x++){
if
(r
this
)
continue
o;}
r[r.length] =
this
;}
return
r;
}
//search inside array
Array.prototype.findIn =
function
(search){
var
r = Array();
for
(
var
i=0; i<
this
.length; i++)
if
(
this
.indexOf(search) != -1){
r.push(
this
.substr(
this
.indexOf(
"\t"
) + 1,
this
.length));
}
return
r;
}
var
usedFonts = app.activeDocument.fonts;
var
sysFonts = app.fonts.everyItem();
sysFontsList = sysFonts.fontFamily.unique();
sysFontsList.unshift(
"- Select Font Family -"
);
var
fontsWin =
new
Window(
'dialog'
,
'Document Fonts'
);
fontsWin.alignChildren =
'center'
;
var
allFonts = fontsWin.add(
'listbox'
, undefined, undefined, {numberOfColumns: 5, showHeaders:
true
, columnTitles: [
'Font Name'
,
'Font Style'
,
'Font PS Name'
,
'Font Type'
,
'Chr count'
]});
allFonts.minimumSize = [600,150];
var
myGrp = fontsWin.add(
'group'
);
myGrp.alignChildren =
'center'
;
var
availableFonts = myGrp.add(
'dropdownlist'
,undefined,sysFontsList);
var
availableStyles = myGrp.add(
'dropdownlist'
);
var
okButton = myGrp.add(
'button'
,undefined,
'OK'
);
var
okButton = myGrp.add(
'button'
,undefined,
'Cancel'
);
availableStyles.minimumSize = [180,25];
availableFonts.selection = 0;
availableFonts.onChange =
function
(){
availableStyles.removeAll();
var
sysFontAvailableStyles = sysFonts.name.findIn(availableFonts.selection);
for
(
var
i = 0; i < sysFontAvailableStyles.length; i++)availableStyles.add(
'item'
,sysFontAvailableStyles);
availableStyles.selection = 0;
}
//// SHOW JUST MISSING FONTS //// [START]
for
(
var
i = 0; i < usedFonts.length; i++){
if
(usedFonts.status != FontStatus.INSTALLED){
allFonts.add(
'item'
,usedFonts.fontFamily);
allFonts.items[allFonts.items.length-1].image =
'SystemWarningIcon'
;
allFonts.items[allFonts.items.length-1].subItems[3].text = findCharacters(usedFonts.name);
allFonts.items[allFonts.items.length-1].subItems[0].text = usedFonts.fontStyleName;
allFonts.items[allFonts.items.length-1].subItems[1].text = usedFonts.postscriptName;
allFonts.items[allFonts.items.length-1].subItems[2].text = usedFonts.fontType;
}
}
//// SHOW JUST MISSING FONTS //// [END]
allFonts.selection = 0;
fontsWin.center();
var
fontAnswer = fontsWin.show();
if
(fontAnswer ==
true
&& availableFonts.selection != 0){
var
sourceFont = allFonts.selection.text +
"\t"
+ allFonts.selection.subItems[0].text;
var
destFont = availableFonts.selection.text +
"\t"
+ availableStyles.selection.text;
changeFont(sourceFont,destFont);
}
else
{
alert(
'No Actions taken!'
);
}
function
findCharacters(findFont){
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat =
"."
;
app.findGrepPreferences.appliedFont = findFont;
var
result = Number(app.activeDocument.findGrep().length);
return
result;
}
function
changeFont(sourceFont, destinationFont){
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedFont = sourceFont;
app.changeTextPreferences.appliedFont = destinationFont;
app.activeDocument.changeText();
}
Copy link to clipboard
Copied
Wow there used to be a document.fonts property? That would sure be useful!
I'm also interested in how this is solved.. I remember something about using metadata these days for getting the document fonts.
Copy link to clipboard
Copied
no there's never been a document.fonts property. Above script is for InDesign.
Copy link to clipboard
Copied
Heh heh. Indeed it is. This has never ever worked for Illustrator – nor was it designed to.
Copy link to clipboard
Copied
there you go, thanks jw
Copy link to clipboard
Copied
Thanks man, do you have js/vb script for illustrator
Copy link to clipboard
Copied
Hi Mike, no sorry I don't
Copy link to clipboard
Copied
So I am not really sure what the purpose of your script is mostly because i dont understand parts of it, nor clear what what problems you are having.
However judging from the title of this thread you want to change the font of text at some point in your script?
Like this?
var active_doc = app.activeDocument;
var text_frames = active_doc.textFrames;
var afont = textFonts[0]
for (var i = 0 ; i < text_frames.length; i++) {
var this_text = text_frames.textRange;
this_text.characterAttributes.textFont = afont
}
And you want all the avaiable fonts?
the textFont Object has all the fonts available in Illustrator. I suppose you could loop through or what ever it is you need.
For fonts used in the document I suppose you could loop through all the textRanges and check the characterAttributes to get the font names.
I really dont know if any of this is what you are looking for.
Copy link to clipboard
Copied
it looks very useful. I will verify. The purpose is that I'd like use javascript to allow massive change font in illustrator cc.
Copy link to clipboard
Copied
So does this code make a costomize popup window populated with all availabe fonts and all fonts in use so you can change all instances of a font in a document???
Thats crazy. I didnt know one could make windows and that sort of thing.
Copy link to clipboard
Copied
Yes, with ScriptUI we can make interfaces inside dialogs and palettes, however it's important to also notice the difference in the scriptability of various applications: the Indesign app can be controlled in a far superior way than Illustrator. For example, with .jsx in Indesign you can add menu items onto the Indesign application menu bar, but this feature is not available for us here in Illustrator.
Copy link to clipboard
Copied
how can I get the fonts used in the document?
Copy link to clipboard
Copied
Here is a loop that runs through all textFrames and gets the font used.
#target "illustrator"
var active_doc = app.activeDocument;
var text_frames = active_doc.textFrames;
var usedFonts = []
for (var i = 0 ; i < text_frames.length; i++) {
var this_text_frame = text_frames;
usedFonts.push(this_text_frame.textRange.textFont.name)
}
However this is only good if all text in a single textFrame has the same Font. If you have multiple fonts in the same text frame then you would have to make a loop to look through every character in the document seperately. Below is a quick example.
#target "illustrator"
var active_doc = app.activeDocument;
var text_frames = active_doc.textFrames;
var usedFonts = []
var listed = false
for (var i = 0 ; i < text_frames.length; i++) {
var this_text_frame = text_frames;
for (var t =0 ; t < this_text_frame.characters.length; t++){
var aFont = this_text_frame.characters
InArray(aFont)
if(listed == false){
usedFonts.push(aFont )
}
listed = false
}
}
function InArray(quiry){
for (var n =0 ; n < usedFonts.length; n++){
var curFont = usedFonts
if(curFont == quiry){
listed = true
}
}
return listed
}
Copy link to clipboard
Copied
Thank you Sir! It's really helpful. Last question, how can I replace font for a text frame?
I doesn't work for the following script
this_text.characterAttributes.textFont = "Gotham_bold"
Copy link to clipboard
Copied
Have a look - the same problem with solution:
Copy link to clipboard
Copied
In my illustrator there is no Gotham Bold, though there may be in your, however what i notice is that the name does not seem to follow the normal naming structure. I believe names are normally capitalized without spaces followed by a dash and the style, also capitalized with out spaces. For example: "ACaslonPro-BoldItalic"
So I would guess that if your font is avaiable on your version of illustrator then it would be "Gotham-Bold".
Correction:
I just remembered this_text.characterAttributes.textFont must be equal to an textFont object.
It is possible however to get a textFont Object by name.
example:
afont = textFonts["ACaslonPro-BoldItalic"]
this_text.characterAttributes.textFont = afont