Skip to main content
Participant
April 17, 2020
Question

Action or Script to replacing fonts in multiple documents

  • April 17, 2020
  • 2 replies
  • 777 views

I am looking for a script that will allow me to replace all the instances of ANY typeface regardless of size or weight to one specific typeface/weight (size should remain the same) across multiple - several hundred - files.

 

I've browsed the boards and seen a few references to something similar, but none of them seem to work in practice for this need.

 

Sarah

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
April 23, 2020

There was something similar … 

https://community.adobe.com/t5/photoshop/how-can-i-randomly-apply-one-of-a-limited-number-of-fonts-to-a-set-of-layered-images-in-data-driven/td-p/9569685?page=1

 

Try this: 

 

// 2020, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theFonts = ["Courier-Bold"];
var theNumberOfFonts = theFonts.length;
var theTypeLayers = collectTypeLayers ();
for (var m = 0; m < theTypeLayers.length; m++) {
changeFont (theFonts[Math.floor (Math.random()*theNumberOfFonts)], theTypeLayers[m][1])
};
};
////// collect type layers //////
function collectTypeLayers () {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; 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"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true && layerDesc.hasKey(stringIDToTypeID("textKey")) == true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
theLayers.push([theName, theID])
};
}
catch (e) {};
};
return theLayers
};
////// change font //////
function changeFont (theFont, theID) {
// =======================================================
var idsetd = charIDToTypeID( "setd" );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idPrpr = charIDToTypeID( "Prpr" );
        var idTxtS = charIDToTypeID( "TxtS" );
        ref1.putProperty( idPrpr, idTxtS );
        ref1.putIdentifier(charIDToTypeID( "TxLr" ), theID);
    desc2.putReference( idnull, ref1 );
    var idT = charIDToTypeID( "T   " );
        var desc3 = new ActionDescriptor();
        var idfontPostScriptName = stringIDToTypeID( "fontPostScriptName" );
        desc3.putString( idfontPostScriptName, theFont );
    desc2.putObject( idT, idTxtS, desc3 );
executeAction( idsetd, desc2, DialogModes.NO );
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), id);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};

 

 

From this 

to this

 

c.pfaffenbichler
Community Expert
Community Expert
April 23, 2020

I omitted to mention: You would still need to combine the Script with Batch or Image Processor etc. to process multiple images in one go. 

c.pfaffenbichler
Community Expert
Community Expert
April 23, 2020

I suppose that would take a Script and not necessarily a simple one at that … how important/frequent is the task for you? 

Have you found any Scripts that approach what you are after?