Undo States (Undo Once)

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

mycort

Undo States (Undo Once)

Post by mycort »

Right now, this code has a series of function:
1)reading text
2)adding text layer
3)Name Text Layer
4) Not sure what happens at this state.....but when I undo several times....nothing happens for awhile
5) Add font info to text layer
6) Change smoothing option to be "Sharp"

Is it possible to undo only once and have it totally clear the text layer creation completely, instead of undoing at every state one at a time?

Code: Select allfunction multiselectfontreader() {

  app.bringToFront();
  main();

  function main() {
    if (!documents.length) return;
    var selLayers = [];
    selLayers = getSelectedLayersIdx();
    for (var a in selLayers) {
      makeActiveByIndex(selLayers[a], false);
      //amend to suit.
      readTextLayer()
    }
  }

  function getSelectedLayersIdx() {
    var selectedLayers = new Array;
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref);
    if (desc.hasKey(stringIDToTypeID('targetLayers'))) {
      desc = desc.getList(stringIDToTypeID('targetLayers'));
      var c = desc.count
      var selectedLayers = new Array();
      for (var i = 0; i < c; i++) {
        try {
          activeDocument.backgroundLayer;
          selectedLayers.push(desc.getReference(i)
            .getIndex());
        } catch (e) {
          selectedLayers.push(desc.getReference(i)
            .getIndex() + 1);
        }
      }
    } else {
      var ref = new ActionReference();
      ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("ItmI"));
      ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
      try {
        activeDocument.backgroundLayer;
        selectedLayers.push(executeActionGet(ref)
          .getInteger(charIDToTypeID("ItmI")) - 1);
      } catch (e) {
        selectedLayers.push(executeActionGet(ref)
          .getInteger(charIDToTypeID("ItmI")));
      }
    }
    return selectedLayers;
  };

  function makeActiveByIndex(idx, visible) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), idx)
    desc.putReference(charIDToTypeID("null"), ref);
    desc.putBoolean(charIDToTypeID("MkVs"), visible);
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
  };
};

// Read one or more selected fonts
function readTextLayer() {
  if (app.documents.length > 0) {
    var myDoc = app.activeDocument;
    var myLayer = myDoc.activeLayer;
    if (myLayer.kind == LayerKind.TEXT) {
      var thisText = myLayer.textItem;
      // Renaming the Justifcation info from Justification.CENTER - > Center;
      try {
        var theJustification = String(thisText.justification)
          .replace("Justification.", "")
      } catch (e) {
        var theJustification = "LEFT"
      };

      theJustification = theJustification[0] + theJustification.slice(1, theJustification.length)
        .toLowerCase();
      
var fonts = getFonts(app.activeDocument.activeLayer);

      for (var i = 0; i < fonts.length; i++) {
        addTextLayer(
          ((fonts.length > 1) ? fonts.text + ((fonts.text.length) ? "\r" : '') : '') + "Font: " + fonts.font + "\r" + "Size: " + fonts.size + "\r" + "Color: #" + fonts.color.rgb.hexValue + "\r" + ((i === 0 && fonts.leading !== '') ? "Leading: " + fonts.leading + "\r" : '') + ((i === 0) ? "Justify: " + theJustification + "\r" : ''),
          "Font Info #" + (i + 1),
          [myLayer.bounds[0] + i * 150, myLayer.bounds[3] + 15]
        );
      }
    }
  }
};

// Add's a text layer which contains the content of the read string above
function addTextLayer(theText, theName, thePosition) {
  var aTextLayer = app.activeDocument.artLayers.add();
  aTextLayer.kind = LayerKind.TEXT;
  aTextLayer.name = theName;
  var aTextLayerRef = aTextLayer.textItem;
  aTextLayerRef.kind = TextType.POINTTEXT;
  aTextLayerRef.size = 10;
  aTextLayerRef.font = "Arial-BoldMT";
  aTextLayerRef.color = app.foregroundColor;
  aTextLayerRef.justification = Justification.LEFT;
  aTextLayerRef.position = thePosition;
  aTextLayer.blendMode = BlendMode.NORMAL;
  aTextLayer.opacity = 100;
  aTextLayer.fillOpacity = 100;
  aTextLayerRef.useAutoLeading = true;
  aTextLayerRef.leading = 0;
  aTextLayerRef.horizontalScale = 100;
  aTextLayerRef.verticalScale = 100;
  aTextLayerRef.contents = theText;
  aTextLayerRef.antiAliasMethod = AntiAlias.SHARP;


  //Move the newly created font layers into the existing "Pixel Spec" folder
  var doc = app.activeDocument;
  var srcLayer = doc.activeLayer;

  try {
    moveLayerToSet(doc, srcLayer, 'Pixel Specs');
  } catch (error) {
    false;
  }

  function moveLayerToSet(doc, srcLayer, toLayerName) {
    var destLayer = doc.layers[toLayerName];
    if (destLayer.layers) destLayer = destLayer.layers[0];
    srcLayer.move(destLayer, ElementPlacement.PLACEBEFORE);
  }



};

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) {

  function markReturnedContentText(text) {
    if (font_content_detection) {
      return font_content_detection_symbols[0] + text + font_content_detection_symbols[1] + "\r";
    } else {
      return '';
    }
  }

  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 = styleDesc.getUnitDoubleValue(stringIDToTypeID('size')) + " " + typeIDToCharID(styleDesc.getUnitDoubleType(stringIDToTypeID('size')));
      var aSize = new UnitValue(styleDesc.getUnitDoubleValue(stringIDToTypeID('size')), "px");

      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.replace(/^\s+/, '').replace(/\s+$/, '');
      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;

  }
};


Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

Undo States (Undo Once)

Post by pfaffenbichler »

Please refer to the documentation or ESTK’s Object Model Viewer for
Document.suspendHistory (historyString: string , javaScriptString: string )
mycort

Undo States (Undo Once)

Post by mycort »

Do i add this type of code to the top:
if (validateState()) {
app.activeDocument.suspendHistory();
};

Sorry, but my programming knowledge is really very little and don't know where to begin to add this properly....

Can you please just add the code here?
pfaffenbichler

Undo States (Undo Once)

Post by pfaffenbichler »

You need to wrap your operations in a function and use that in suspendHistory.
Code: Select all// 2012, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
myDocument.suspendHistory("this will show up in the history panel", "main(myDocument)");
};
//
function main () {
myDocument.artLayers.add();
myDocument.artLayers.add();
myDocument.artLayers.add();
};
mycort

Undo States (Undo Once)

Post by mycort »

Is this a placeholder code string?
"this will show up in the history panel", "main(myDocument)");



Like this?
Code: Select allif (app.documents.length > 0) {
var myDocument = app.activeDocument;
myDocument.suspendHistory("this will show up in the history panel", "main(myDocument)");
};
//
function main () {
function multiselectfontreader() {

  app.bringToFront();
  main();

  function main() {
    if (!documents.length) return;
    var selLayers = [];
    selLayers = getSelectedLayersIdx();
    for (var a in selLayers) {
      makeActiveByIndex(selLayers[a], false);
      //amend to suit.
      readTextLayer()
    }
  }

  function getSelectedLayersIdx() {
    var selectedLayers = new Array;
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref);
    if (desc.hasKey(stringIDToTypeID('targetLayers'))) {
      desc = desc.getList(stringIDToTypeID('targetLayers'));
      var c = desc.count
      var selectedLayers = new Array();
      for (var i = 0; i < c; i++) {
        try {
          activeDocument.backgroundLayer;
          selectedLayers.push(desc.getReference(i)
            .getIndex());
        } catch (e) {
          selectedLayers.push(desc.getReference(i)
            .getIndex() + 1);
        }
      }
    } else {
      var ref = new ActionReference();
      ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("ItmI"));
      ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
      try {
        activeDocument.backgroundLayer;
        selectedLayers.push(executeActionGet(ref)
          .getInteger(charIDToTypeID("ItmI")) - 1);
      } catch (e) {
        selectedLayers.push(executeActionGet(ref)
          .getInteger(charIDToTypeID("ItmI")));
      }
    }
    return selectedLayers;
  };

  function makeActiveByIndex(idx, visible) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), idx)
    desc.putReference(charIDToTypeID("null"), ref);
    desc.putBoolean(charIDToTypeID("MkVs"), visible);
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
  };
};

// Read one or more selected fonts
function readTextLayer() {
  if (app.documents.length > 0) {
    var myDoc = app.activeDocument;
    var myLayer = myDoc.activeLayer;
    if (myLayer.kind == LayerKind.TEXT) {
      var thisText = myLayer.textItem;
      // Renaming the Justifcation info from Justification.CENTER - > Center;
      try {
        var theJustification = String(thisText.justification)
          .replace("Justification.", "")
      } catch (e) {
        var theJustification = "LEFT"
      };

      theJustification = theJustification[0] + theJustification.slice(1, theJustification.length)
        .toLowerCase();
     
var fonts = getFonts(app.activeDocument.activeLayer);

      for (var i = 0; i < fonts.length; i++) {
        addTextLayer(
          ((fonts.length > 1) ? fonts.text + ((fonts.text.length) ? "\r" : '') : '') + "Font: " + fonts.font + "\r" + "Size: " + fonts.size + "\r" + "Color: #" + fonts.color.rgb.hexValue + "\r" + ((i === 0 && fonts.leading !== '') ? "Leading: " + fonts.leading + "\r" : '') + ((i === 0) ? "Justify: " + theJustification + "\r" : ''),
          "Font Info #" + (i + 1),
          [myLayer.bounds[0] + i * 150, myLayer.bounds[3] + 15]
        );
      }
    }
  }
};

// Add's a text layer which contains the content of the read string above
function addTextLayer(theText, theName, thePosition) {
  var aTextLayer = app.activeDocument.artLayers.add();
  aTextLayer.kind = LayerKind.TEXT;
  aTextLayer.name = theName;
  var aTextLayerRef = aTextLayer.textItem;
  aTextLayerRef.kind = TextType.POINTTEXT;
  aTextLayerRef.size = 10;
  aTextLayerRef.font = "Arial-BoldMT";
  aTextLayerRef.color = app.foregroundColor;
  aTextLayerRef.justification = Justification.LEFT;
  aTextLayerRef.position = thePosition;
  aTextLayer.blendMode = BlendMode.NORMAL;
  aTextLayer.opacity = 100;
  aTextLayer.fillOpacity = 100;
  aTextLayerRef.useAutoLeading = true;
  aTextLayerRef.leading = 0;
  aTextLayerRef.horizontalScale = 100;
  aTextLayerRef.verticalScale = 100;
  aTextLayerRef.contents = theText;
  aTextLayerRef.antiAliasMethod = AntiAlias.SHARP;


  //Move the newly created font layers into the existing "Pixel Spec" folder
  var doc = app.activeDocument;
  var srcLayer = doc.activeLayer;

  try {
    moveLayerToSet(doc, srcLayer, 'Pixel Specs');
  } catch (error) {
    false;
  }

  function moveLayerToSet(doc, srcLayer, toLayerName) {
    var destLayer = doc.layers[toLayerName];
    if (destLayer.layers) destLayer = destLayer.layers[0];
    srcLayer.move(destLayer, ElementPlacement.PLACEBEFORE);
  }



};

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) {

  function markReturnedContentText(text) {
    if (font_content_detection) {
      return font_content_detection_symbols[0] + text + font_content_detection_symbols[1] + "\r";
    } else {
      return '';
    }
  }

  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 = styleDesc.getUnitDoubleValue(stringIDToTypeID('size')) + " " + typeIDToCharID(styleDesc.getUnitDoubleType(stringIDToTypeID('size')));
      var aSize = new UnitValue(styleDesc.getUnitDoubleValue(stringIDToTypeID('size')), "px");

      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.replace(/^\s+/, '').replace(/\s+$/, '');
      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;

  }
};

mycort

Undo States (Undo Once)

Post by mycort »

can someone please help and advise....tried this out and didn't work right.
Mike Hale

Undo States (Undo Once)

Post by Mike Hale »

I see several problems with what you posted. One is that the top level function main does not have a closing bracket. It also doesn't accept any arguments. And it doesn't do anything, it just contains a group of functions.

Try removing that top level main() and use this.
Code: Select allmyDocument.suspendHistory("this will show up in the history panel", "multiselectfontreader()");
mycort

Undo States (Undo Once)

Post by mycort »

I tried doing this......seems to undo the last font info layer but leaves the other ones alone.....

Like this?
Code: Select all  if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
      myDocument.suspendHistory("this will show up in the history panel", "multiselectfontreader()");
    };
    //
 function multiselectfontreader() {

      app.bringToFront();
      main();

      function main() {
        if (!documents.length) return;
        var selLayers = [];
        selLayers = getSelectedLayersIdx();
        for (var a in selLayers) {
          makeActiveByIndex(selLayers[a], false);
          //amend to suit.
          readTextLayer()
        }
      }

      function getSelectedLayersIdx() {
        var selectedLayers = new Array;
        var ref = new ActionReference();
        ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
        var desc = executeActionGet(ref);
        if (desc.hasKey(stringIDToTypeID('targetLayers'))) {
          desc = desc.getList(stringIDToTypeID('targetLayers'));
          var c = desc.count
          var selectedLayers = new Array();
          for (var i = 0; i < c; i++) {
            try {
              activeDocument.backgroundLayer;
              selectedLayers.push(desc.getReference(i)
                .getIndex());
            } catch (e) {
              selectedLayers.push(desc.getReference(i)
                .getIndex() + 1);
            }
          }
        } else {
          var ref = new ActionReference();
          ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("ItmI"));
          ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
          try {
            activeDocument.backgroundLayer;
            selectedLayers.push(executeActionGet(ref)
              .getInteger(charIDToTypeID("ItmI")) - 1);
          } catch (e) {
            selectedLayers.push(executeActionGet(ref)
              .getInteger(charIDToTypeID("ItmI")));
          }
        }
        return selectedLayers;
      };

      function makeActiveByIndex(idx, visible) {
        var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putIndex(charIDToTypeID("Lyr "), idx)
        desc.putReference(charIDToTypeID("null"), ref);
        desc.putBoolean(charIDToTypeID("MkVs"), visible);
        executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
      };
    };

    // Read one or more selected fonts
    function readTextLayer() {
      if (app.documents.length > 0) {
        var myDoc = app.activeDocument;
        var myLayer = myDoc.activeLayer;
        if (myLayer.kind == LayerKind.TEXT) {
          var thisText = myLayer.textItem;
          // Renaming the Justifcation info from Justification.CENTER - > Center;
          try {
            var theJustification = String(thisText.justification)
              .replace("Justification.", "")
          } catch (e) {
            var theJustification = "LEFT"
          };

          theJustification = theJustification[0] + theJustification.slice(1, theJustification.length)
            .toLowerCase();
         
    var fonts = getFonts(app.activeDocument.activeLayer);

          for (var i = 0; i < fonts.length; i++) {
            addTextLayer(
              ((fonts.length > 1) ? fonts.text + ((fonts.text.length) ? "\r" : '') : '') + "Font: " + fonts.font + "\r" + "Size: " + fonts.size + "\r" + "Color: #" + fonts.color.rgb.hexValue + "\r" + ((i === 0 && fonts.leading !== '') ? "Leading: " + fonts.leading + "\r" : '') + ((i === 0) ? "Justify: " + theJustification + "\r" : ''),
              "Font Info #" + (i + 1),
              [myLayer.bounds[0] + i * 150, myLayer.bounds[3] + 15]
            );
          }
        }
      }
    };

    // Add's a text layer which contains the content of the read string above
    function addTextLayer(theText, theName, thePosition) {
      var aTextLayer = app.activeDocument.artLayers.add();
      aTextLayer.kind = LayerKind.TEXT;
      aTextLayer.name = theName;
      var aTextLayerRef = aTextLayer.textItem;
      aTextLayerRef.kind = TextType.POINTTEXT;
      aTextLayerRef.size = 10;
      aTextLayerRef.font = "Arial-BoldMT";
      aTextLayerRef.color = app.foregroundColor;
      aTextLayerRef.justification = Justification.LEFT;
      aTextLayerRef.position = thePosition;
      aTextLayer.blendMode = BlendMode.NORMAL;
      aTextLayer.opacity = 100;
      aTextLayer.fillOpacity = 100;
      aTextLayerRef.useAutoLeading = true;
      aTextLayerRef.leading = 0;
      aTextLayerRef.horizontalScale = 100;
      aTextLayerRef.verticalScale = 100;
      aTextLayerRef.contents = theText;
      aTextLayerRef.antiAliasMethod = AntiAlias.SHARP;


      //Move the newly created font layers into the existing "Pixel Spec" folder
      var doc = app.activeDocument;
      var srcLayer = doc.activeLayer;

      try {
        moveLayerToSet(doc, srcLayer, 'Pixel Specs');
      } catch (error) {
        false;
      }

      function moveLayerToSet(doc, srcLayer, toLayerName) {
        var destLayer = doc.layers[toLayerName];
        if (destLayer.layers) destLayer = destLayer.layers[0];
        srcLayer.move(destLayer, ElementPlacement.PLACEBEFORE);
      }



    };

    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) {

      function markReturnedContentText(text) {
        if (font_content_detection) {
          return font_content_detection_symbols[0] + text + font_content_detection_symbols[1] + "\r";
        } else {
          return '';
        }
      }

      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 = styleDesc.getUnitDoubleValue(stringIDToTypeID('size')) + " " + typeIDToCharID(styleDesc.getUnitDoubleType(stringIDToTypeID('size')));
          var aSize = new UnitValue(styleDesc.getUnitDoubleValue(stringIDToTypeID('size')), "px");

          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.replace(/^\s+/, '').replace(/\s+$/, '');
          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;

      }
    };


mycort

Undo States (Undo Once)

Post by mycort »

can someone verufy if my code structure is correct here? Not sure, but it doesn't seem to work how i want it.....it doesn't undo all text info layer creation.......
mycort

Undo States (Undo Once)

Post by mycort »

anyone...pretty please.......any help is appreciated:)