Copy link to clipboard
Copied
Hello!
In my day to day job I'm often having to transfer supplied art work into Indesign. One of the things I have to do is duplicate the fonts (weight, color, etc) to an Indesign file and have it editable. To help with this I created this script that lists the layers, font, weight and color, and then saves it to at text file so I don't have to constantly jump back and forth between programs. It works pretty well but unfortunately it can't really give me the CMYK listings (I work in print) and tends to just error out when I run it. I was hoping maybe someone in the community could look it over and see where the hang up was.
-Matt
#target photoshop
function findFontInstances() {
var results = [];
var refPSD = new ActionReference();
refPSD.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
var docDesc = executeActionGet(refPSD);
var docModeID = docDesc.getEnumerationValue(stringIDToTypeID("mode"));
var isCMYKDoc = (typeIDToStringID(docModeID) === "CMYKColor");
var layerCount = docDesc.getInteger(charIDToTypeID('NmbL')) + 1;
for (var i = 0; i < layerCount; i++) {
var refLayer = new ActionReference();
refLayer.putIndex(charIDToTypeID('Lyr '), i);
var descLayer;
try {
descLayer = executeActionGet(refLayer);
} catch (e) {
continue;
}
if (!descLayer.hasKey(stringIDToTypeID('textKey'))) continue;
var layerName = descLayer.hasKey(stringIDToTypeID("name"))
? descLayer.getString(stringIDToTypeID("name"))
: "Unnamed Layer";
var textKey = descLayer.getObjectValue(stringIDToTypeID('textKey'));
var styleList = textKey.getList(stringIDToTypeID('textStyleRange'));
for (var j = 0; j < styleList.count; j++) {
var textStyle = styleList.getObjectValue(j).getObjectValue(stringIDToTypeID('textStyle'));
var fontName = textStyle.getString(stringIDToTypeID('fontPostScriptName'));
var fontStyle = textStyle.hasKey(stringIDToTypeID('fontStyleName')) ? textStyle.getString(stringIDToTypeID('fontStyleName')) : "Unknown";
var fontSize = textStyle.hasKey(stringIDToTypeID('size')) ? textStyle.getDouble(stringIDToTypeID('size')) : "Unknown";
var colorInfo = "Unknown color";
if (textStyle.hasKey(stringIDToTypeID('color'))) {
try {
var colorDesc = textStyle.getObjectValue(stringIDToTypeID('color'));
var solidColor = new SolidColor();
if (
colorDesc.hasKey(stringIDToTypeID('red')) &&
colorDesc.hasKey(stringIDToTypeID('green')) &&
colorDesc.hasKey(stringIDToTypeID('blue'))
) {
solidColor.rgb.red = colorDesc.getDouble(stringIDToTypeID('red'));
solidColor.rgb.green = colorDesc.getDouble(stringIDToTypeID('green'));
solidColor.rgb.blue = colorDesc.getDouble(stringIDToTypeID('blue'));
colorInfo = "RGB: " +
Math.round(solidColor.rgb.red) + ", " +
Math.round(solidColor.rgb.green) + ", " +
Math.round(solidColor.rgb.blue);
} else if (
colorDesc.hasKey(stringIDToTypeID('cyan')) &&
isCMYKDoc
) {
solidColor.cmyk.cyan = colorDesc.getDouble(stringIDToTypeID('cyan'));
solidColor.cmyk.magenta = colorDesc.getDouble(stringIDToTypeID('magenta'));
solidColor.cmyk.yellow = colorDesc.getDouble(stringIDToTypeID('yellow'));
solidColor.cmyk.black = colorDesc.getDouble(stringIDToTypeID('black'));
colorInfo = "CMYK: " +
Math.round(solidColor.cmyk.cyan) + ", " +
Math.round(solidColor.cmyk.magenta) + ", " +
Math.round(solidColor.cmyk.yellow) + ", " +
Math.round(solidColor.cmyk.black);
} else if (
colorDesc.hasKey(stringIDToTypeID('cyan')) &&
!isCMYKDoc
) {
colorInfo = "CMYK color (not accessible in RGB document)";
}
} catch (e) {
colorInfo = "Error reading color";
}
}
results.push(
"Layer: " + layerName +
" | Font: " + fontName +
" | Style: " + fontStyle +
" | Size: " + fontSize + " pt" +
" | " + colorInfo
);
}
}
return results;
}
function saveWithDialog(text) {
var defaultName = app.activeDocument.name.replace(/\.[^\.]+$/, "") + "_FontReport.txt";
var saveFile = File.saveDialog("Save font report as .txt", defaultName);
if (saveFile) {
saveFile.open('w');
saveFile.write(text);
saveFile.close();
alert("Font info saved to:\n" + saveFile.fsName);
}
}
if (documents.length) {
var fontInstances = findFontInstances();
if (fontInstances.length > 0) {
var output = fontInstances.join('\n\n');
saveWithDialog(output);
} else {
alert("No font instances found.");
}
} else {
alert("Open a PSD file before running the script.");
}
Maybe:
function findFontInstances() {
var results = [];
var refPSD = new ActionReference();
refPSD.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
var docDesc = executeActionGet(refPSD);
var docModeID = docDesc.getEnumerationValue(stringIDToTypeID("mode"));
var isCMYKDoc = (typeIDToStringID(docModeID) === "CMYKColor");
var layerCount = docDesc.getInteger(charIDToTypeID('NmbL')) + 1;
for (var i = 0; i < layerCount; i++) {
var refLayer = new ActionReference(
...
Copy link to clipboard
Copied
Maybe:
function findFontInstances() {
var results = [];
var refPSD = new ActionReference();
refPSD.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
var docDesc = executeActionGet(refPSD);
var docModeID = docDesc.getEnumerationValue(stringIDToTypeID("mode"));
var isCMYKDoc = (typeIDToStringID(docModeID) === "CMYKColor");
var layerCount = docDesc.getInteger(charIDToTypeID('NmbL')) + 1;
for (var i = 0; i < layerCount; i++) {
var refLayer = new ActionReference();
refLayer.putIndex(charIDToTypeID('Lyr '), i);
var descLayer;
try {
descLayer = executeActionGet(refLayer);
} catch (e) {
continue;
}
if (!descLayer.hasKey(stringIDToTypeID('textKey'))) continue;
var layerName = descLayer.hasKey(stringIDToTypeID("name"))
? descLayer.getString(stringIDToTypeID("name"))
: "Unnamed Layer";
var textKey = descLayer.getObjectValue(stringIDToTypeID('textKey'));
var styleList = textKey.getList(stringIDToTypeID('textStyleRange'));
for (var j = 0; j < styleList.count; j++) {
var textStyle = styleList.getObjectValue(j).getObjectValue(stringIDToTypeID('textStyle'));
var fontName = textStyle.getString(stringIDToTypeID('fontPostScriptName'));
var fontStyle = textStyle.hasKey(stringIDToTypeID('fontStyleName')) ? textStyle.getString(stringIDToTypeID('fontStyleName')) : "Unknown";
var fontSize = textStyle.hasKey(stringIDToTypeID('size')) ? textStyle.getDouble(stringIDToTypeID('size')) : "Unknown";
var colorInfo = "Unknown color";
if (textStyle.hasKey(stringIDToTypeID('color'))) {
//try {
var colorDesc = textStyle.getObjectValue(stringIDToTypeID('color'));
var solidColor = new SolidColor();
if (
colorDesc.hasKey(stringIDToTypeID('red')) &&
colorDesc.hasKey(stringIDToTypeID('green')) &&
colorDesc.hasKey(stringIDToTypeID('blue'))
) {
solidColor.rgb.red = colorDesc.getDouble(stringIDToTypeID('red'));
solidColor.rgb.green = colorDesc.getDouble(stringIDToTypeID('green'));
solidColor.rgb.blue = colorDesc.getDouble(stringIDToTypeID('blue'));
colorInfo = "RGB: " +
Math.round(solidColor.rgb.red) + ", " +
Math.round(solidColor.rgb.green) + ", " +
Math.round(solidColor.rgb.blue);
} else {
if (colorDesc.hasKey(stringIDToTypeID('cyan'))) {
solidColor.cmyk.cyan = colorDesc.getDouble(stringIDToTypeID('cyan'));
solidColor.cmyk.magenta = colorDesc.getDouble(stringIDToTypeID('magenta'));
solidColor.cmyk.yellow = colorDesc.getDouble(stringIDToTypeID('yellowColor'));
solidColor.cmyk.black = colorDesc.getDouble(stringIDToTypeID('black'));
colorInfo = "CMYK: " +
Math.round(solidColor.cmyk.cyan) + ", " +
Math.round(solidColor.cmyk.magenta) + ", " +
Math.round(solidColor.cmyk.yellow) + ", " +
Math.round(solidColor.cmyk.black);
} else {
if (colorDesc.hasKey(stringIDToTypeID('cyan')) && !isCMYKDoc) {
colorInfo = "CMYK color (not accessible in RGB document)";
}
}
}
//} catch (e) {colorInfo = "Error reading color"}
}
results.push(
"Layer: " + layerName +
" | Font: " + fontName +
" | Style: " + fontStyle +
" | Size: " + fontSize + " pt" +
" | " + colorInfo
);
}
}
return results;
}
function saveWithDialog(text) {
var defaultName = app.activeDocument.name.replace(/\.[^\.]+$/, "") + "_FontReport.txt";
var saveFile = File.saveDialog("Save font report as .txt", defaultName);
if (saveFile) {
saveFile.open('w');
saveFile.write(text);
saveFile.close();
alert("Font info saved to:\n" + saveFile.fsName);
}
}
if (documents.length) {
var fontInstances = findFontInstances();
if (fontInstances.length > 0) {
var output = fontInstances.join('\n\n');
saveWithDialog(output);
} else {
alert("No font instances found.");
}
} else {
alert("Open a PSD file before running the script.");
}
Copy link to clipboard
Copied
Thanks @c.pfaffenbichler that worked perfectly! Appreciate the help!
-Matt