Skip to main content
Participating Frequently
April 2, 2024
Answered

Script to check right font for each group

  • April 2, 2024
  • 2 replies
  • 339 views

I need a script to check fonts for groups in a fixed file. Each group is assigned a different font, and when text with the wrong font is detected, a notification can be displayed. If the group does not exist or the group does not have text, it should be ignored.

I have attached a specific image below. Each group "speech", "narration", and "sfx" will have its own default font. For example:

  • sfx is Arial Narrow font
  • speech is Myriad Pro Regular font
  • narration is Arial Italic font

In the sample file I have attached, I have intentionally changed the text of "SFX3" to a different font. If the script can detect this, it should display a notification (e.g., "Incorrect font "SFX3")

Group "narration" in sample file have no text, then just ignore it.

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

edited 2024-04-03

 

 

// check fonts in group of name;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
    var theStuff = [["sfx", "Arial Narrow", "Regular"], ["speech", "Myriad Pro", "Regular"]];
    var theResults = new Array;
// check groups;
    for (var n = 0; n < theStuff.length; n++) {
        var theLayers = collectTypeLayersInGroupOfName (theStuff[n][0]);
        var theFont = theStuff[n][1];
        var theStyle = theStuff[n][2];
// check fonts;
        for (var m = 0; m < theLayers.length; m++) {
            var theCheck = true;
            var theseFonts = theLayers[m][3];
            for (var o = 0; o < theseFonts.length; o++) {
                if (theseFonts[o][0] != theFont || theseFonts[o][1] != theStyle) {theCheck = false}
            };
            if (theCheck == false) {theResults.push(theLayers[m])}
        };
    };
// results;
    if (theResults.length > 0) {
        selectLayerByID(theResults[0][2], false)
        for (var p = 1; p < theResults.length; p++) {selectLayerByID(theResults[p][2], true)};
        alert ("the layers\n\n"+theResults.join("\n\n")+"\n\ncontain incorrect fonts.");
    } else {alert ("no problem found")}
};
////////////////////////////////////
////// collect layers in group of name //////
function collectTypeLayersInGroupOfName (theName) {
var theGroup = collectLayersOfName (theName, true);
var theLayers = new Array;
for (var a = 0; a < theGroup.length; a++) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
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"));
var isTypeLayer = layerDesc.hasKey(stringIDToTypeID("textKey"));
var theParentId = layerDesc.getInteger(stringIDToTypeID('parentLayerID'));
// check group names;
var theCheck = false;
while (theParentId != -1 && theCheck == false) {
//if (Number(theParentId) == Number(theGroup[0][2])) {theCheck = true};
if (Number(theParentId) == Number(theGroup[a][2])) {theCheck = true};
var theParent = getLayerNameAndParentIdById (theParentId);
theParentId = theParent[3];
};
// if type layer collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true && isTypeLayer == true && theCheck == true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
var theFonts = new Array;
for (var x = 0; x < rangeList.count; x++) {
var thisOne = rangeList.getObjectValue(x);
var aFont = thisOne.getObjectValue(stringIDToTypeID('textStyle')).getString(stringIDToTypeID('fontName'));
var aStyle = thisOne.getObjectValue(stringIDToTypeID('textStyle')).getString(stringIDToTypeID('fontStyleName'));
var theFrom = thisOne.getInteger(stringIDToTypeID('from'));
var theTo = thisOne.getInteger(stringIDToTypeID('to'));
theFonts.push([aFont, aStyle, theFrom, theTo]);
};
theLayers.push([theName, theIndex, theID, theFonts])
};
}
catch (e) {};
};
};
return theLayers
};
////// collect layers //////
function collectLayersOfName (theString, isGroup) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
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 group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
if (layerSet == "layerSectionStart" && isGroup == true) {
if (theName == theString) {theLayers.push([theName, theIndex, theID, theColor])}
};
if (layerSet != "layerSectionStart" && isGroup == false) {
if (theName == theString) {theLayers.push([theName, theIndex, theID, theColor])}
};
};
}
catch (e) {};
};
return theLayers
};
////// get parentid //////
function getLayerNameAndParentIdById (theID) {
var ref = new ActionReference();
ref.putIdentifier( charIDToTypeID( "Lyr " ), theID);
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theParentId = layerDesc.getInteger(stringIDToTypeID('parentLayerID'));
return [theName, theIndex, theID, theParentId]
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
try {
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); 
}
} catch (e) {}
};

 

 

2 replies

c.pfaffenbichler
Community Expert
Community Expert
April 3, 2024

I updated the posted Script, have you been able to test it yet? 

KathyyyyAuthor
Participating Frequently
April 5, 2024

It's working really smooth! Thank you!

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

edited 2024-04-03

 

 

// check fonts in group of name;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
    var theStuff = [["sfx", "Arial Narrow", "Regular"], ["speech", "Myriad Pro", "Regular"]];
    var theResults = new Array;
// check groups;
    for (var n = 0; n < theStuff.length; n++) {
        var theLayers = collectTypeLayersInGroupOfName (theStuff[n][0]);
        var theFont = theStuff[n][1];
        var theStyle = theStuff[n][2];
// check fonts;
        for (var m = 0; m < theLayers.length; m++) {
            var theCheck = true;
            var theseFonts = theLayers[m][3];
            for (var o = 0; o < theseFonts.length; o++) {
                if (theseFonts[o][0] != theFont || theseFonts[o][1] != theStyle) {theCheck = false}
            };
            if (theCheck == false) {theResults.push(theLayers[m])}
        };
    };
// results;
    if (theResults.length > 0) {
        selectLayerByID(theResults[0][2], false)
        for (var p = 1; p < theResults.length; p++) {selectLayerByID(theResults[p][2], true)};
        alert ("the layers\n\n"+theResults.join("\n\n")+"\n\ncontain incorrect fonts.");
    } else {alert ("no problem found")}
};
////////////////////////////////////
////// collect layers in group of name //////
function collectTypeLayersInGroupOfName (theName) {
var theGroup = collectLayersOfName (theName, true);
var theLayers = new Array;
for (var a = 0; a < theGroup.length; a++) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
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"));
var isTypeLayer = layerDesc.hasKey(stringIDToTypeID("textKey"));
var theParentId = layerDesc.getInteger(stringIDToTypeID('parentLayerID'));
// check group names;
var theCheck = false;
while (theParentId != -1 && theCheck == false) {
//if (Number(theParentId) == Number(theGroup[0][2])) {theCheck = true};
if (Number(theParentId) == Number(theGroup[a][2])) {theCheck = true};
var theParent = getLayerNameAndParentIdById (theParentId);
theParentId = theParent[3];
};
// if type layer collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true && isTypeLayer == true && theCheck == true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
var theFonts = new Array;
for (var x = 0; x < rangeList.count; x++) {
var thisOne = rangeList.getObjectValue(x);
var aFont = thisOne.getObjectValue(stringIDToTypeID('textStyle')).getString(stringIDToTypeID('fontName'));
var aStyle = thisOne.getObjectValue(stringIDToTypeID('textStyle')).getString(stringIDToTypeID('fontStyleName'));
var theFrom = thisOne.getInteger(stringIDToTypeID('from'));
var theTo = thisOne.getInteger(stringIDToTypeID('to'));
theFonts.push([aFont, aStyle, theFrom, theTo]);
};
theLayers.push([theName, theIndex, theID, theFonts])
};
}
catch (e) {};
};
};
return theLayers
};
////// collect layers //////
function collectLayersOfName (theString, isGroup) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
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 group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
if (layerSet == "layerSectionStart" && isGroup == true) {
if (theName == theString) {theLayers.push([theName, theIndex, theID, theColor])}
};
if (layerSet != "layerSectionStart" && isGroup == false) {
if (theName == theString) {theLayers.push([theName, theIndex, theID, theColor])}
};
};
}
catch (e) {};
};
return theLayers
};
////// get parentid //////
function getLayerNameAndParentIdById (theID) {
var ref = new ActionReference();
ref.putIdentifier( charIDToTypeID( "Lyr " ), theID);
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theParentId = layerDesc.getInteger(stringIDToTypeID('parentLayerID'));
return [theName, theIndex, theID, theParentId]
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
try {
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); 
}
} catch (e) {}
};