6 Replies Latest reply on Jul 1, 2016 9:10 AM by Jamesthe500

    multiple fonts in a textItem using jsx

    Aubreyff Level 1

      Hi,

       

       

      I am saving currentLayer.textItem.contents to a text file. That part is working. But I would also like to save attributes about the textItem, specifically the fonts being used and style attributes in the textItem.

      However, I am only able to find the first font using currentLayer.textItem.font . Can I somehow find a list of all fonts and attributes used in a specific textItem?

       

       

      Thanks

        • 1. Re: multiple fonts in a textItem using jsx
          pixxxel schubser MVP & Adobe Community Professional

          if available - search in estk after the properties of text item

          • 2. Re: multiple fonts in a textItem using jsx
            Aubreyff Level 1

            Hi pixxxel schubser,

             

            I'm not sure what you mean by "estk"?

             

            Would I do that like this:

             

            for (var i = 0; i<currentLayer.textItem.estk.properties.length; i++){

            txtFile.writeln('property'+ i +':  ' +currentLayer.textItem.estk.properties[i]);

            }

            • 3. Re: multiple fonts in a textItem using jsx
              Muppet Mark Level 5

              Using the standard DOM you can only access the attributes of first character of a text layer… or thats what I recall… It may be possible with action manager to dig deeper into this but I've never tried… More than likely too complex for me… I think pixxxel means for you to look in the Object Model Viewer ( omv ) of the ExtendScript Toolkit ( estk ) just key comm+/ on the mac

              • 4. Re: multiple fonts in a textItem using jsx
                Paul Riggott Level 6

                As Mark has said it can get very messy getting the information you want, here is a snippet that a few people have worked on and might be of use to you...

                 

                 

                    function getColorFromDescriptor(colorDesc, keyClass) {
                      var colorObject = new SolidColor();
                      switch (keyClass) {
                      case "Grsc":
                        colorObject.grey.grey = color.getDouble(charIDToTypeID('Gry '));
                        break;
                      case "RGBC":
                        colorObject.rgb.red = colorDesc.getDouble(charIDToTypeID('Rd  '));
                        colorObject.rgb.green = colorDesc.getDouble(charIDToTypeID('Grn '));
                        colorObject.rgb.blue = colorDesc.getDouble(charIDToTypeID('Bl  '));
                        break;
                      case "CMYC":
                        colorObject.cmyk.cyan = colorDesc.getDouble(charIDToTypeID('Cyn '));
                        colorObject.cmyk.magenta = colorDesc.getDouble(charIDToTypeID('Mgnt'));
                        colorObject.cmyk.yellow = colorDesc.getDouble(charIDToTypeID('Ylw '));
                        colorObject.cmyk.black = colorDesc.getDouble(charIDToTypeID('Blck'));
                        break;
                      case "LbCl":
                        colorObject.lab.l = colorDesc.getDouble(charIDToTypeID('Lmnc'));
                        colorObject.lab.a = colorDesc.getDouble(charIDToTypeID('A   '));
                        colorObject.lab.b = colorDesc.getDouble(charIDToTypeID('B   '));
                        break;
                      default:
                        return null;
                      }
                      return colorObject;
                    };
                    // get fonts and other parameters used in type layer
                    function getFonts(textLayer) {
                var font_content_detection = false;
                      function markReturnedContentText(text) {
                        if (font_content_detection) {
                          return font_content_detection_symbols[0] + text + font_content_detection_symbols[1] + "\r";
                        } else {
                          return text;
                        }
                      }
                      if (textLayer.kind == LayerKind.TEXT) {
                        app.activeDocument.activeLayer = textLayer;
                        var ref = new ActionReference();
                        ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
                        var layerDesc = executeActionGet(ref);
                        var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
                        var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
                        var fonts = [];
                        for (var m = 0; m < rangeList.count; m++) {
                          var styleDesc = rangeList.getObjectValue(m).getObjectValue(stringIDToTypeID('textStyle'));
                          var aFrom = rangeList.getObjectValue(m).getInteger(stringIDToTypeID('from'));
                          var aTo = rangeList.getObjectValue(m).getInteger(stringIDToTypeID('to'));
                          if (m > 0) {
                            if (rangeList.getObjectValue(m - 1)
                              .getInteger(stringIDToTypeID('from')) == aFrom && rangeList.getObjectValue(m - 1)
                              .getInteger(stringIDToTypeID('to')) == aTo) continue;
                          }
                          var theLetters = app.activeDocument.activeLayer.textItem.contents.substring(aFrom, aTo);
                          var aFont = styleDesc.getString(stringIDToTypeID('fontPostScriptName'));
                          var aSize = new UnitValue(styleDesc.getUnitDoubleValue(stringIDToTypeID('size')), "px");
                          //Check if font has been transformed
                            if (textDesc.hasKey(stringIDToTypeID('transform'))) {
                                var mFactor = textDesc.getObjectValue(stringIDToTypeID('transform')).getUnitDoubleValue (stringIDToTypeID("yy") );
                                aSize = Math.round(aSize * mFactor);
                                }
                          var aColor = getColorFromDescriptor(styleDesc.getObjectValue(charIDToTypeID("Clr ")), typeIDToCharID(styleDesc.getClass(charIDToTypeID("Clr "))));
                       
                          if (styleDesc.hasKey(stringIDToTypeID('leading'))) {
                            var aLeading = new UnitValue(styleDesc.getUnitDoubleValue(stringIDToTypeID('leading')), "px");
                          } else {
                            var aLeading = "";
                          }
                        var txt = theLetters;
                          var merged = false;
                          if (txt.length > 0) {
                            for (var x = 0; x < m; x++) {
                              try {
                                if (fonts[x].font === aFont && fonts[x].size === aSize && fonts[x].color.rgb.hexValue === aColor.rgb.hexValue && fonts[x].leading === aLeading) {
                                  // It's a hack!!!
                                  if (fonts[x].text !== txt) {
                                    fonts[x].text += markReturnedContentText(txt);
                                  }
                                  merged = true;
                                }
                              } catch (e) {}
                            }
                            if (!merged) {
                              fonts.push({
                                text: markReturnedContentText(txt),
                                font: aFont,
                                size: aSize,
                                color: aColor,
                                leading: aLeading
                              });
                            }
                          }
                        };
                        return fonts;
                      }
                  };
                var f = getFonts(activeDocument.activeLayer);
                for(var s in f) {
                    $.writeln( f[s].font + " - " + f[s].color.rgb.hexValue + " - " + Number(f[s].size).toFixed(2) + " - [" + f[s].text + "] - " + Number(f[s].leading));
                    }
                
                
                 
                1 person found this helpful
                • 5. Re: multiple fonts in a textItem using jsx
                  Aubreyff Level 1

                  Thank you, Thank you, Thank you. Paul and Mark and pixxxel schubser.

                   

                  I like this forum and I don't mind messy. It worked.

                  • 6. Re: multiple fonts in a textItem using jsx
                    Jamesthe500

                    This was so helpful, thanks. One note, I think Adobe changed some of its endpoint names. In order to get this to work, I had to change lines 38 & 39 to:

                     

                    if (textLayer.kind == TextType.POINTTEXT) {  
                            app.activeDocument.activeLayer = textLayer.parent;  
                    

                     

                    I wasn't interested in the color functionality, so if there are bugs there, I don't know about them.