Photoshop 脚本——尝试替换项目根文件夹中的图像

Photoshop Script -- Trying to Replace an image from project root folder

我完全re-editied 我的沟通和拼写能力不是很好,如果您觉得困难,请见谅。

为了让阅读更容易,我将缩短和甜化我的 post

  1. 我有一个 psd 文件,其中根据需要设置了组和图层

  2. 然后我创建了一个脚本,根据下面的 json 文件代码和文件示例将文本更改为 json inout。

    #include json2.js
    //////// ^^^^ loading JSON2 ///////////
    var obj = loadJson('text.json');
    //////// ^^^^ Variable for JSON Text Data  ///////////
    var titleGroup = app.activeDocument.layerSets.getByName('text');
    var titleLayer = titleGroup.layers[0];
    var ordinatesLayer = titleGroup.layers[1];
    titleLayer.textItem.contents = obj.title;
    ordinatesLayer.textItem.contents = obj.ord;
    ////// ^^^ Locate And change Text using JSON Data ///////////
    var theLayer = app.activeDocument.layerSets.getByName('image');
    var changeLayer = theLayer.layers[0]
    //////// ^^^ var set need to create future functions to grab image location from the json data and replace image  ///////////
    
    
    
    saveJpeg(obj.title + 'Finished');
    
    //////// ^^^ Script Action Using Functions Below to Save Altered results ///////////
    
    ////////  Functions BELOW!!! /////////
    function loadJson(relPath) {
      var script = new File($.fileName);
      var jsonFile = new File(script.path + '/' + relPath);
      jsonFile.open('r');
      var str = jsonFile.read();
      jsonFile.close();
      return JSON.parse(str);
    }
    
    ////// ^^^ load and parse data to set vairiables for use //////
    
    function saveJpeg(name) {
      var doc = app.activeDocument;
      var file = new File(doc.path + '/' + name + '.jpg');
      var opts = new JPEGSaveOptions();
      opts.quality = 10;
      doc.saveAs(file, opts, true);
    }
    
    ////// ^^^ save Finished Results /////
    //alert('Your Script has done!!!');
    

JSON数据示例。

{“标题”:“伦敦”, “ord”:“51.5074° N,0.1278° W”}

  1. 然后我找到了一段代码并根据我的需要修改了它(差不多) 代码片段允许打开一个对话框,我可以选择所需的文件

问题是我需要它 select 使用来自 json 数据的标题名称的图像到 grab say 示例 LONDON.PNG 然后在没有对话框的情况下全部替换它并且selection(静音和自动)

下面是我更新的代码和项目根文件夹的屏幕截图

    #include json2.js
    //////// ^^^^ loading JSON2 ///////////
    var obj = loadJson('text.json');
    //////// ^^^^ Variable for JSON Text Data  ///////////
    var titleGroup = app.activeDocument.layerSets.getByName('text');
    var titleLayer = titleGroup.layers[0];
    var ordinatesLayer = titleGroup.layers[1];
    titleLayer.textItem.contents = obj.title;
    ordinatesLayer.textItem.contents = obj.ord;
    ////// ^^^ Locate And change Text using JSON Data ///////////
    var theLayer = app.activeDocument.layerSets.getByName('image');
    var changeLayer = theLayer.layers[0]
    var replacementFile = new File(obj.title + "png");
    //////// ^^^ var set need to create future functions to grab image location from the json data and replace image  ///////////



    
    changeLayer = replaceContents(replacementFile);
    saveJpeg(obj.title + 'Finished');

    //////// ^^^ Script Action Using Functions Below to Save Altered results ///////////

    ////////  Functions BELOW!!! /////////
    function loadJson(relPath) {
      var script = new File($.fileName);
      var jsonFile = new File(script.path + '/' + relPath);
      jsonFile.open('r');
      var str = jsonFile.read();
      jsonFile.close();
      return JSON.parse(str);
    }
    
    ////// ^^^ load and parse data to set vairiables for use //////

    function saveJpeg(name) {
      var doc = app.activeDocument;
      var file = new File(doc.path + '/' + name + '.jpg');
      var opts = new JPEGSaveOptions();
      opts.quality = 10;
      doc.saveAs(file, opts, true);
    }
    
    ////// ^^^ save Finished Results /////
    //alert('Your Script has done!!!');


    function replaceContents (newFile) {  

    var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );  
        var desc3 = new ActionDescriptor();  
        var idnull = charIDToTypeID( "null" );  
        desc3.putPath( idnull, new File( newFile ) );  
        var idPgNm = charIDToTypeID( "PgNm" );  
        desc3.putInteger( idPgNm, 1 );  
    executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );  
    return app.activeDocument.activeLayer  
    };  
    
    ////// ^^^ replace contents ////// 

    #include json2.js
    //////// ^^^^ loading JSON2 ///////////
    var obj = loadJson('text.json');
    //////// ^^^^ Variable for JSON Text Data  ///////////
    var titleGroup = app.activeDocument.layerSets.getByName('text');
    var titleLayer = titleGroup.layers[0];
    var ordinatesLayer = titleGroup.layers[1];
    titleLayer.textItem.contents = obj.title;
    ordinatesLayer.textItem.contents = obj.ord;
    ////// ^^^ Locate And change Text using JSON Data ///////////
    var theLayer = app.activeDocument.layerSets.getByName('image');
    var changeLayer = theLayer.layers[0]
    /////// added and fixed below /////
    var string = 'C:/Users/apps/Documents/script/';
    var replacementFile = new File(string  + obj.title + '.png');
    //////// ^^^ var set need to create future functions to grab image location from the json data and replace image  ///////////



    
    changeLayer = replaceContents(replacementFile);
    saveJpeg(obj.title + 'Finished');

    //////// ^^^ Script Action Using Functions Below to Save Altered results ///////////

    ////////  Functions BELOW!!! /////////
    function loadJson(relPath) {
      var script = new File($.fileName);
      var jsonFile = new File(script.path + '/' + relPath);
      jsonFile.open('r');
      var str = jsonFile.read();
      jsonFile.close();
      return JSON.parse(str);
    }
    
    ////// ^^^ load and parse data to set vairiables for use //////

    function saveJpeg(name) {
      var doc = app.activeDocument;
      var file = new File(doc.path + '/' + name + '.jpg');
      var opts = new JPEGSaveOptions();
      opts.quality = 10;
      doc.saveAs(file, opts, true);
    }
    
    ////// ^^^ save Finished Results /////
    //alert('Your Script has done!!!');


    function replaceContents (newFile) {  

    var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );  
        var desc3 = new ActionDescriptor();  
        var idnull = charIDToTypeID( "null" );  
        desc3.putPath( idnull, new File( newFile ) );  
        var idPgNm = charIDToTypeID( "PgNm" );  
        desc3.putInteger( idPgNm, 1 );  
    executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );  
    return app.activeDocument.activeLayer  
    };  
    
    ////// ^^^ replace contents //////