Skip to main content
Mohamed Hameed21513110
Inspiring
February 2, 2022
Answered

Resize selected layers

  • February 2, 2022
  • 3 replies
  • 1496 views

welcome everybody
I have code that resizes a layer by a value
The code only works on one layer
I found a code that implements action on selected layers, but it doesn't work for me
It only works on one layer, and when implemented on more than one layer, it gives a very different result from what I want
I don't know where the problem is... and this is the code

 

    Width = 100
    Hight = 200
   
  //- Resize Layer With  Value - //
var startRulerUnits = app.preferences.rulerUnits;  
    app.preferences.rulerUnits = Units.PIXELS;  
    var bounds = activeDocument.activeLayer.bounds;  
    var width = bounds[2].value - bounds[0].value;
    var height = bounds[3].value - bounds[1].value;
    var newWidth = (100 / width) * Width;  
    var newHeight = (100 / height) * Hight;  
    
    
var layers = getSelectedLayers();
for (var i = 0; i < layers.length; i ++){
	layers[i].resize(newWidth, newHeight, AnchorPosition.MIDDLECENTER);
}    
    
function getSelectedLayers(){ 
	var idGrp = stringIDToTypeID( "groupLayersEvent" );
	var descGrp = new ActionDescriptor();
	var refGrp = new ActionReference();
	refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
	descGrp.putReference(charIDToTypeID( "null" ), refGrp );
	executeAction( idGrp, descGrp, DialogModes.ALL );
	var resultLayers=new Array();
	
    for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++){resultLayers.push(app.activeDocument.activeLayer.layers[ix])}
	var id8 = charIDToTypeID( "slct" );
    var desc5 = new ActionDescriptor();
    var id9 = charIDToTypeID( "null" );
    var ref2 = new ActionReference();
    var id10 = charIDToTypeID( "HstS" );
    var id11 = charIDToTypeID( "Ordn" );
    var id12 = charIDToTypeID( "Prvs" );  
    ref2.putEnumerated( id10, id11, id12 );
	desc5.putReference( id9, ref2 );
	executeAction( id8, desc5, DialogModes.NO );
	return resultLayers;
}
This topic has been closed for replies.
Correct answer c.pfaffenbichler

I would recommend converting all the Layers to Smart Objects (if they are not already) before transforming. 

Does this help? 

// scale all selected layers to pixeldimensions;
// 2022, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
    var theW = 400;
    var theH = 200;
    var theLayers = collectSelectedLayersBounds ();
// transform;
    for (var x = 0; x < theLayers.length; x++) {
        var horScale = theW / (theLayers[x][2][2] - theLayers[x][2][0]) * 100;
        var verScale = theH / (theLayers[x][2][3] - theLayers[x][2][1]) * 100;
        layerDuplicateOffsetScaleRotate (theLayers[x][1], 0, 0, horScale, verScale, 0, true)   
    };
// reselect layers;
    selectLayerByID(theLayers[0][1], false);
    for (var y = 1; y < theLayers.length; y++) {selectLayerByID(theLayers[y][1], true)}
    };
////// collect bounds of selected layers //////
function collectSelectedLayersBounds () {
    // set to pixels;
        var originalRulerUnits = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;
    // get selected layers;
        var selectedLayers = new Array;
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
        var desc = executeActionGet(ref);
        if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
        else {var theAdd = 1};
        if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
        desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
        var c = desc.count;
        var selectedLayers = new Array();
        // run through selected layers;
        for(var i=0;i<c;i++){
        var theIndex = desc.getReference( i ).getIndex()+theAdd;
        // get id for solid color layers;
        try {
        var ref = new ActionReference();
        ref.putIndex( charIDToTypeID("Lyr "), theIndex ); 
        var layerDesc = executeActionGet(ref);
        var theName = layerDesc.getString(stringIDToTypeID('name'));
        var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
        var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
        var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
        selectedLayers.push([theName, theIdentifier, theseBounds]);
        } catch (e) {};
        };
        // if only one:
        }else{
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
        var layerDesc = executeActionGet(ref);
        try {
        var theName = layerDesc.getString(stringIDToTypeID('name'));
        var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
        var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
        var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
        selectedLayers = [[theName, theIdentifier, theseBounds]]
        } catch (e) {};
        };
    // reset;
        app.preferences.rulerUnits = originalRulerUnits;
        return selectedLayers;
    };
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){ 
    add = undefined ? add = false:add 
    var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID("Lyr "), id);
        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){
    alert(e.message); 
    }
    };
////// duplicate layer (id, xOffset, yOffset, theXScale, theYScale, theAngle) //////
function layerDuplicateOffsetScaleRotate (theIdentifier, xOffset, yOffset, theXScale, theYScale, theAngle, copy) {
    selectLayerByID(theIdentifier, false);
        var desc23 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref2 = new ActionReference();
            ref2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
        desc23.putReference( idnull, ref2 );
        desc23.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
        var idOfst = charIDToTypeID( "Ofst" );
            var desc24 = new ActionDescriptor();
            var idPxl = charIDToTypeID( "#Pxl" );
            desc24.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
            desc24.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
        desc23.putObject( idOfst, idOfst, desc24 );
        var idPrc = charIDToTypeID( "#Prc" );
        desc23.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theXScale );
        desc23.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theYScale );
        desc23.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), theAngle );
        desc23.putEnumerated( charIDToTypeID( "Intr" ), charIDToTypeID( "Intp" ), stringIDToTypeID( "bicubicAutomatic" ) );
        if (copy == true) {desc23.putBoolean( charIDToTypeID( "Cpy " ), true )};
    executeAction( charIDToTypeID( "Trnf" ), desc23, DialogModes.NO );
    };

3 replies

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
February 3, 2022

I would recommend converting all the Layers to Smart Objects (if they are not already) before transforming. 

Does this help? 

// scale all selected layers to pixeldimensions;
// 2022, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
    var theW = 400;
    var theH = 200;
    var theLayers = collectSelectedLayersBounds ();
// transform;
    for (var x = 0; x < theLayers.length; x++) {
        var horScale = theW / (theLayers[x][2][2] - theLayers[x][2][0]) * 100;
        var verScale = theH / (theLayers[x][2][3] - theLayers[x][2][1]) * 100;
        layerDuplicateOffsetScaleRotate (theLayers[x][1], 0, 0, horScale, verScale, 0, true)   
    };
// reselect layers;
    selectLayerByID(theLayers[0][1], false);
    for (var y = 1; y < theLayers.length; y++) {selectLayerByID(theLayers[y][1], true)}
    };
////// collect bounds of selected layers //////
function collectSelectedLayersBounds () {
    // set to pixels;
        var originalRulerUnits = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;
    // get selected layers;
        var selectedLayers = new Array;
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
        var desc = executeActionGet(ref);
        if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
        else {var theAdd = 1};
        if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
        desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
        var c = desc.count;
        var selectedLayers = new Array();
        // run through selected layers;
        for(var i=0;i<c;i++){
        var theIndex = desc.getReference( i ).getIndex()+theAdd;
        // get id for solid color layers;
        try {
        var ref = new ActionReference();
        ref.putIndex( charIDToTypeID("Lyr "), theIndex ); 
        var layerDesc = executeActionGet(ref);
        var theName = layerDesc.getString(stringIDToTypeID('name'));
        var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
        var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
        var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
        selectedLayers.push([theName, theIdentifier, theseBounds]);
        } catch (e) {};
        };
        // if only one:
        }else{
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
        var layerDesc = executeActionGet(ref);
        try {
        var theName = layerDesc.getString(stringIDToTypeID('name'));
        var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
        var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
        var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
        selectedLayers = [[theName, theIdentifier, theseBounds]]
        } catch (e) {};
        };
    // reset;
        app.preferences.rulerUnits = originalRulerUnits;
        return selectedLayers;
    };
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){ 
    add = undefined ? add = false:add 
    var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID("Lyr "), id);
        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){
    alert(e.message); 
    }
    };
////// duplicate layer (id, xOffset, yOffset, theXScale, theYScale, theAngle) //////
function layerDuplicateOffsetScaleRotate (theIdentifier, xOffset, yOffset, theXScale, theYScale, theAngle, copy) {
    selectLayerByID(theIdentifier, false);
        var desc23 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref2 = new ActionReference();
            ref2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
        desc23.putReference( idnull, ref2 );
        desc23.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
        var idOfst = charIDToTypeID( "Ofst" );
            var desc24 = new ActionDescriptor();
            var idPxl = charIDToTypeID( "#Pxl" );
            desc24.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
            desc24.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
        desc23.putObject( idOfst, idOfst, desc24 );
        var idPrc = charIDToTypeID( "#Prc" );
        desc23.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theXScale );
        desc23.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theYScale );
        desc23.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), theAngle );
        desc23.putEnumerated( charIDToTypeID( "Intr" ), charIDToTypeID( "Intp" ), stringIDToTypeID( "bicubicAutomatic" ) );
        if (copy == true) {desc23.putBoolean( charIDToTypeID( "Cpy " ), true )};
    executeAction( charIDToTypeID( "Trnf" ), desc23, DialogModes.NO );
    };
Mohamed Hameed21513110
Inspiring
February 3, 2022

@c.pfaffenbichler @Stephen Marsh 

thank you very much all
Your code is really nice and good

Stephen Marsh
Community Expert
Community Expert
February 2, 2022
Mohamed Hameed21513110
Inspiring
February 3, 2022

@Stephen Marsh 

very nice sir but i'm sorry
This code converts the layers of the Smart Object and then Restrizelayer

I want the specific layer code I need to perform more than one action on these specific layers
for example
- Change fonts for selected layers
- Make a specific filter on the selected layers, etc.
Thank you very much for your interest, appreciation and outstanding work

Stephen Marsh
Community Expert
Community Expert
February 3, 2022

You didn't mention that!

 

So, is it just that you need code to apply a specific function* to multiple selected layers?

 

* The function could be anything, you were just using resize as one example of many possible operations that need to be performed to multiple selected layers?

c.pfaffenbichler
Community Expert
Community Expert
February 2, 2022

The function getSelectedLayers seems antiquated, I suspect it precedes some changes that made evaluating the selected Layers easier. 

 


when implemented on more than one layer, it gives a very different result from what I want

What is that result and how does it differ from your expectations? 

Mohamed Hameed21513110
Inspiring
February 2, 2022

@c.pfaffenbichler 

 


The function getSelectedLayers seems antiquated, I suspect it precedes some changes that made evaluating the selected Layers easier. 



What can be changed in the code?

 

I attached a picture showing what is required