如何使用自动识别水平和垂直框架重命名图层 [Photoshop 脚本]

问题描述 投票:0回答:2

我是 Photoshop 脚本新手。 我想加载一个 PSD 文件,其中有 2 张垂直照片和 3 张水平照片,我选择该图层。 现在我希望所有选定的图层都已重命名为带有 VFrame 的 Verticle 和带有 HFrame 的 Horizontal 并使用该名称保存的 PSD 2v3h_My Name_0001 如何才能做到这一点?有人可以分享 Photoshop 的 javascript 吗?

javascript typescript photoshop photoshop-script windows-scripting
2个回答
1
投票

这是您的第一个问题,欢迎来到 StackOverflow。有很多 Photoshop 脚本指南。 这个涵盖了基础知识。

您想要的脚本需要:

  • 循环各层(我们将省略组,这得到 复杂快)
  • 识别图层是水平还是垂直
  • 适当重命名图层
  • 使用新文档复制文档 根据水平层数命名
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF 

// call the source document
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
var vCount = 0;
var hCount = 0;


// main loop
for (var i = numOfLayers -1; i >= 0  ; i--) 
{
  // get a referenec to each layer as we loop over them
  var thisLayer = srcDoc.layers[i];

  // get the layer bounds of each layer as you go
  srcDoc.activeLayer = thisLayer;
  var lb = get_layer_bounds();

  // set a variable to for portrait or landscape
  var portrait = true;

  // if layer is wider than it is long,
  // it's landscape
  if (lb[0] > lb[1]) portrait = false;
  //alert(lb[0] + "," + lb[1] + "\n" + portrait + "\n" +thisLayer.name);

  // ignore the background layer
  // if there is one
  if (thisLayer.isBackgroundLayer == false)
  {
    if (portrait)
      {
        srcDoc.artLayers[i].name = "HFrame";
        // add one to the horizontal count
        hCount +=1
      }
    else
      {
        srcDoc.artLayers[i].name = "VFrame";
        // add one to the vertical count
        vCount+=1
      }
  }
}
var docName = srcDoc.name.substring(0, srcDoc.name.length -4);
var imgName = vCount + "v" + hCount + "h_" + docName + "_0001.psd";
duplicateIt(imgName);


// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL



// function GET LAYER BOUNDS ()
// ----------------------------------------------------------------
function get_layer_bounds()
{
  var x = parseFloat(app.activeDocument.activeLayer.bounds[0]);
  var y = parseFloat(app.activeDocument.activeLayer.bounds[1]);
  var x1 = parseFloat(app.activeDocument.activeLayer.bounds[2]);
  var y1 = parseFloat(app.activeDocument.activeLayer.bounds[3]);

  var selW = parseFloat(x1-x);
  var selH = parseFloat(y1-y);

  // return the results as an array
  return [selW, selH];
}


function duplicateIt(str)
{
  // duplicate image into new document
  if (arguments.length == 0) str = "temp";

  var id428 = charIDToTypeID( "Dplc" );
  var desc92 = new ActionDescriptor();
  var id429 = charIDToTypeID( "null" );
  var ref27 = new ActionReference();
  var id430 = charIDToTypeID( "Dcmn" );
  var id431 = charIDToTypeID( "Ordn" );
  var id432 = charIDToTypeID( "Frst" );
  ref27.putEnumerated( id430, id431, id432 );
  desc92.putReference( id429, ref27 );
  var id433 = charIDToTypeID( "Nm  " );
  desc92.putString( id433, str ); // name
  executeAction( id428, desc92, DialogModes.NO );
}

0
投票

如何仅水平和垂直重命名选定的图层,没有所有图层帮助先生 仅选定层

© www.soinside.com 2019 - 2024. All rights reserved.