Skip to main content
Community Expert
September 7, 2022
Answered

Illustrator Script – replace font by name case insensitive(!) Help needed!

  • September 7, 2022
  • 1 reply
  • 859 views

I got some sort of trapped in the core part of my script:

 

Dealing with nested arrays created of lists the script runs well replacing fonts by their names so far.

Now this has to be done case insensitive(!)

For instance a font should be replaced no matter if it's Myriad | MYriad | myRIad.

 

First thing I tried was to use indexOf() but this did not work out as there are might be Myriad-Regular, Myriad-Bold, Myriad-Italic... on later lists and it is only supposed to change maybe the Bold one.

 

Second try was .toString. However this works everywhere but not in this particular function...

 

Any solution anyone?

Help is very much appriciated!

And as usual just to keep away useless comments: No it can not be done manually for a reasion!

 

Here's the Script + attached Demo-.ai (.pdf pls rename as .ai after download)

Feel free to change any fonts anyhow to fit to your OS/computer:

 

 

 

#target illustrator

if (app.documents.length > 0) {

var docRef = app.activeDocument;
var allTextFrames = docRef.textFrames;
var splittedRows = [
['ArialMT','PoloCEF-Regular'],
['MyriadPro-Regular','Arial-BoldMT'],
['Bembo','PoloCEF-Regular','U+1234','U+4321'],
['PoloST11K-Buch','PoloCEF-Regular','U+666','U+876']
];
var autoSubList = ['ArialMT','PoloST11K-Buch','Bembo'];

changeFontAndGlyphs();

 

// replace unicode glyphs based on changelist
function changeFontAndGlyphs() {
for (var i=0 ; i<allTextFrames.length; i++) {
var allCharacters = allTextFrames[i].characters;
for (var t=0; t<allCharacters.length; t++) {
var charFontName = allCharacters[t].textFont.name;
var thisChar = allCharacters[t];

for (var c=0; c<splittedRows.length; c++) {
var thisCLFont = splittedRows[c][0];
var newCLFont = splittedRows[c][1];

for (var a=0; a<autoSubList.length; a++) {
var thisASLFont = autoSubList[a];

if (thisASLFont == thisCLFont && charFontName == thisCLFont) {
// if AutoSubList font is on changeListFont && character font has same name as changeListFont
thisChar.textFont = app.textFonts.getByName(newCLFont); //change character font to new changeList font
if (splittedRows[c].length > 2) { // changelist row >2 ==> has Glyphs to change
var thisGlyph = splittedRows[c][2];
var thisSubGlyph = splittedRows[c][3];
thisSplitArray = thisGlyph.split('\+');
thisSplitArray.shift(); //remove first array element
thisSubSplitArray = thisSubGlyph.split('\+');
thisSubSplitArray.shift(); //remove first array element

thisUcode = String.fromCharCode(parseInt(thisSplitArray,16)); //parse value to string
newUcode = String.fromCharCode(parseInt(thisSubSplitArray,16)); //parse value to string
fakeCode = "xxx"; //to avoid issues with unknown unicode characters replace with common

thisChar.contents = thisChar.contents.replace( thisUcode, fakeCode );//to avoid issues with unknown unicode characters in different fonts replace with common
thisChar.textFont = app.textFonts.getByName(newCLFont);//change font a second time
thisChar.contents = thisChar.contents.replace( fakeCode, newUcode );//change to new unicode
} else {}
} else {}
}
}
}
}
}

}

//did not work out:
//var thisASLFontSTRING = thisASLFont.toString.toUpperCase();
//var thisCLFontSTRING = thisCLFont.toString.toUpperCase();
//var charFontNameSTRING = charFontName.toString.toUpperCase();
//alert("thisASLFont: "+thisASLFont+", thisCLFont: "+thisCLFont+", charFontName: "+charFontName);

This topic has been closed for replies.
Correct answer Manan Joshi

Ok so if I understand it correctly you want to change the following line into case insensitive match

 

if (thisASLFont == thisCLFont && charFontName == thisCLFont)

 

Then what happens if you make the comparison after changing all to lowercase. Something like the following

 

if (thisASLFont.toLowerCase() == thisCLFont.toLowerCase() && charFontName.toLowerCase() == thisCLFont.toLowerCase()) 

 

-Manan

1 reply

Community Expert
September 7, 2022

Can you simplify your code to just exemplify the problem. It currently has much more stuff that requires more time to look into. I think your question pertains to looking/matching in splittedRows and autoSubList. If you could explain it a bit more that would be great.

-Manan

-Manan
Community Expert
September 7, 2022

Hi Manan,

 

sure:

based on the existing characters the font should be replaced by a system font which appears in both the splittedRows AND(!) the autoSubList array

Here's a simplified version of the script:

 

#target illustrator

if (app.documents.length > 0) {

var docRef = app.activeDocument;
var allTextFrames = docRef.textFrames;
var splittedRows = [
['ArialMT','PoloCEF-Regular'],
['MyriadPro-Regular','Arial-BoldMT'],
['Bembo','PoloCEF-Regular','U+1234','U+4321'],
['PoloST11K-Buch','PoloCEF-Regular','U+666','U+876']
];
var autoSubList = ['ArialMT','PoloST11K-Buch','Bembo'];

changeFontAndGlyphs();

 

// replace unicode glyphs based on changelist
function changeFontAndGlyphs() {
for (var i=0 ; i<allTextFrames.length; i++) {
var allCharacters = allTextFrames[i].characters;
for (var t=0; t<allCharacters.length; t++) {
var charFontName = allCharacters[t].textFont.name;
var thisChar = allCharacters[t];

for (var c=0; c<splittedRows.length; c++) {
var thisCLFont = splittedRows[c][0];
var newCLFont = splittedRows[c][1];

for (var a=0; a<autoSubList.length; a++) {
var thisASLFont = autoSubList[a];

if (thisASLFont == thisCLFont && charFontName == thisCLFont) {
// if AutoSubList font is on changeListFont && character font has same name as changeListFont
thisChar.textFont = app.textFonts.getByName(newCLFont); //change character font to new changeList font
} else {}
} else {}
}
}
}
}
}

}

Manan JoshiCommunity ExpertCorrect answer
Community Expert
September 7, 2022

Ok so if I understand it correctly you want to change the following line into case insensitive match

 

if (thisASLFont == thisCLFont && charFontName == thisCLFont)

 

Then what happens if you make the comparison after changing all to lowercase. Something like the following

 

if (thisASLFont.toLowerCase() == thisCLFont.toLowerCase() && charFontName.toLowerCase() == thisCLFont.toLowerCase()) 

 

-Manan

-Manan