愿所有的美好和期待都能如约而至

Photoshop重命名脚本不会遍历文件夹中的图层

发布时间:  来源:互联网  作者:匿名  标签:error Photoshop renaming script does not iterate through layers within folders e  热度:37.5℃

本文介绍了Photoshop重命名脚本不会遍历文件夹中的图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个脚本完美地工作在一个方面:它不会循环遍历嵌套在文件夹中的层。我已尝试根据this问题将layers更改为layerSets,但之后它停止处理文件夹名称。我正在Mac Catalina上使用Photoshop 2020。

// 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];
 }
}

推荐答案

考虑以下解决方案,该解决方案使用名为renameLayers的recursive function遍历文档层树结构中的所有层节点。

typename属性可以设置为ArtLayerLayerSet。基本上,当层typenameLayerSet(即文件夹)时,我们再次递归调用该函数。


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

用法说明:

正如您在(上面)最后一行代码中看到的,我们调用renameLayers函数如下:

renameLayers(doc, layerNames);

在这里,我们传入doc变量,即对要更改的文档的引用,以及定义原始层名称和新层名称的映射的layerNames对象。

运行上面显示的代码(按原样)将根据layerNames对象中指定的映射重命名任何层。但是,它当前不重命名任何文本层或LayerSet。

如何还重命名文本层和/或LayerSets?

renameLayers函数列出了名为options的第三个可选参数,以允许定义自定义配置。

以下三个函数调用演示了如何通过传入可选的options参数来实现不同的配置:

  • 调用该函数,includeLayerSets设置为true也重命名LayerSets:

    renameLayers(doc, layerNames, { includeLayerSets: true });
    
  • includeTextLayers设置为true的情况下调用如下函数也会重命名文本层:

    renameLayers(doc, layerNames, { includeTextLayers: true });
    
  • 调用该函数时,includeLayerSetsincludeTextLayers设置为true也会重命名LayerSets和Text层:

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

这篇关于Photoshop重命名脚本不会遍历文件夹中的图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

勇敢去编程!

勇敢的热爱编程,未来的你一定会大放异彩,未来的生活一定会因编程更好!

TOP