PS js脚本循环槽层,逐个可见,每次在

时间:2017-03-16 16:20:28

标签: javascript photoshop-script

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

  1. 在工作文件中打开智能对象图层。
  2. 获取第一个图层名称,使第一个图层可见,更新智能对象。
  3. 从主文档中获取组(颜色)中的名称和活动图层。
  4. 2.Name + 3.Name并另存为.png。
  5. 请参阅我的附图以及此处的简短截屏视频: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. 如何检查layerset / group" Colors"中的哪一层是活动的? mockupDoc文档并将其存储为变量以将其用于保存名称?

      1. 我需要脚本从上到下穿过Front.psd的每一层,如:从顶层开始,只使该层可见 - >保存此文档 - >返回mockup.psd - >将此文件另存为jpg和png - >返回到Front.psd并进入下一层重复这些步骤(可见/保存/以前的doc / save png jpg /等等)?当最后一层处理完并停止脚本时,脚本将如何知道?

      2. 现在,脚本将文件保存在mockup.psd的根文件夹中。是否可以通过颜色(即红色,黄色......)使用文件夹,并检查我要为变量保存的文件"颜色"我之前存储并保存到黄色文件夹,如果变量是黄色?

    2. 希望这听起来不奇怪,我的英语太糟糕了;)

      感谢大家的帮助。

2 个答案:

答案 0 :(得分:0)

欢迎使用Stack Overflow。通常情况下,我们会要求查看您编写的代码 - 即使它不起作用,只需添加注释即可解释您要执行的操作。在提问时,破碎的代码总比没有代码好。但是请记住,SO不是脚本编写服务。

在图层上循环是直截了当的 - 现在避免使用组 - 这是更高级的。环顾四周,你应该找到代码来保存pngs。

这将帮助您入门。

 // 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);
}

答案 1 :(得分:0)

谢谢@Mr Mystery Guest

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

//================================= 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
};