我自己对 CS5 脚本编写很陌生,但我想我可以解释它是如何工作的。代码示例可能不是最有效的方法,但它确实有效。
一组层或单个层本身之间存在很大差异。
所有层和组均以 DOM 格式排序。要获取根权限,您可以使用全局实例app
获取活动文档:app.activeDocument
.
混乱的部分是单层和组有两个单独的数组。
要获取单层数组,请使用app.activeDocument.layers
and app.activeDocument.layerSets
对于团体。
要深入层次结构,请使用 layerSets 数组向下迭代。
例如,让我们假设以下层次结构:
-Border
+Icons
+Left
-Star
-Home
+Right
-Add
-Remove
Here Border
, Star
, Home
, Add
and Remove
都是单层,而Icons
, Left
and Right
是团体。
开启群组Left
我们需要迭代Icon
group:
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);
我希望这对我以外的其他人有用:)