我当前的代码很糟糕,这是周五下午的特别事件:
if(layer.parent == doc && isLayerSet) {
//do something in this case fileNew.write('foo');
}
else if(layer.parent.parent == doc && isLayerSet) {
//do something in this case fileNew.write('foo');
}
else if(layer.parent.parent.parent == doc && isLayerSet) {
//do something in this case fileNew.write('foo');
}
else if(layer.parent.parent.parent.parent == doc && isLayerSet) {
//do something in this case fileNew.write('foo');
}
else if(layer.parent.parent.parent.parent.parent == doc && isLayerSet) {
//do something in this case fileNew.write('foo');
}
else if(layer.parent.parent.parent.parent.parent.parent == doc && isLayerSet)
{
//do something in this case fileNew.write('foo');
}
我这样做是为了验证脚本的其余部分是否有效(它确实有效)。
层的游戏目标我想获取它的顶级父级的名称(即文档的第一个子级)。所有 block 都应该做同样的事情。
我知道有一种更简单的方法可以做到这一点,但是我对此仍然很陌生,并且我不确定 javascript 和 Photoshop 的一些细微差别,因此我们将不胜感激任何帮助!
最佳答案
你可以做一些递归的事情。它可以替换所有“if-else if”语句。
(function getTopLayer(element) {
if (element.parent == doc && isLayerSet) {
//do something in this case fileNew.write('foo');
} else {
getTopLayer(element.parent);
}
})(layer);
但请注意,这将无限运行,直到第一个 if 为 true。
关于javascript - 使用 Javascript 在 Photoshop 中查找顶级图层集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51704261/