[FIXED] Das Umbenennen des Photoshop -Umbenennens ist nicht durch Ebenen in Ordnern iteriert

Ausgabe

Abgesehen von einem Aspekt funktioniert dieses Skript perfekt: Es durchläuft keine Ebenen, die in Ordnern verschachtelt sind. Ich habe versucht , gemäß dieserlayers Frage zu wechseln , aber dann funktioniert es nicht mehr mit Ordnernamen. Ich verwende Photoshop 2020 auf Mac Catalina.layerSets

// JavaScript Document
var doc = app.activeDocument;

// name indexed object
var layernames = {
 'Products':'Working',
 'Product':'Working',
 'Notes':'Note',
 'Background color':'Background Colour',
 'White background':'White Background'
};

// loop through all layers
for (var i = 0; i < doc.layers.length; i++)
{

 //Set up Variable to access layer name
 var currentLayer = app.activeDocument.layers;

 if (layernames[currentLayer.name]) {
     currentLayer.name = layernames[currentLayer.name];
 }
}

Lösung

Betrachten Sie die folgende Lösung, die eine rekursive Funktion verwendet, die aufgerufen renameLayerswird, um alle Schichtknoten in der Baumstruktur der Dokumentschichten zu durchlaufen.

Eine Ebeneneigenschaft typenamekann entweder auf ArtLayeroder gesetzt werden LayerSet. Im Wesentlichen, wenn eine Ebene typenameeine LayerSet(dh ein Ordner) ist, rufen wir die Funktion rekursiv erneut auf.


var doc = app.activeDocument;

/**
  * Rename layers in a Photoshop document.
  *
  * @param {Object} doc A reference to the document to change.
  * @param {Object} layerName Each property key name indicates the original layer
  * name, and its corresponding value indicates the new layer name.
  * @param {Object} options The configuration options.
  * @param {Boolean} [options.includeTextLayers=false] Whether to rename text layers.
  * @param {Boolean} [options.includeLayerSets=false] Whether to rename LayerSets.
  */
function renameLayers(doc, layerNames, options) {

  // Default options
  var opts = {
    includeTextLayers: false,
    includeLayerSets: false
  };

  // Overwrite properties in the default `opts` object by properties in the
  // user defined `options` object if they have the same key. Shallow copy only.
  if (typeof options === 'object') {
    for (var key in opts) {
      if (options.hasOwnProperty(key)) {
        opts[key] = options[key];
      }
    }
  }

  // Iterate each layer in the document.
  for (var i = 0, max = doc.layers.length; i < max; i++) {
    var currentLayer = doc.layers[i];

    if (currentLayer.typename === 'ArtLayer') {

      if (layerNames[currentLayer.name]
          && (opts.includeTextLayers || currentLayer.kind !== LayerKind.TEXT)) {
        currentLayer.name = layerNames[currentLayer.name];
      }

    } else { // The layers `typename` is a `LayerSet`

      if (layerNames[currentLayer.name] && opts.includeLayerSets) {
        currentLayer.name = layerNames[currentLayer.name];
      }
      renameLayers(currentLayer, layerNames, options); // Resursive call
    }
  }
}


// Demo Usage

var layerNames = {
  'Products': 'Working',
  'Product': 'Working',
  'Notes': 'Note',
  'Background color': 'Background Colour',
  'White background': 'White Background'
};

renameLayers(doc, layerNames);

Nutzungshinweise:

Wie Sie in der letzten Codezeile (oben) sehen können, rufen wir die renameLayersFunktion wie folgt auf:

renameLayers(doc, layerNames);

Here we pass in the doc variable, i.e. a reference to the document to change, and the layerNames object that defines the mappings for the original layer name and the new layer name.

Running the code shown above (as is) will rename any layer in accordance with the mappings specified in the layerNames object. However it currently does not rename any Text Layer(s) or LayerSet(s).

How can I also rename Text Layers and/or LayerSets?

The renameLayers function lists a third optional parameter, named options to allow a custom configuration to be defined.

The following three function invocations demonstrate how different configurations can be achieved by passing in the optional options argument:

  • Invoking the function as follows with includeLayerSets set to true renames LayerSets too:

    renameLayers(doc, layerNames, { includeLayerSets: true });
    
  • Aufruf der Funktion wie folgt mit includeTextLayersset to truebenennt auch Textebenen um:

    renameLayers(doc, layerNames, { includeTextLayers: true });
    
  • Aufruf der Funktion wie folgt mit includeLayerSetsund includeTextLayersset to truebenennt sowohl LayerSets als auch Text Layers um:

    renameLayers(doc, layerNames, {
      includeLayerSets: true,
      includeTextLayers: true
    });
    


Beantwortet von –
RobC


Antwort geprüft von –
Pedro (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like