Getting layer font info if more than one font/size is used.

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Kukurykus, Tom, xbytor

Mike Hale

Getting layer font info if more than one font/size is used.

Post by Mike Hale »

If a text layer has different text ranges. textItem only returns info for the first. With the help of Xbytor, here are two function that can be used to get the font info if there is more than one text range. This only gets the info. If you want to set the info see Xbytor's text scripts bb/viewtopic.php?t=479

The first function returns the number of text ranges of a text layer. If it returns one, you can use the normal script interface to get the info.

Code: Select all///////////////////////////////////////////////////////////////////////////////
// Function: getTextRangeCount
// Description: Gets the number of text ranges in a textItem
// Usage: getTextRangeCount()
// Input: None
// Return: Intger =  the number of ranges
///////////////////////////////////////////////////////////////////////////////
function getTextRangeCount(){
var ref = new ActionReference();
   ref.putEnumerated( stringIDToTypeID( "layer" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ));// activeLayer
var desc= executeActionGet( ref )
var list =  desc.getObjectValue(charIDToTypeID("Txt ")) ;// textItem
var tsr =  list.getList(charIDToTypeID("Txtt")) ;// text range(s)
return tsr.count
}   
alert(getTextRangeCount())


If there are more than one you can use this next function to get the info about each range.



Code: Select all///////////////////////////////////////////////////////////////////////////////
// Function: getFontInfo
// Description: Gets info about font if more than one textrange
// Usage: getFontInfo() works on activeDocument.activeLayer
// Input: None
// Return: Array. array[idx] = info for textRange idx
///////////////////////////////////////////////////////////////////////////////
function getFontInfo(){
var ref = new ActionReference();
   ref.putEnumerated( stringIDToTypeID( "layer" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ));
var desc= executeActionGet( ref )
var list =  desc.getObjectValue(charIDToTypeID("Txt ")) ;
var tsr =  list.getList(charIDToTypeID("Txtt")) ;
var info = new Array;
for(var i = 0;i<tsr.count;i++){
   var tsr0 =  tsr.getObjectValue(i) ;
   var from = tsr0.getInteger(charIDToTypeID("From"));
   var to = tsr0.getInteger(charIDToTypeID("T   "));
   var range = [from,to];
   var textStyle = tsr0.getObjectValue(charIDToTypeID("TxtS"));
   var font = textStyle.getString(charIDToTypeID("FntN" ));
   var size = textStyle.getDouble(charIDToTypeID("Sz  " ));
   var color = textStyle.getObjectValue(charIDToTypeID('Clr '));
   var textColor = new SolidColor;
      textColor.rgb.red = color.getDouble(charIDToTypeID('Rd  '));
      textColor.rgb.green = color.getDouble(charIDToTypeID('Grn '));
      textColor.rgb.blue = color.getDouble(charIDToTypeID('Bl  '));
   info.push([range,font,size, textColor.rgb.hexValue]);
   }
return info;
}   

var info = getFontInfo();
alert(info[0]);

Both function use the activeLayer. You can change that by changing the 'ref' line as follows;

Code: Select all   ref.putIndex( charIDToTypeID( "Lyr " ), 2 );//by index or
   ref.putName( charIDToTypeID( "Lyr " ), "Layer 2" );//by name