Script to Select and Raster similar text-layer (font-family + font weight)
Though this is not a usual use case, I am stuck with the scripting part. Really need help!!!
My use case :
I want to select text layer of my choice (for eg. CircularStd-Medium) out of several hundred text-layers while designing a UI, I may not know the no. of exact text layers which has that specific font-family or font-weight i.e CircularStd-Medium. It would quite hectic to do such things manually, clearly, need a script to resolve such issue. So I just want my script to select the text-layers which I want to target and rasterize them, without affecting other text-layer.
Problem with my script :
I got partial success to select & raster similar text-layers with specific font-family or font-weight i.e CircularStd-Medium. The only issue is that the script is not working with layer-group, nested-layers, artboards. It would be great if someone can enlighten me.
SCRIPT.jsx
var refPSD = new ActionReference();
function arrayUnique(a) {
var temp = []
i = a.length;
// ExtendScript has no indexOf function
while (i--) {
var found = false,
n = temp.length;
while (n--) {
if (a === temp
) { found = true;
}
}
if (!found) {
temp.push(a);
}
}
return temp;
}
function findFonts() {
refPSD.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
// Get layers from PSD
var countLayers = executeActionGet(refPSD).getInteger(charIDToTypeID('NmbL')) + 1,
fonts = [];
// Loop through each layer
while (countLayers--) {
var refLayer = new ActionReference(),
descLayer,
layerStyles,
countStyles;
refLayer.putIndex(charIDToTypeID('Lyr '), countLayers);
// Catch error when no backgroundLayer is present
try {
descLayer = executeActionGet(refLayer);
} catch (e) {
continue;
}
// Only proceed if text layer
if (!descLayer.hasKey(stringIDToTypeID('textKey'))) continue;
// Get list of styles (in case of multiple fonts in a text layer)
layerStyles = descLayer.getObjectValue(stringIDToTypeID('textKey')).getList(stringIDToTypeID('textStyleRange'));
countStyles = layerStyles.count;
// Loop through each style and get the font name
while (countStyles--) {
try {
var fontName = layerStyles.getObjectValue(countStyles).getObjectValue(stringIDToTypeID('textStyle')).getString(stringIDToTypeID('fontPostScriptName'));
fonts.push(fontName);
} catch (e) {
continue;
}
}
}
// Return a unique and sorted array of font names
return arrayUnique(fonts).sort();
}
if (documents.length) {
var fontsFound = findFonts();
alert(fontsFound.length + ' fonts found \n' + fontsFound.join('\n'));
} else {
alert('No fonts found \nOpen a PSD before running this script', );
}
var fontToRaster = prompt("Which font would you like to Raster?", "\(PostScript font name\)", "Raster Font");
var doc = app.activeDocument;
function RasterFont(target) {
var layers = target.layers;
for (var i = 0; i < layers.length; i++) {
if (layers.kind == LayerKind.TEXT) {
if (layers.textItem.font == fontToRaster) {
layers.rasterize(RasterizeType.TEXTCONTENTS);
};
};
};
};
RasterFont(doc);
Regards
