Highlighted

Replace only selected smart objects with images from the bridge:

Contributor ,
Nov 13, 2020

Copy link to clipboard

Copied

Hello everyone!
Friends I'm having trouble creating a script that has the purpose of replacing the selected smart objects with images from the bridge according to their selection order, but the result is frustrating, only the last image is loaded for all smart objects. All help is valid.Thanks.
Here are the prints:

Captura de tela 2020-11-13 123045.jpg

Script here:

#target photoshop
app.bringToFront();    
var selLayers=[];    
    selLayers= getSelectedLayersIdx();    
    for (var a in selLayers){    
    makeActiveByIndex( selLayers[a], false );    
    PicturesBRIDGE();
}    

function PicturesBRIDGE(){
    var bridgeFileList =[];
    fileList =GetFilesFromBridge();
    if(i >= fileList.length)
    var bridgeFile= File(fileList[i]);
    for(i=0;i<fileList.length;i++){
       replaceSmart( fileList[i]);
        //open( fileList[i] ); 
    }
}

function replaceSmart(file){
    function c2t(s){return app.charIDToTypeID(s);};
    function s2t(s){return app.stringIDToTypeID(s);};
    var d = new ActionDescriptor();
    d.putPath( c2t( "null" ), new File(file));
    executeAction( s2t( "placedLayerReplaceContents" ), d, DialogModes.NO );
}

function GetFilesFromBridge() {
function script(){
var fL = app.document.selections;
var tF=[];
for(var a in fL){
    if(fL[a].type =='file'){
        tF.push(new File(encodeURI(fL[a].spec.fsName)));
        }
    }
return tF.toSource();
}
    var fileList;
        var bt = new BridgeTalk();
        bt.target = "bridge";
         bt.body = "var ftn = " + script.toSource() + "; ftn();";
        bt.onResult = function( inBT ) { fileList = eval( inBT.body ); }
        bt.onError = function( inBT ) { fileList = new Array(); }
        bt.send(8); 
        bt.pump();
    if ( undefined == fileList ) fileList = new Array();
    return fileList; 
};

function makeActiveByIndex( idx, visible ){     
    try{
    var desc = new ActionDescriptor();     
        var ref = new ActionReference();     
        ref.putIndex(charIDToTypeID( "Lyr " ), idx)     
        desc.putReference( charIDToTypeID( "null" ), ref );     
        desc.putBoolean( charIDToTypeID( "MkVs" ), visible );     
        executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );  
    }
    catch(e){}
};

function getSelectedLayersIdx(){   
    var selectedLayers = new Array;   
    var ref = new ActionReference();   
    ref.putProperty(charIDToTypeID('Prpr'), stringIDToTypeID('targetLayers'));  
    ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );   
    var desc = executeActionGet(ref);   
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){   
    desc = desc.getList( stringIDToTypeID( 'targetLayers' ));   
    var c = desc.count;   
    var selectedLayers = new Array();   
    for(var i=0;i<c;i++){   
    try{ activeDocument.backgroundLayer; selectedLayers.push(  desc.getReference( i ).getIndex() );   
    }catch(e){ selectedLayers.push(  desc.getReference( i ).getIndex()+1 );}}   
    }else{   
    var ref = new ActionReference();   
    ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));   
    ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );   
    try{ activeDocument.backgroundLayer;   
    selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1);   
    }catch(e){ selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));}   
    var vis = app.activeDocument.activeLayer.visible;  
    if(vis == true) app.activeDocument.activeLayer.visible = false;  
    var desc9 = new ActionDescriptor();  
    var list9 = new ActionList();  
    var ref9 = new ActionReference();  
    ref9.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );  
    list9.putReference( ref9 );  
    desc9.putList( charIDToTypeID('null'), list9 );  
    executeAction( charIDToTypeID('Shw '), desc9, DialogModes.NO );  
    if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();  
    app.activeDocument.activeLayer.visible = vis; } return selectedLayers;};  
    function selectLayerByIndex(index,add){   
    var ref = new ActionReference();  
    ref.putIndex(charIDToTypeID('Lyr '), index);  
    var desc = new ActionDescriptor();  
    desc.putReference(charIDToTypeID('null'), ref );  
    if(add) desc.putEnumerated( stringIDToTypeID( 'selectionModifier' ), stringIDToTypeID( 'selectionModifierType' ), stringIDToTypeID( 'addToSelection' ) );   
    desc.putBoolean( charIDToTypeID( 'MkVs' ), false );   
    try{ executeAction(charIDToTypeID('slct'), desc, DialogModes.NO );}catch(e){}  
}; 
Adobe Community Professional
Correct answer by Kukurykus | Adobe Community Professional

I didn't want to go too far, so I made just a few changes:

 

sL = getSelectedLayersIdx(); for(i in sL) {
	makeActiveByIndex(sL[i], false); function script(v) {
		if ((fle = app.document.selections[v]).type =='file')
			return fle.spec.toSource()
	}
	(bt = new BridgeTalk()).target = 'bridge'
	bt.body = 'ftn = ' + script.toSource() + '; ftn(' + i + ')'
	bt.onResult = function(v) {fle = eval(v.body)}
	bt.send(2); replaceSmart(fle)
}

 

TOPICS
Actions and scripting

Views

41

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Replace only selected smart objects with images from the bridge:

Contributor ,
Nov 13, 2020

Copy link to clipboard

Copied

Hello everyone!
Friends I'm having trouble creating a script that has the purpose of replacing the selected smart objects with images from the bridge according to their selection order, but the result is frustrating, only the last image is loaded for all smart objects. All help is valid.Thanks.
Here are the prints:

Captura de tela 2020-11-13 123045.jpg

Script here:

#target photoshop
app.bringToFront();    
var selLayers=[];    
    selLayers= getSelectedLayersIdx();    
    for (var a in selLayers){    
    makeActiveByIndex( selLayers[a], false );    
    PicturesBRIDGE();
}    

function PicturesBRIDGE(){
    var bridgeFileList =[];
    fileList =GetFilesFromBridge();
    if(i >= fileList.length)
    var bridgeFile= File(fileList[i]);
    for(i=0;i<fileList.length;i++){
       replaceSmart( fileList[i]);
        //open( fileList[i] ); 
    }
}

function replaceSmart(file){
    function c2t(s){return app.charIDToTypeID(s);};
    function s2t(s){return app.stringIDToTypeID(s);};
    var d = new ActionDescriptor();
    d.putPath( c2t( "null" ), new File(file));
    executeAction( s2t( "placedLayerReplaceContents" ), d, DialogModes.NO );
}

function GetFilesFromBridge() {
function script(){
var fL = app.document.selections;
var tF=[];
for(var a in fL){
    if(fL[a].type =='file'){
        tF.push(new File(encodeURI(fL[a].spec.fsName)));
        }
    }
return tF.toSource();
}
    var fileList;
        var bt = new BridgeTalk();
        bt.target = "bridge";
         bt.body = "var ftn = " + script.toSource() + "; ftn();";
        bt.onResult = function( inBT ) { fileList = eval( inBT.body ); }
        bt.onError = function( inBT ) { fileList = new Array(); }
        bt.send(8); 
        bt.pump();
    if ( undefined == fileList ) fileList = new Array();
    return fileList; 
};

function makeActiveByIndex( idx, visible ){     
    try{
    var desc = new ActionDescriptor();     
        var ref = new ActionReference();     
        ref.putIndex(charIDToTypeID( "Lyr " ), idx)     
        desc.putReference( charIDToTypeID( "null" ), ref );     
        desc.putBoolean( charIDToTypeID( "MkVs" ), visible );     
        executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );  
    }
    catch(e){}
};

function getSelectedLayersIdx(){   
    var selectedLayers = new Array;   
    var ref = new ActionReference();   
    ref.putProperty(charIDToTypeID('Prpr'), stringIDToTypeID('targetLayers'));  
    ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );   
    var desc = executeActionGet(ref);   
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){   
    desc = desc.getList( stringIDToTypeID( 'targetLayers' ));   
    var c = desc.count;   
    var selectedLayers = new Array();   
    for(var i=0;i<c;i++){   
    try{ activeDocument.backgroundLayer; selectedLayers.push(  desc.getReference( i ).getIndex() );   
    }catch(e){ selectedLayers.push(  desc.getReference( i ).getIndex()+1 );}}   
    }else{   
    var ref = new ActionReference();   
    ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));   
    ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );   
    try{ activeDocument.backgroundLayer;   
    selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1);   
    }catch(e){ selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));}   
    var vis = app.activeDocument.activeLayer.visible;  
    if(vis == true) app.activeDocument.activeLayer.visible = false;  
    var desc9 = new ActionDescriptor();  
    var list9 = new ActionList();  
    var ref9 = new ActionReference();  
    ref9.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );  
    list9.putReference( ref9 );  
    desc9.putList( charIDToTypeID('null'), list9 );  
    executeAction( charIDToTypeID('Shw '), desc9, DialogModes.NO );  
    if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();  
    app.activeDocument.activeLayer.visible = vis; } return selectedLayers;};  
    function selectLayerByIndex(index,add){   
    var ref = new ActionReference();  
    ref.putIndex(charIDToTypeID('Lyr '), index);  
    var desc = new ActionDescriptor();  
    desc.putReference(charIDToTypeID('null'), ref );  
    if(add) desc.putEnumerated( stringIDToTypeID( 'selectionModifier' ), stringIDToTypeID( 'selectionModifierType' ), stringIDToTypeID( 'addToSelection' ) );   
    desc.putBoolean( charIDToTypeID( 'MkVs' ), false );   
    try{ executeAction(charIDToTypeID('slct'), desc, DialogModes.NO );}catch(e){}  
}; 
Adobe Community Professional
Correct answer by Kukurykus | Adobe Community Professional

I didn't want to go too far, so I made just a few changes:

 

sL = getSelectedLayersIdx(); for(i in sL) {
	makeActiveByIndex(sL[i], false); function script(v) {
		if ((fle = app.document.selections[v]).type =='file')
			return fle.spec.toSource()
	}
	(bt = new BridgeTalk()).target = 'bridge'
	bt.body = 'ftn = ' + script.toSource() + '; ftn(' + i + ')'
	bt.onResult = function(v) {fle = eval(v.body)}
	bt.send(2); replaceSmart(fle)
}

 

TOPICS
Actions and scripting

Views

42

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 13, 2020 0
Most Valuable Participant ,
Nov 13, 2020

Copy link to clipboard

Copied

It look like if I open at mockup template in photoshop and target  smart object layer that need to be replaced. Your script create a list of target layers, The tries to use replaces content on the selected layer with all the files currently selected in Bridges UI. Without regard to layer type or  object size.  So if I select a layer that is not a smart object layer the replace content will fail.  If a select only Smart object layer.  Replace content will replaced the object with any size file that may or may not  work with the template. Each selected Smart object layer will  have it content replaced with all the selected files in the bridge UI. So all the smart object layers selected will wind up the last image seleced in the bridge.

 

Your process has major issues. Each smart object that need te be replaced needs its own Que of replacements and after all smart objects have their first replacement an output would need to be saved for the populated template.  Then the next set  of objects replacement would be processed and saved. Replacement must be the correct size for the templates objects for the replacement  to work correctly for the template.

 

I would not know how to do the processing for replaceing  more the a single smart object  layer with a single bridge filelist.  My Batch Mockup scripts use image folders to hold replacement for each smart object layer that get replacement content. Image file manes must sort correctly so the correct set of image will bet populated into  the templates together.

 

Free Phoroshop Photo Collage and Toolkit 

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
Nov 13, 2020

Copy link to clipboard

Copied

I didn't want to go too far, so I made just a few changes:

 

sL = getSelectedLayersIdx(); for(i in sL) {
	makeActiveByIndex(sL[i], false); function script(v) {
		if ((fle = app.document.selections[v]).type =='file')
			return fle.spec.toSource()
	}
	(bt = new BridgeTalk()).target = 'bridge'
	bt.body = 'ftn = ' + script.toSource() + '; ftn(' + i + ')'
	bt.onResult = function(v) {fle = eval(v.body)}
	bt.send(2); replaceSmart(fle)
}

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 13, 2020 1
Contributor ,
Nov 13, 2020

Copy link to clipboard

Copied

Perfect! Here it worked very well. Kukurykus  thanks for sharing your knowledge.
JJMack thanks for your attention.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 13, 2020 1