尝试提取 Photoshop 图层坐标和尺寸

Trying to extract Photoshop layer co-ordinates and dimensions

示例 Photoshop 文件:

[Layer 1]
[Group 1]
   [Layer 2]
   [Layer 3]
[Layer 4 (Hidden)]
[Layer 5]

所需输出冒号分隔的文本文件):

Layer 1:x:y:w:h
Group 1:Layer 2:x:y:w:h
Group 1:Layer 3:x:y:w:h
Layer 5:x:y:w:h

如您所见,我正在尝试将每一层的名称及其坐标和尺寸写入冒号分隔的文本文件。忽略任何隐藏层并将组名称附加到组内的任何层。 我可以使用此脚本获取每一层的坐标和尺寸:

// Enables double-click launching from the Mac Finder or Windows Explorer
#target photoshop

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var layerNum = app.activeDocument.artLayers.length;

// Define variable for the active layer in the active document
var layerRef = app.activeDocument.activeLayer;

// Define varibles for x and y of layers
var x = layerRef.bounds[0].value;
var y = layerRef.bounds[1].value;
var w = layerRef.bounds[2].value;
var h = layerRef.bounds[3].value;
var coords = "";

// Loop to iterate through all layers
function recurseLayers(currLayers) {
  for ( var i = 0; i < currLayers.layers.length; i++ ) {
    layerRef = currLayers.layers[i];
    if (!layerRef.visibility) {
        continue;
    }       
    x = layerRef.bounds[0].value;
    y = layerRef.bounds[1].value;
    w = layerRef.bounds[2].value - x;
    h = layerRef.bounds[3].value - y;
    coords += layerRef.name + ":" + x + ":" + y + ":" + w + ":" + h + "\n";

    //test if it's a layer set
    if ( isLayerSet(currLayers.layers[i]) ) {
      recurseLayers(currLayers.layers[i]);
    }
  }
}

//a test for a layer set
function isLayerSet(layer) {
  try {
    if ( layer.layers.length > 0 ) {
      return true;
    }
  }

  catch(err) {
    return false;
  }
}

// Ask the user for the folder to export to
var FPath = Folder.selectDialog("Save exported coordinates to");

// Detect line feed type
if ( $.os.search(/windows/i) !== -1 ) {
  fileLineFeed = "Windows";
}
else {
  fileLineFeed = "Macintosh";
}

// Export to txt file
function writeFile(info) {
  try {
    var f = new File(FPath + "/" + docName + ".txt");
    f.remove();
    f.open('a');
    f.lineFeed = fileLineFeed;
    f.write(info);
    f.close();
  }
  catch(e){}
}

// Run the functions
recurseLayers(docRef);
preferences.rulerUnits = defaultRulerUnits; // Set preferences back to user 's defaults
writeFile(coords);

// Show results
if ( FPath == null ) {
  alert("Export aborted", "Canceled");
}
else {
  alert("Exported " + layerNum + " layer's coordinates to " + FPath + "/" + docName + ".txt " + "using " + fileLineFeed + " line feeds.", "Success!");
}

以上脚本最初由 Chris DeLuca 在本网站上提供,我稍作修改以提供尺寸。 但是,它列出了所有图层,无论它们是否隐藏并且不关心组(图层集 - 对吗?)

为了忽略隐藏层,我将递归函数更改为:

// Loop to iterate through all layers
function recurseLayers(currLayers) {
  for ( var i = 0; i < currLayers.layers.length; i++ ) {
    layerRef = currLayers.layers[i];
    if (!layerRef.visibility) {
        continue;
    }       
    x = layerRef.bounds[0].value;
    y = layerRef.bounds[1].value;
    w = layerRef.bounds[2].value - x;
    h = layerRef.bounds[3].value - y;
    coords += layerRef.name + "," + x + "," + y + "," + w + "," + h + "\n";

    //test if it's a layer set
    if ( isLayerSet(currLayers.layers[i]) ) {
      recurseLayers(currLayers.layers[i]);
    }
  }
}

认为它会忽略任何隐藏层。不幸的是,这会产生一个零字节文件。

因此,暂时忽略该问题并尝试分组问题。 我不想列出一个组,但我确实希望将组名称附加到组内任何图层的开头。我试过这个:

    // Loop to iterate through all layers
function recurseLayers(currLayers) {
  for ( var i = 0; i < currLayers.layers.length; i++ ) {
    layerRef = currLayers.layers[i];
    x = layerRef.bounds[0].value;
    y = layerRef.bounds[1].value;
    w = layerRef.bounds[2].value - x;
    h = layerRef.bounds[3].value - y;
    coords += group + ":" + layerRef.name + ":" + x + :," + y + ":" + w + ":" + h + "\n";
    group = "";

    //test if it's a layer set
    if ( isLayerSet(currLayers.layers[i]) ) {
        group = currLayers.layers[i]
        recurseLayers(currLayers.layers[i]);
    }
  }
}

但这只会将组名附加到其中的第一层。确实可以理解,但我不知道如何获得每一层的父组。

对于其中一个或两个问题的任何帮助,我们将不胜感激。 谢谢

我已经修改了几个小时,并设法回答了我自己的问题。 这有点老套,我相信还有更优雅的解决方案,但它确实有效!

这里是,如果有人需要的话:

#target photoshop

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var layerNum = app.activeDocument.artLayers.length;

// Define variable for the active layer in the active document
var layerRef = app.activeDocument.activeLayer;

// Define varibles for x and y of layers
var x = layerRef.bounds[0].value;
var y = layerRef.bounds[1].value;
var w = layerRef.bounds[2].value;
var h = layerRef.bounds[3].value;
var coords = "";
var group = "";
var layersInGroup = 0;
var thisGroupsLayer = 0;
var inGroup = false;

// Loop to iterate through all layers
function recurseLayers(currLayers) {
  for ( var i = 0; i < currLayers.layers.length; i++ ) {
    layerRef = currLayers.layers[i];
    if (!layerRef.visible) {
        continue;
    }

    if (isLayerSet(currLayers.layers[i]) == 0){
        x = layerRef.bounds[0].value;
        y = layerRef.bounds[1].value;
        w = layerRef.bounds[2].value - x;
        h = layerRef.bounds[3].value - y;
        coords += group + layerRef.name + ":" + x + ":" + y + ":" + w + ":" + h + "\n";
    }

    if (inGroup && thisGroupsLayer == layersInGroup){
        inGroup = false;
        group = "";
        thisGroupsLayer = 0;
    }

    if (inGroup && thisGroupsLayer != layersInGroup){
        thisGroupsLayer++;  
    }

    if (!inGroup){
        layersInGroup = isLayerSet(currLayers.layers[i]);
        if ( layersInGroup ) {
            var groupname = currLayers.layers[i].toString();
            group = groupname.substring(0, groupname.length-1);
            group = group.slice(10, group.length)+":";
            inGroup = true;
            thisGroupsLayer++;
            recurseLayers(currLayers.layers[i]);
        }
    }
  }
}

//a test for a layer set
function isLayerSet(layer) {
  try {
    if ( layer.layers.length > 0 ) {
      return layer.layers.length;
    }
  }

  catch(err) {
    return false;
  }
}

// Ask the user for the folder to export to
var FPath = Folder.selectDialog("Save exported coordinates to");

// Detect line feed type
if ( $.os.search(/windows/i) !== -1 ) {
  fileLineFeed = "Windows";
}
else {
  fileLineFeed = "Macintosh";
}

// Export to txt file
var filename = docName.substring(0, docName.length - 4) + " subimages.txt";
function writeFile(info) {
  try {
    var f = new File(FPath + "/" + filename);
    f.remove();
    f.open('a');
    f.lineFeed = fileLineFeed;
    f.write(info);
    f.close();
  }
  catch(e){}
}

// Run the functions
recurseLayers(docRef);
preferences.rulerUnits = defaultRulerUnits; // Set preferences back to user 's defaults
writeFile(coords);

// Show results
if ( FPath == null ) {
  alert("Export aborted", "Canceled");
}
else {
  alert("Exported " + layerNum + " layer's coordinates to " + FPath + "/" + filename + " using " + fileLineFeed + " line feeds.", "Success!");
}