简体   繁体   中英

Script to copy Layer Names to text box in Photoshop

I'm trying to create a script out of 2 working scripts

Goal:

We need a script that will go through all the layers in the current document, find each layer marked in certain color (Red for example), copy the names of only the layers marked in red, then put all names in a text layer one after another (attached example image).

在此处输入图片说明

Resources:

I found 2 scripts that each do half of what we need, so how to we put them together?

1) "Select by red" goes through the document and finds how many layers marked in "red" are in the document:

#target photoshop

if (app.documents.length > 0) {

// the file;

var myDocument = app.activeDocument;

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// process the layers;

var theLayers = new Array;

var theOthers = new Array;

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), m);

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

// if not layer group collect values;

if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true) {

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

var visible = layerDesc.getBoolean(stringIDToTypeID("visible"));

var theColor = layerDesc.getEnumerationValue(stringIDToTypeID("color"));

if (typeIDToStringID(theColor) == "red") {theLayers.push([theName, theID])}

else {theOthers.push([theName, theID])}

};

}

catch (e) {};

};

// if layers are red;

if (theLayers.length > 0) {alert ("there are " + theLayers.length + " Red layers")}

else {alert ("no red layers")}

};

2) "Text box from layer name" takes the name of the currently selected layer, and pastes it into a new text layer called "Comp".

if (app.documents.length > 0) mainScript();    



function mainScript() {  

    try{  

        var myLayerName = activeDocument.activeLayer.name;          

        var myLayerText = activeDocument.artLayers.add();  

        myLayerText.name = "Comp";  

        myLayerText.kind = LayerKind.TEXT; 

        var textProperty = myLayerText.textItem;

        textProperty.size = 10;   

        textProperty.font = "Arial";

        myLayerText.textItem.contents = myLayerName;  

    }catch (errStr){  

        alert(errStr);  

    }  

}  

Plan:

From my understanding, we need to start with a loop that's as long as our document size ie total number of layers for (var i = 0; i < doc.layers.length; i++) .

Then layer by layer the script will check for color ID if (typeIDToStringID(theColor) == "red") . When it finds layer marked in red, it copies the layer name - then either stores it in array (to output later all at once), or creates a new text box and pastes the layer name myLayerText.textItem.contents = myLayerName .

Then for each time it finds another layer marked in red , it copies the layer name, and pastes it in the same text box just a line above/below previous layer name.

Any help is much appreciated!

The only thing you needed to do was to join the names of your theLayers array using the line-break symbol \\r :

if (app.documents.length > 0)
{

  // the file;
  var myDocument = app.activeDocument;

  // get number of layers;
  var ref = new ActionReference();
  ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

  var applicationDesc = executeActionGet(ref);
  var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

  // process the layers;
  var theLayers = new Array;

  for (var m = 0; m <= theNumber; m++)
  {
    try
    {
      var ref = new ActionReference();
      ref.putIndex(charIDToTypeID("Lyr "), m);
      var layerDesc = executeActionGet(ref);
      var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
      var isBackground = layerDesc.getBoolean(stringIDToTypeID("background")); // if not layer group collect values;  if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true)
      {
        var theName = layerDesc.getString(stringIDToTypeID('name'));
        var theColor = layerDesc.getEnumerationValue(stringIDToTypeID("color"));
        if (typeIDToStringID(theColor) == "red")
        {
          theLayers.push(theName); // we only need names here
        }
      };
    }
    catch (e)
    {};
  };

  // got our red layers in theLayers

  var myLayerText = activeDocument.artLayers.add();
  myLayerText.name = "Result";
  myLayerText.kind = LayerKind.TEXT;
  var textProperty = myLayerText.textItem;
  textProperty.size = 10;
  textProperty.font = "Arial";
  myLayerText.textItem.contents = theLayers.join('\r'); // joining layers with a line-break: this is going to be textItem text

};

Here's the result:

在此处输入图片说明

I'd suggest you to take a course on JS on any learning website (code academy, etc): this won't take you more than an hour or two but you'll get the basic concepts: this will make your life much easier in terms of dealing with questions like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM