我想列出json文档中的图层。在我的代码之后:
#include json2.js
var doc = app.activeDocument;
var allLayers = [];
var allLayers = collectAllLayers(doc, allLayers);
function collectAllLayers (doc, allLayers){
for (var m = 0; m < doc.layers.length; m++){
var theLayer = doc.layers[m];
if (theLayer.typename === "ArtLayer"){
allLayers.push(theLayer);
}else{
collectAllLayers(theLayer, allLayers);
}
}
return allLayers;
}
var json = JSON.stringify(allLayers);
alert(json);
我遇到错误General Photoshop error occurred.This functionality may not be avaliable in this version of photoshop
我想列出群组和图层。像这样:
Group1
> Layer 1
> Layer 2
> Group 2
> > Layer 3
> > Layer 4
> > Group 3
> > > Layer 5
> > > Layer 6
> Layer 3
您有什么想法要这样做吗? 感谢您的答复和事先的帮助!
答案 0 :(得分:0)
我建议您阅读有关Action Manager代码的信息,在Photoshop中使用DOM通常比较缓慢且受限制。例如,此非递归函数将遍历所有图层并将图层DOM对象添加到您的allLayers
:
var doc = app.activeDocument;
var allLayers = [];
traverseLayersAMFlat(doc);
alert(allLayers);
//non-recursive action manager traversal function
function traverseLayersAMFlat(doc)
{
function _selectLayerById(ID) //select just this layer
{
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID('null'), ref);
desc.putBoolean(charIDToTypeID('MkVs'), false);
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
}//_selectLayerById
//how many layers are there in this document?
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL'));
//traverse the list backwards (does parents first)
for (var i = count; i >= 1; i--)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), i);
var desc = executeActionGet(ref); //access layer index #i
var layerID = desc.getInteger(stringIDToTypeID('layerID')); //ID for selecting by ID #
var layerSection = typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('layerSection'))); //layerSectionStart, layerSectionContent, laterSectionEnd
if (layerSection != 'layerSectionEnd')
{
_selectLayerById(layerID);
allLayers.push(app.activeDocument.activeLayer)
}
}//for i-- countdown
try
{ //if there is a magic background layer, process it, too
app.activeDocument.activeLayer = app.activeDocument.backgroundLayer;
allLayers.push(app.activeDocument.backgroundLayer)
} catch (e) {;}
}