【问题标题】:Replace content via scripting in batch of smart objects(Replace layers) in photoshop cc 20.0.0在Photoshop立方厘米20.0.0替换批次智能对象(取代层)通过脚本内容
【发布时间】:2019-09-06 03:25:19
【问题描述】:

所以我将这 28 个 *.tif 图像文件作为 28 个层(智能对象)排列在一个 .psd 文件中,并希望用另一个 .tif 文件替换每个层。我希望用这样的循环运行一些脚本(jsx):

for (i=1;i<=28;i++) {
  for j in (start,end) {
     for k in (a,b,c,d,e,f) {
            file = 'chr' + $i + '_' + $j + '_' + $k;
                     }}}

2020 年 7 月更新:抱歉这么晚,我找到了一个解决方案,是这样的,你首先应该下载 json2.js:

// replace smart object’s content and save psd;
#target photoshop
#include json2.js
if (app.documents.length > 0) {
  var myDocument = app.activeDocument;
  var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
  var thePath = myDocument.path;

  psdOpts = new PhotoshopSaveOptions();
  psdOpts.embedColorProfile = true;
  psdOpts.alphaChannels = true;
  psdOpts.layers = true;
  psdOpts.spotColors = true;

//lists for desired filename input 
    var ppath = "c:/here/goes/file/path/for/taking/input/file";
// (you can implement using json too)
       var num = [
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "10",
      "11",
      "12",
      "13",
      "14",
      "15",
      "16",
      "17",
      "18",
      "19",
      "20",
      "21",
      "22",
      "X",
      "Y"
    ];
    var nnum = [
      "1",
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "10",
      "11",
      "12",
      "13",
      "14",
      "15",
      "16",
      "17",
      "18",
      "19",
      "20",
      "21",
      "22",
      "X"
    ];
    var lletter = [
      "q",
      "p",
      "o",
      "n",
      "m",
      "l",
      "u",
      "t",
      "k",
      "j",
      "i",
      "h",
      "g",
      "f",
      "d",
      "c",
      "b",
      "ae",
      "ad",
      "ac",
      "ab",
      "aa",
      "a",
      "z",
      "y",
      "x",
      "w",
      "v"
    ];
    var letter = [
      "q",
      "p",
      "o",
      "n",
      "m",
      "l",
      "u",
      "t",
      "k",
      "j",
      "i",
      "h",
      "g",
      "f",
      "d",
      "c",
      "b",
      "ae",
      "ad",
      "ac",
      "ab",
      "aa",
      "a",
      "z",
      "y",
      "x",
      "w",
      "v"
    ];
  
// main code starts here : 

      for (var i in num) {
        saveJPEG(thNamer);
        alert("saved Jpeg");
        myDocument.saveAs((new File("D:\\thesis-bioinformatics" + '/vol-ID/' + thNamer + "_" +".psd")),psdOpts,true);
        alert("saved psd");
        
          for (var k in letter) {
            var TitleGroup = myDocument.layerSets.getByName('chr_place_plot_');
            var TitleGroup2 = myDocument.layerSets.getByName('chr_text');
           
            var thNamer = 'chr' + num[i] + '_start' + '_plot_';
            var thNames = 'chr' + num[i] + '_start' + '_plot_' + letter[k];
            var thprevNames = 'chr' + nnum[i] + '_start' + '_plot_' + letter[k];
            VolLayer = TitleGroup.artLayers.getByName(thprevNames);
            VolLayer2 = TitleGroup2.artLayers.getByName('Chr1');
           
            myDocument.activeLayer = VolLayer;
            var theFiles = ppath + thNames + '.tif';
          
          if (theFiles && thprevNames != thNames) 
          {         
            VolLayer = replaceContents(theFiles);
            VolLayer2.textItem.contents = thNamer;
            }

      }
    }
};  

【问题讨论】:

  • 文件名按以下顺序排列:chr$i_$k_$k 示例:chr1_start_a、chr1_end_q、chr22_start_b 等
  • 仅供参考,通常最好edit your question 来添加信息。究竟是什么问题?

标签: javascript json jsx photoshop


【解决方案1】:

这将接受 JSON 格式的输入并替换文本,同样可以替换给出文件路径的图像。

它所做的只是查找图层组,然后将内容替换为您选择的文本或文件,然后将 jpg 和 PSD 文件保存在输出文件夹中。

您可以在此处查看代码:https://github.com/avialxee/photoshop-jsx


#target photoshop
#include json2.js // json to js parser


if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var outpath = thePath+'/output/';
    
    psdOpts = new PhotoshopSaveOptions();
    psdOpts.embedColorProfile = true;
    psdOpts.alphaChannels = true;
    psdOpts.layers = true;
    psdOpts.spotColors = true;


    var obj = loadInput('inputs.json')

    function loadInput(relpath){
        var input = new File($.fileName);
        var jsoninput = new File(input.path + '/' + relpath);

        jsoninput.open('r');
        var name = jsoninput.read();
        jsoninput.close();

        return JSON.parse(name);
    }


    
    for (var i in obj.name) {
        
        var TitleGroup = myDocument.layerSets.getByName('baseImage');
        var TitleGroup2 = myDocument.layerSets.getByName('text');
        var thNamer = obj.name[i];
        VolLayer = TitleGroup2.artLayers.getByName('participantName');
        myDocument.activeLayer = VolLayer;
        //var theFiles = ppath + thNamer + '.tif';
        
        if (thNamer) {
            //VolLayer = replaceContents(theFiles);
            VolLayer.textItem.contents = thNamer;
            saveJPEG(thNamer);
            alert("saved Jpeg");
            myDocument.saveAs((new File(outpath+ thNamer + "_" + ".psd")), psdOpts, true);
            
        }      
    }
};




function getFiles(theFile) {
    if (theFile.name.match(/\.(psd|tif)$/i)) {
        return true
    };
};

function replaceContents(newFile) {
    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
    var desc3 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc3.putPath(idnull, new File(theFiles));
    var idPgNm = charIDToTypeID("PgNm");
    desc3.putInteger(idPgNm, 1);
    executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
};

function saveJPEG(name) {
    var doc = app.activeDocument;
    var file = new File(outpath + name + '.Jpg');
    var opts = new JPEGSaveOptions();
    opts.quality = 10;
    doc.saveAs(file, opts, true);
}

【讨论】:

    【解决方案2】:

    试试这个代码

    它将遍历所有层并将其替换为您将选择的 .tiff 文件

    var doc = app.activeDocument;
    
    function editContent() {
    
    cTID = function(s) { return app.charIDToTypeID(s); };
    sTID = function(s) { return app.stringIDToTypeID(s); };
    
    function Rename() {
      // Edit Contents
      function step1(enabled, withDialog) {
        if (enabled != undefined && !enabled)
          return;
        var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
        var desc1 = new ActionDescriptor();
        executeAction(sTID('placedLayerEditContents'), desc1, dialogMode);
      };
    
      step1();      // Edit Contents
    };
    
    Rename.main = function () {
      Rename();
    };
    
    Rename.main();
    
        }
    
    function merge() {
        var fileRef = File.openDialog ('Choose File', ['*.tif;*tiff'], true);
        app.open( new File( fileRef ) );
        if(app.activeDocument.activeLayer.isBackgroundLayer ) app.activeDocument.activeLayer.name = 'From Background';    
        app.activeDocument.selection.selectAll();
        app.activeDocument.selection.copy();
        app.activeDocument.selection.deselect();
    
    
        var idMk = charIDToTypeID( "Mk  " );
        var desc8946 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1324 = new ActionReference();
            var idBckL = charIDToTypeID( "BckL" );
            ref1324.putClass( idBckL );
        desc8946.putReference( idnull, ref1324 );
        var idUsng = charIDToTypeID( "Usng" );
            var ref1325 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref1325.putEnumerated( idLyr, idOrdn, idTrgt );
        desc8946.putReference( idUsng, ref1325 );
    executeAction( idMk, desc8946, DialogModes.NO );
    
    
    
        app.activeDocument.close(SaveOptions.SAVECHANGES);
        app.activeDocument.artLayers.add();
        app.activeDocument.paste();
        app.activeDocument.flatten();
        app.activeDocument.close(SaveOptions.SAVECHANGES);
        }
    
    for (var i=0;i<doc.layers.length;i++){ 
        doc.activeLayer=doc.layers[i];
        editContent();
        merge();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2013-10-23
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多