Skip to main content
Participating Frequently
March 31, 2024
Answered

Script to replace all missing fonts in a Photoshop file with a specified font

  • March 31, 2024
  • 1 reply
  • 639 views

Hello,

In fact, I know it is possible to replace missing fonts with the default font by going to Type > Manage Missing Fonts > Replace Missing Fonts with Default Font. However, since I have a lot of files to process, and to save more time, I really want to find a script that can automatically replace missing fonts with another font that I specify (for example, I want to change the text with missing fonts to Arial font).

Thank you!

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Does this help? 

// replace missing fonts;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theFonts = getFonts ();
for (var m = 0; m < theFonts.length; m++) {
if (theFonts[m][2] == false) {replaceMissingFont (theFonts[m][0], theFonts[m][1])}
}
};
////////////////////////////////////
////// get fonts //////
function getFonts () {
var theFonts = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
//////
for (var m = theNumber; m >= 0; m--) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var hasText = layerDesc.hasKey(stringIDToTypeID("textKey"));
if (hasText == true) {
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var paragraphRangeList = textDesc.getList(stringIDToTypeID('paragraphStyleRange'));
var kernRange = textDesc.getList(stringIDToTypeID('kerningRange'));
var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
for (var o = 0; o < rangeList.count; o++) {
var styleDesc = rangeList.getObjectValue(o).getObjectValue(stringIDToTypeID('textStyle'));
// check for default font;
if (styleDesc.hasKey(stringIDToTypeID('fontName')) == true) {
    var available = styleDesc.getBoolean(stringIDToTypeID("fontAvailable"));
    var aFont = [styleDesc.getString(stringIDToTypeID('fontName')), styleDesc.getString(stringIDToTypeID("fontStyleName")), available]}
else {
	var theDefault = styleDesc.getObjectValue(stringIDToTypeID('baseParentStyle'));
    var available = styleDesc.getBoolean(stringIDToTypeID("fontAvailable"));
	var aFont = [theDefault.getString(stringIDToTypeID('fontName')), styleDesc.getString(stringIDToTypeID("fontStyleName")), available];
	};
// add to array;
var theCheck = true;
for (var n = 0; n < theFonts.length; n++) {
if (theFonts[n] == aFont) {theCheck = false}
};
if (theCheck  == true) {theFonts.push(aFont)}
}
}
}
}
catch (e) {};
};
return theFonts
};
////// replace missing font //////
function replaceMissingFont (fontName, styleName) {
try {
var desc10 = new ActionDescriptor();
var list1 = new ActionList();
var desc11 = new ActionDescriptor();
var desc12 = new ActionDescriptor();
desc12.putString( stringIDToTypeID( "fontName" ), fontName );
desc12.putString( stringIDToTypeID( "fontStyleName" ), styleName );
desc11.putObject( stringIDToTypeID( "fromFont" ), stringIDToTypeID( "fontSpec" ), desc12 );
var desc13 = new ActionDescriptor();
desc13.putString( stringIDToTypeID( "fontName" ), "Myriad Pro" );
desc13.putString( stringIDToTypeID( "fontStyleName" ), "Regular" );
desc11.putObject( stringIDToTypeID( "toFont" ), stringIDToTypeID( "fontSpec" ), desc13 );
list1.putObject( stringIDToTypeID( "fontRemapEntry" ), desc11 );
desc10.putList( stringIDToTypeID( "fontMap" ), list1 );
executeAction( stringIDToTypeID( "remapFonts" ), desc10, DialogModes.NO );
} catch (e) {};
};

 

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
April 1, 2024

Does this help? 

// replace missing fonts;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theFonts = getFonts ();
for (var m = 0; m < theFonts.length; m++) {
if (theFonts[m][2] == false) {replaceMissingFont (theFonts[m][0], theFonts[m][1])}
}
};
////////////////////////////////////
////// get fonts //////
function getFonts () {
var theFonts = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
//////
for (var m = theNumber; m >= 0; m--) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var hasText = layerDesc.hasKey(stringIDToTypeID("textKey"));
if (hasText == true) {
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var paragraphRangeList = textDesc.getList(stringIDToTypeID('paragraphStyleRange'));
var kernRange = textDesc.getList(stringIDToTypeID('kerningRange'));
var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
for (var o = 0; o < rangeList.count; o++) {
var styleDesc = rangeList.getObjectValue(o).getObjectValue(stringIDToTypeID('textStyle'));
// check for default font;
if (styleDesc.hasKey(stringIDToTypeID('fontName')) == true) {
    var available = styleDesc.getBoolean(stringIDToTypeID("fontAvailable"));
    var aFont = [styleDesc.getString(stringIDToTypeID('fontName')), styleDesc.getString(stringIDToTypeID("fontStyleName")), available]}
else {
	var theDefault = styleDesc.getObjectValue(stringIDToTypeID('baseParentStyle'));
    var available = styleDesc.getBoolean(stringIDToTypeID("fontAvailable"));
	var aFont = [theDefault.getString(stringIDToTypeID('fontName')), styleDesc.getString(stringIDToTypeID("fontStyleName")), available];
	};
// add to array;
var theCheck = true;
for (var n = 0; n < theFonts.length; n++) {
if (theFonts[n] == aFont) {theCheck = false}
};
if (theCheck  == true) {theFonts.push(aFont)}
}
}
}
}
catch (e) {};
};
return theFonts
};
////// replace missing font //////
function replaceMissingFont (fontName, styleName) {
try {
var desc10 = new ActionDescriptor();
var list1 = new ActionList();
var desc11 = new ActionDescriptor();
var desc12 = new ActionDescriptor();
desc12.putString( stringIDToTypeID( "fontName" ), fontName );
desc12.putString( stringIDToTypeID( "fontStyleName" ), styleName );
desc11.putObject( stringIDToTypeID( "fromFont" ), stringIDToTypeID( "fontSpec" ), desc12 );
var desc13 = new ActionDescriptor();
desc13.putString( stringIDToTypeID( "fontName" ), "Myriad Pro" );
desc13.putString( stringIDToTypeID( "fontStyleName" ), "Regular" );
desc11.putObject( stringIDToTypeID( "toFont" ), stringIDToTypeID( "fontSpec" ), desc13 );
list1.putObject( stringIDToTypeID( "fontRemapEntry" ), desc11 );
desc10.putList( stringIDToTypeID( "fontMap" ), list1 );
executeAction( stringIDToTypeID( "remapFonts" ), desc10, DialogModes.NO );
} catch (e) {};
};

 

KathyyyyAuthor
Participating Frequently
April 2, 2024

yes, thank you!