在Javascript中遍历Photoshop层 [英] Looping through Photoshop layers in Javascript

查看:256
本文介绍了在Javascript中遍历Photoshop层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Photoshop脚本,该脚本将显示给定名称的所有层.我需要遍历所有可能的嵌套图层集,并使用以下代码:

I'm trying to write a Photoshop script that will show all layers of a given name. I need to loop through all the possible nested layer sets and am using the following code:

function showBounds(layerNode)
{
    for(var layer in layerNode.artLayers)
    {
        if (layer.name == "@bounds")
        {
            layer.visible = 1;
        }
    }

    showBounds(layerNode.layerSets);
}

showBounds(app.activeDocument.doc.layerSets);

但是当我运行它时,出现以下错误:

But when I run it, I get the following error:

Error 1302: No such element
Line: 5
->      for(var layer in layerNode.artLayers)

artLayers应该是LayerSets的属性,所以我很困惑.

artLayers should be a property of LayerSets, so I'm confused.

这也是我第一次尝试编写PS脚本(并使用javascript),因此可能有一些基本概念没有得到.

This is also my first attempt at scripting PS (and using javascript), so there might be some fundamental concept I am not getting.

推荐答案

我认为您需要更多类似的东西:

I think you need something more like:

function showBounds(layerNode) {    
    for (var i=0; i<layerNode.length; i++) {

        showBounds(layerNode[i].layerSets);

        for(var layerIndex=0; layerIndex < layerNode[i].artLayers.length; layerIndex++) {
            var layer=layerNode[i].artLayers[layerIndex];
            if (layer.name == "@bounds") {
                layer.visible = 1;
            }
        }
    }
}

showBounds(app.activeDocument.layerSets);

此外,语法中的JavaScript不适用于您认为的方式.它不像一个foreach循环.它遍历对象的属性名称.

Also, javascripts for...in syntax doesn't work the way you think it does. It's not like a foreach loop. It loops over the property names of an object.

这篇关于在Javascript中遍历Photoshop层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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