Skip to content
Advertisement

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

i have totally re-editied My communication and spelling skills are not very good so sorry if you find it hard.

to make it easier to read i am going to shorten and sweeten my post

  1. i have a psd file with groups and layers set as needed

  2. i then created a script to chnage the text accordingly to json inout from a json file code and file example below.

    #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 Data example.

{“title” : “LONDON”, “ord” : “51.5074° N, 0.1278° W”}

  1. i then found a piece of code and altered it to my needs (well nearly) the code snippet allows a dialog to open and me to selct the file needed

the problem is i need it to select the image using the title name from the json data to the grab say example LONDON.PNG and then replace it all without a dialog and selection (silent and auto)

what is happening with my new code snippet added

below is my uodated code and screenshot of my projects root folder

    #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 ////// 

root folder content and structure

Advertisement

Answer

    #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 ////// 
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement