Search code examples
javascriptautomationphotoshopphotoshop-script

Photoshop jsx script - script works fine but I want to be able to use it without a dialogue and selection


I have a piece of script which I am using in a bigger project

In Photoshop I have a group named images and a layer inside

I have managed to get this script working. A dialogue opens I select the file and it replace the content without problem

But I want the script to run without opening a dialogue and having to select a file

The file I want to replace is called london.png

How do I do this automatically without a dialogue

Here's my code and also a screenshot of what it asks (dilog I want to disappear and auto select london.png

Also a screenshot of the root folder and files

    var replacementFile = new File("~/london.png");
    var theLayer = app.activeDocument.layerSets.getByName('image');
    var changeLayer = theLayer.layers[0]
    changeLayer = replaceContents(replacementFile);

    ////// replace contents //////  
    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  
    };  

Screenshot 1 dialogue enter image description here

Screenshot 2 file structure enter image description here


Solution

  • ///// >>> the mistake was here
    var replacementFile = new File("C;/users/harry/script/london.png");
    var theLayer = app.activeDocument.layerSets.getByName('image');
    var changeLayer = theLayer.layers[0]
    changeLayer = replaceContents(replacementFile);
    
    ////// replace contents //////  
    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  
    };