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

Explorer ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied

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);

TOPICS
Scripting

Views

64

Likes

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

Community Expert , Sep 07, 2022 Sep 07, 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

Likes

Translate

Translate
Community Expert ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied

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

Likes

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
Explorer ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied

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 {}
}
}
}
}
}

}

Likes

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
Community Expert ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied

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

Likes

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
Explorer ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied

LATEST

It's always the small things in live...

I didn't know that there is no need to convert toString first.

And I accidently forgot ( ) first time testing...

 

Now it seems to work perfectly!

Thank you soo much for your fast answer! 

 

Here's the final 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.toUpperCase() == thisCLFont.toUpperCase() && charFontName.toUpperCase() == thisCLFont.toUpperCase()) {
// 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 {}
}
}
}
}
}
}

Likes

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

Save time with Adobe Express quick actions