2012-02-22 107 views
3

我在Photoshop中有6个组,每个组中包含多个图层。我正在打开/关闭每个组中的图层,以创建图像的每种可能的组合。在Photoshop脚本中打开和关闭多个图层

有人能指出我正确的方向吗?

我从来没有在Photoshop中脚本,但试图找出我自己的。

+1

看一看这个答案更加有用:HTTP:/ /stackoverflow.com/a/8544923/327466 – KatieK 2012-02-23 22:52:49

回答

3

我对CS5脚本编程很陌生,但我想我可以解释它是如何工作的。代码示例可能不是最有效的方法,但它的确有用。

一组图层或单个图层本身之间存在很大差异。 所有图层和组按DOM格式排序。要获取根目录,可以使用全局实例app获取活动文档:app.activeDocument

凌乱的部分是,有两个单独的数组单层和组。 要获得单层阵列,请使用组app.activeDocument.layersapp.activeDocument.layerSets

要更深层次地使用层次结构,请使用layerSets数组迭代。

例如,让我们假设以下hieralcy:

-Border 
+Icons 
    +Left 
     -Star 
     -Home 
    +Right 
     -Add 
     -Remove 

这里BorderStarHomeAddRemove都是单层而IconsLeftRight是组。

要打开组Left我们需要遍历下来Icon组:

Icons = app.activeDocument.layerSets.getByName("Icons"); 
Left = Icons.layerSets.getByName("Left"); 
Left.visible = true; 

如果显示CS5图层/组用鼠标点击,所有的家长群体将自动显示太大。通过编写脚本并非如此,您还必须启用所有父母。

Icons = app.activeDocument.layerSets.getByName("Icons"); 
Icons.visible = true; 
Left = Icons.layerSets.getByName("Left"); 
Left.visible = true; 

要显示边框图层,您需要改为使用图层数组。

app.activeDocument.layers.getByName("Border").visible = true; 

如果您想要显示添加图层,则同样的情况适用。

Icons = app.activeDocument.layerSets.getByName("Icons"); 
Icons.visible = true; 
Right = Icons.layerSets.getByName("Right"); 
Right.visible = true; 
AddLayer = Right.layers.getByName("Add"); 
AddLayer.visible = true; 

如果您有很多组和图层,这可能有点麻烦。我创建了一个遵循提供的路径来获取最终对象的函数。它将自行确定它是一个图层还是一个组。

//****************************************** 
// GET BY PATH 
// Author: Max Kielland 
// 
// Gets the LayerSet or Layer at the path's end. 
// Example path "Icons/left" will return the LayerSet object "Left" 
// while "Icons/left/Star" will return the Layer object "Star" 
// If fSetPath is true, all the parents will be visible as well. 

function GetByPath(fPath,fSetPath) { 

    var lGroup = null; 
    var lPathArray = new Array(); 

    lPathArray = fPath.split('/'); 
    try { 
    lGroup = app.activeDocument.layers.getByName(lPathArray[0]); 
    } catch (err) { 
    lGroup = app.activeDocument.layerSets.getByName(lPathArray[0]); 
    } 

    if (fSetPath) 
    lGroup.visible = true; 

    for (n=1; n<lPathArray.length; n++) { 
    try { 
     lGroup = lGroup.layerSets.getByName(lPathArray[n]); 
    } catch(err) { 
     lGroup = lGroup.layers.getByName(lPathArray[n]); 
    } 
    if (fSetPath == true) 
     lGroup.visible = true; 
    } 

    return lGroup; 
} 

...以及通过其路径简单地设置或清除组或层的功能之一。

//****************************************** 
// SET STATUS 
// Author: Max Kielland 
// 
// Sets the Group or Layer's visible property 
// at the end of the path to fStatus. 

function SetStatus(fPath, fStatus) { 

    Obj = GetByPath(fPath,false); 
    Obj.visible = fStatus; 
} 

..最后我写了这个函数来隐藏用户指定的根的所有组和/或图层。

/****************************************** 
// CLEAR GROUP 
// Author: Max Kielland 
// 
// Clears the visible property in a single 
// group/layer with the option to clear all 
// its children as well (fRecurs = true). 
// fLayerSet can be a layerSet object or a 
// String path. 

function ClearGroup(fLayerSet,fRecurs) { 

    var n; 
    var TargetGroup; 

    // Get LayerSet Object if reference is a string. 
    if (typeof fLayerSet == "string") 
    TargetGroup = GetByPath(fLayerSet); 
    else 
    TargetGroup = fLayerSet; 

    // Iterate through all LayerSets 
    for (n=0; n<TargetGroup.layerSets.length; n++) { 
    if (fRecurs == true) 
     ClearGroup(TargetGroup.layerSets[n],true); 
    else 
    TargetGroup.layerSets[n].visible = false; 
    } 

    // Iterate through all layers 
    for (n=0; n<TargetGroup.layers.length; n++) { 
    TargetGroup.layers[n].visible = false; 
    } 

    // Clear self 
    TargetGroup.visible = false; 
} 

下面是如何使用功能

// Hide group "Icon" and its children 
ClearGroup("Icons",true); 

//Show the layer "Home" 
GetByPath("Icons/Left/Home",true); 

// To just get the object "Right" 
var MyGroup = GetByPath("Icons/Right"); 

// Save the current document as a PNG file 
app.activeDocument.saveAs(File("Scripted Document.png"),PNGSaveOptions); 

一个例子,我希望这是对别人不只是我:)