Photoshop Java脚本列表层

2022-09-04 00:00:00 json javascript photoshop

我想列出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

你有什么办法做到这一点吗? 提前感谢您的回答和帮助!


解决方案

我建议你阅读有关操作管理器代码的内容,在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) {;}

}

相关文章