PS 循环遍历层的js脚本,使一层一层可见并在每次之间保存

PS js script to cycle trough layers, make one by one visible and save each time between

希望标题听起来不会令人困惑。 我想用脚本做什么:

  1. 在工作文件中打开智能 object 图层。
  2. 获取第一层名称,使第一层可见,智能更新object。
  3. 从主文档的组(颜色)中获取名称和活动层。
  4. 2.Name + 3.Name 并另存为 .png.

请在此处查看我的图表和简短的截屏视频:http://recordit.co/2QnBK0ZV2M

Workflow Diagram

感谢您的帮助!

#target photoshop


//This stores the currently active document to the variable mockupDoc
var mockupDoc = app.activeDocument;
 
//This stores Front.psd file's name so Photoshop can look for the active layers in the correct document.
var designDoc = app.documents.getByName("Front.psd");
 
//Gets the name of tha active layer of Front.psd
var designlayer = designDoc.activeLayer;  

// getting the name and location;
var docName = mockupDoc.name;  
if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}  
else {var basename = docName};  

// getting the location, if unsaved save to desktop;  
try {var docPath = mockupDoc.path}  
catch (e) {var docPath = "~/Desktop"}; 

// jpg options, But would love to save trough my Tiny png/jpg PS plugin instead PS native 
var jpegOptions = new JPEGSaveOptions();  
jpegOptions.quality = 9;  
jpegOptions.embedColorProfile = true;  
jpegOptions.matte = MatteType.NONE;  

// Saves the Mockup File containing the Name of the active layer in Front.psd
mockupDoc.saveAs((new File(docPath+'/'+basename + ' ' + designDoc.activeLayer.name +'.jpg')),jpegOptions,true);      

在上面添加了我当前的代码。我正在努力完成以下任务:

  1. 我如何检查 mockupDoc 文档的 layerset/group "Colors" 中哪个图层处于活动状态并将其存储为变量以在保存时将其用作名称?

    1. 我需要脚本从上到下遍历 Front.psd 的每一层,例如:从顶层开始,仅使该层可见 -> 保存此文档 -> return 到 mockup.psd -> 将此文件保存为 jpg 和 png -> return 到 Front.psd 并到达下一层重复这些步骤(visible/save/previous doc/save png jpg/ 等等)?脚本如何知道最后一层何时被处理并停止脚本?

    2. 现在脚本将文件保存在 mockup.psd 的根文件夹中。是否可以按颜色(即红色、黄色..)在其中放置文件夹,并检查我要保存的文件是否包含我之前存储的变量 "colors",如果变量为黄色,则保存到黄色文件夹?

希望这听起来不会很奇怪,我的英语很烂 ;)

感谢大家的热心帮助。

欢迎来到 Stack Overflow。通常我们会要求查看您编写的代码 - 即使它不起作用,也只需添加注释来解释您正在尝试做什么。问问题的时候,断码比 none 好太多了。但也要记住,SO 不是脚本编写服务。

在图层上循环是直接的——暂时避免分组——那更高级。环顾四周,您应该会找到保存 png 的代码。

这将帮助您入门。

 // Call the open document the source document
 // It's easier than having to type app.activeDocument each time :)
var srcDoc = app.activeDocument;

// create a variable that holds the number of layers in the psd
var numOfLayers = srcDoc.layers.length;

// temp string to hold all layer names
var str = ""

// loop over each layer - from the background to the top
for (var i = numOfLayers -1; i >= 0  ; i--)
// for the top to the bottom use: for (var i = 0; i < numOfLayers; i++)
{
    // get the name of each layer
    var layerName = srcDoc.layers[i].name;

    // add the layer name to a string
    str += layerName + "\n";

    // set the visiblity of each layer to true (on)
    srcDoc.layers[i].visible = true;
}

// display the layer names
alert(str);



function save_as_png(myFilePath)
{
    var f = myFilePath + ".png";

    // save out the image
    var pngFile = new File(f);
    pngSaveOptions = new PNGSaveOptions();
    pngSaveOptions.embedColorProfile = true;
    pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 1;

    activeDocument.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE);
}

谢谢@神秘嘉宾先生

我同时解决了大部分任务并在下面添加了我的代码。也许其他一些用户会发现它的某些部分有帮助或有更好的解决方案。

//================================= OPTIONS ================================= 

// jpg options.
  var jpegOptions = new JPEGSaveOptions();  
  jpegOptions.quality = 9;  
  jpegOptions.embedColorProfile = true;  
  jpegOptions.matte = MatteType.NONE;  

// PNG options.
    pngOptions = new PNGSaveOptions()
    pngOptions.compression = 0
    pngOptions.interlaced = false


//================================= VARIABLES ================================= 


// Variables for the "Paste in Place" function
  cTID = function(s) { return app.charIDToTypeID(s); };
  sTID = function(s) { return app.stringIDToTypeID(s); };

// Checks which one is the Mockup.psd file (as my names differ but allways contain "Mockup")
  var docs = app.documents;
  for(var i = docs.length - 1; i >= 0; i--){
    if(docs[i].name.indexOf('Mockup') > 0){
      var mockupDoc = docs[i];
    }
  }


// Getting the name and location of Mockup.psd and remove "Mockup" from the name/string.
  var mockupDocName = mockupDoc.name.replace(/Mockup /i,'');  
  if (mockupDocName.indexOf(".") != -1) {var basename = mockupDocName.match(/(.*)\.[^\.]+$/)[1]}  
  else {var basename = mockupDocName};  

  
// Getting the location of Mockup.psd;  
  var mockupDocPath = mockupDoc.path


// Setting variable for layerset "GTO Background" 
  var gtoBG = mockupDoc.layerSets.getByName("GTO Background");


// This stores Front.psd file's name so Photoshop can look for the active layers in the correct document.
  var designDoc = app.documents.getByName("Designs Front.psd");
  var currentLayer = designDoc.activeLayer;


// create a variable that holds the number of layers in the psd
var numOfLayers = designDoc.layers.length;

// temp string to hold all layer names
//var str = ""

    // get the name of each layer
    //var layerName = designDoc.layers[i].name;

    // add the layer name to a string
    //str += layerName + "\n";


function resetDesignDoc (){
// Sets "FRONT.psd" aka designDoc as active document.
  app.activeDocument = designDoc;

// loop over each layer - from the background to the top
for (var i = numOfLayers -1; i >= 0  ; i--)
// for the top to the bottom use: for (var i = 0; i < numOfLayers; i++)
{
    designDoc.layers[i].visible = false; // Hide all Layers
    designDoc.activeLayer = designDoc.layers[0]; // Selects the top layer
    designDoc.activeLayer.visible = true; // Make Top Layer Visible

} 
};


//================================= MAIN ================================= 
// Reset "Front.psd" aka designDoc 
resetDesignDoc()

var create = true;


while (create) {
CreateMockup();      // Create Item
  gotoNextLayer();      // Select Next Layer
  selectNEXT()
  if(designDoc.activeLayer.name.indexOf("END") >= 0){
           //alert('All Designs Applied')
            var create = false;

    } 

}




// Create Mockup
  function CreateMockup() {

 // Sets "Mockup.psd" aka mockupDoc as active document.
  app.activeDocument = mockupDoc;

// Toggles layerset "GTO BBACKGROUND" to visible for .jpg version export.
  gtoBG.visible = true;

// Sets "FRONT.psd" aka designDoc as active document.
  app.activeDocument = designDoc;

// Saves "Front.psd" to original destination and updates embeded content (smart object) in "Mockup.psd" aka mockupDoc
  var designDocPath = designDoc.path;
  var Name = designDoc.name.replace(/\.[^\.]+$/, ''); 
  designDoc.saveAs(File(designDocPath + "/" + Name + ".psd"));

// Sets "Mockup.psd" aka mockupDoc as active document.
  app.activeDocument = mockupDoc;

// Checks if current Design is a "FLEX" print or not.
if(designDoc.activeLayer.name.toLowerCase().indexOf("flex") >= 0){
    //Hides Texture Overlays
        Selecttexures();
       }


// Creates a Sharpness Layer for the jpg version.
  createSharpnessLayer ();

// Saves the composited image from Mockup.psd containing Filename minus "Mockup", Name of the visible layer "Colors" layerset and the Name of the active layer in Front.psd
  mockupDoc.saveAs((new File(mockupDocPath + "/_Files Export/with GTO Background" +'/' + basename + ' ' + designDoc.activeLayer.name +'.jpg')),jpegOptions,true);      

// Remove OLD Sharpness Layer
  mockupDoc.layers.getByName('Sharpness').remove(); 

// Toggles layerset "GTO BBACKGROUND" to hidden for .png version export.
  gtoBG.visible = false;

// Creates a Sharpness Layer for the png version.
  createSharpnessLayer ();

// Saves the composited image from Mockup.psd containing Filename minus "Mockup", Name of the visible layer "Colors" layerset and the Name of the active layer in Front.psd
  mockupDoc.saveAs((new File(mockupDocPath + "/_Files Export/png" +'/' + basename + ' ' + designDoc.activeLayer.name +'.png')),pngOptions,true);     

// Remove OLD Sharpness Layer
  mockupDoc.layers.getByName('Sharpness').remove(); 


} //END Create Items function



  // Select Next Layer

function gotoNextLayer() {

// Sets "FRONT.psd" aka designDoc as active document.
  app.activeDocument = designDoc;


  };

  // Selects Next Layer in Front.psd aka designDoc and makes only this one visible.
  function selectNEXT(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Bckw'));
    desc1.putReference(cTID('null'), ref1);
    desc1.putBoolean(cTID('MkVs'), false);
    executeAction(cTID('slct'), desc1, dialogMode);
  };


//================================= HELPERS ================================= 

// "Paste in Place" function.
function pasteInPlace(enabled, withDialog) {
    if (enabled != undefined && !enabled)
    return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putBoolean(sTID("inPlace"), true);
    desc1.putEnumerated(cTID('AntA'), cTID('Annt'), cTID('Anno'));
    executeAction(cTID('past'), desc1, dialogMode);
};

 
// Create "Sharpness" Layer.
function createSharpnessLayer () { 
  mockupDoc.selection.selectAll();  
  mockupDoc.selection.copy(true); 
  pasteInPlace();
  mockupDoc.activeLayer.name = "Sharpness" 
  mockupDoc.activeLayer.move( mockupDoc, ElementPlacement.PLACEATBEGINNING );
  mockupDoc.activeLayer.applyHighPass(0.5)
  mockupDoc.activeLayer.blendMode  = BlendMode.LINEARLIGHT;
  mockupDoc.activeLayer.opacity = 50; 
};  

// Select all print texture overlays in "Mockuo.psd" aka mockupDoc
function Selecttexures() {
  // Select
  function step1(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putName(cTID('Lyr '), "LIGHT FABRIC TEXTURE");
    desc1.putReference(cTID('null'), ref1);
    desc1.putBoolean(cTID('MkVs'), false);
    var list1 = new ActionList();
    list1.putInteger(43);
    desc1.putList(cTID('LyrI'), list1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Select Linked Layers
  function step2(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
    desc1.putReference(cTID('null'), ref1);
    executeAction(sTID('selectLinkedLayers'), desc1, dialogMode);
  };

  // Hide
  function step3(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var list1 = new ActionList();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
    list1.putReference(ref1);
    desc1.putList(cTID('null'), list1);
    executeAction(cTID('Hd  '), desc1, dialogMode);
  };
  step1();      // Select
  step2();      // Select Linked Layers
  step3();      // Hide
};