Photoshop重命名脚本不会遍历文件夹中的图层 [英] Photoshop renaming script does not iterate through layers within folders

查看:0
本文介绍了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];
 }
}

推荐答案

考虑以下解决方案,该解决方案使用名为renameLayersrecursive 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重命名脚本不会遍历文件夹中的图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆