Skip to main content
Inspiring
February 14, 2026
Answered

Need a script or plugin that resizes all selected layers to a specific pixel size. My failed attempt is included.

  • February 14, 2026
  • 7 replies
  • 102 views

I have a ton of shape layers that I’ve placed in a Photoshop document, and they are all different sizes. I want to be able to select all of my shape layers and run a script or use a plugin that will resize them all to specific pixel dimensions (maintaining aspect ratio).

This my first attempt:
 

// Scale Selected Layers Script
if (app.documents.length > 0) {
var targetWidthInPixels = prompt("Enter the desired width/height value in Pixels", "126", "Batch Layer Resize");
if (targetWidthInPixels !== null && targetWidthInPixels !== "") {
var doc = app.activeDocument;
var selectedLayers = getSelectedLayers();
var targetWidth = targetWidthInPixels; // Define desired width in pixels

// Ensure units are pixels for accurate measurement
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

for (var i = 0; i < selectedLayers.length; i++) {
// Get current layer
var layer = selectedLayers[i];

// Get current bounds
var bounds = layer.bounds;
var w = bounds[2] - bounds[0];

// Calculate scaling percentage
var scale = (targetWidth / w) * 100;

// Resize layer from center
layer.resize(scale, scale, AnchorPosition.MIDDLECENTER);
}
}
}

// Restore original units
preferences.rulerUnits = originalRulerUnits;

// Function to get selected layers
function getSelectedLayers() {
var su = stringIDToTypeID("select"),
desc = new ActionDescriptor(),
ref = new ActionReference();
ref.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
desc.putReference(stringIDToTypeID("null"), ref);
executeAction(su, desc, DialogModes.NO);
var targetLayers = app.activeDocument.activeLayers;
return targetLayers;
}

But when I run it I get the following error in a dialog box:
 

Error 21: undefined is not an object.
Line: 13
=> for (vari = 0;i < selectedLayerslength; i++) {

 

Error


I think the getSelectedLayers function isn’t working properly. So I tried again with a different function to get the selected layers that I found on here:
 

// Scale Selected Layers Script

if (app.documents.length > 0) {
var targetWidthInPixels = prompt("Enter the desired width/height value in Pixels", "126", "Batch Layer Resize");
if (targetWidthInPixels !== null && targetWidthInPixels !== "") {

var doc = app.activeDocument;
var selectedLayers = SelectedLayers();
var targetWidth = targetWidthInPixels; // Define desired width in pixels

// Ensure units are pixels for accurate measurement
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

for (var i = 0; i < selectedLayers.length; i++) {
// Get current layer
var layer = selectedLayers[i];

// Get current bounds
var bounds = layer.bounds;
var w = bounds[2] - bounds[0];

// Calculate scaling percentage
var scale = (targetWidth / w) * 100;

// Resize layer from center
layer.resize(scale, scale, AnchorPosition.MIDDLECENTER);
}
}
}


// Restore original units
preferences.rulerUnits = originalRulerUnits;


function SelectedLayers() {
try{
var ActLay = app.activeDocument.activeLayer;
ActLay.allLocked = true; //lock all selected layers
var LayerStack = app.activeDocument.artLayers.length;
var selLayers = new Array();
for(var i = 0; i <= LayerStack - 1; i++) {
ActLay = app.activeDocument.layers[i]
if (ActLay.allLocked == true) {selLayers.push(app.activeDocument.layers[i]);} // push all locked layers into an array
}
for (i = 0; i <= LayerStack - 1; i++) {
var LAY = app.activeDocument.layers[i];
LAY.allLocked = false; // unlock all locked Layers
}
return selLayers;
}
catch(e){/*alert(e);*/}
}


But this script silently fails and also locks all my selected layers.

Can someone here help me out? I am open to a plugin suggestion that does this, or if someone has a working script that can accomplish this I would be forever grateful!

Thanks for any help.
 

    Correct answer c.pfaffenbichler

    You can test this code for the changing of selected solid color layers.  

    // change selected solid color layers to a hexvalue;
    // 2026, use it at your own risk;
    if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    myDocument.suspendHistory("stuff", "main()");
    };
    ////// the function //////
    function main () {
    var theHexValue = prompt("enter the hexvalue", app.foregroundColor.rgb.hexValue, "enter hexvalue");
    //var theHexValue = "\#d61c55"
    if (theHexValue != false) {
    var theLayers = getSelectedSolidColorLayersIdentifier();
    for (var m = 0; m < theLayers.length; m++) {
    changeSolidColor (theLayers[m], undefined, undefined, undefined, theHexValue);
    }
    } else {alert ("problem with the hexvalue")}
    };
    //////////////////////////////////////////
    ////// based on code by paul mr //////
    function getSelectedSolidColorLayersIdentifier(){
    var selectedLayers = new Array;
    var ref = new ActionReference();
    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();
    // run through selected layers;
    for(var i=0;i<c;i++){
    try{activeDocument.backgroundLayer;
    var theIndex = desc.getReference( i ).getIndex();
    }catch(e){var theIndex = desc.getReference( i ).getIndex()+1 };
    // get id for solid color layers;
    try {
    var ref = new ActionReference();
    ref.putIndex( charIDToTypeID("Lyr "), theIndex );
    var layerDesc = executeActionGet(ref);
    var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
    var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
    var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
    selectedLayers.push( theIdentifier );
    } catch (e) {};
    };
    // if only one:
    }else{
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var layerDesc = executeActionGet(ref);
    try {
    var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
    var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
    var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
    selectedLayers = [theIdentifier]
    } catch (e) {};
    };
    return selectedLayers;
    };
    ////// change color of solid color layer //////
    function changeSolidColor (theIdentifier, theR, theG, theB, hexValue) {
    if (hexValue) {
    var theRgb = hex2rgb(hexValue);
    var theR = theRgb[0];
    var theG = theRgb[1];
    var theB = theRgb[2];
    };
    // =======================================================
    var idsetd = charIDToTypeID( "setd" );
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    var ref1 = new ActionReference();
    ref1.putIdentifier ( stringIDToTypeID( "contentLayer" ), theIdentifier );
    desc4.putReference( idnull, ref1 );
    var idT = charIDToTypeID( "T " );
    var desc5 = new ActionDescriptor();
    var idClr = charIDToTypeID( "Clr " );
    var desc6 = new ActionDescriptor();
    var idRd = charIDToTypeID( "Rd " );
    desc6.putDouble( idRd, theR );
    var idGrn = charIDToTypeID( "Grn " );
    desc6.putDouble( idGrn, theG );
    var idBl = charIDToTypeID( "Bl " );
    desc6.putDouble( idBl, theB );
    var idRGBC = charIDToTypeID( "RGBC" );
    desc5.putObject( idClr, idRGBC, desc6 );
    var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
    desc4.putObject( idT, idsolidColorLayer, desc5 );
    executeAction( idsetd, desc4, DialogModes.NO );
    };
    ////// hex to rgb //////
    function hex2rgb (hex) {
    try {
    hex = hex.replace("\#", "");
    r = parseInt(hex.slice(0, 2), 16);
    g = parseInt(hex.slice(2, 4), 16);
    b = parseInt(hex.slice(4, 6), 16);
    return [r, g, b]
    } catch (e) {return false}
    };

     

    7 replies

    JR Boulay
    Community Expert
    Community Expert
    February 15, 2026

    You don't need to reinvent the wheel.
    You just have to enable the Generator in Preferences: Plug-ins
    (As I'm not sure about the wording in English, I'm including this screenshot so you know where to find this option).

     

     

    Acrobate du PDF, InDesigner et Photoshopographe
    c.pfaffenbichler
    Community Expert
    Community Expert
    February 15, 2026

    The op did not mention exporting the layers. 

    c.pfaffenbichler
    Community Expert
    c.pfaffenbichlerCommunity ExpertCorrect answer
    Community Expert
    February 15, 2026

    You can test this code for the changing of selected solid color layers.  

    // change selected solid color layers to a hexvalue;
    // 2026, use it at your own risk;
    if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    myDocument.suspendHistory("stuff", "main()");
    };
    ////// the function //////
    function main () {
    var theHexValue = prompt("enter the hexvalue", app.foregroundColor.rgb.hexValue, "enter hexvalue");
    //var theHexValue = "\#d61c55"
    if (theHexValue != false) {
    var theLayers = getSelectedSolidColorLayersIdentifier();
    for (var m = 0; m < theLayers.length; m++) {
    changeSolidColor (theLayers[m], undefined, undefined, undefined, theHexValue);
    }
    } else {alert ("problem with the hexvalue")}
    };
    //////////////////////////////////////////
    ////// based on code by paul mr //////
    function getSelectedSolidColorLayersIdentifier(){
    var selectedLayers = new Array;
    var ref = new ActionReference();
    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();
    // run through selected layers;
    for(var i=0;i<c;i++){
    try{activeDocument.backgroundLayer;
    var theIndex = desc.getReference( i ).getIndex();
    }catch(e){var theIndex = desc.getReference( i ).getIndex()+1 };
    // get id for solid color layers;
    try {
    var ref = new ActionReference();
    ref.putIndex( charIDToTypeID("Lyr "), theIndex );
    var layerDesc = executeActionGet(ref);
    var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
    var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
    var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
    selectedLayers.push( theIdentifier );
    } catch (e) {};
    };
    // if only one:
    }else{
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var layerDesc = executeActionGet(ref);
    try {
    var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
    var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
    var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
    selectedLayers = [theIdentifier]
    } catch (e) {};
    };
    return selectedLayers;
    };
    ////// change color of solid color layer //////
    function changeSolidColor (theIdentifier, theR, theG, theB, hexValue) {
    if (hexValue) {
    var theRgb = hex2rgb(hexValue);
    var theR = theRgb[0];
    var theG = theRgb[1];
    var theB = theRgb[2];
    };
    // =======================================================
    var idsetd = charIDToTypeID( "setd" );
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    var ref1 = new ActionReference();
    ref1.putIdentifier ( stringIDToTypeID( "contentLayer" ), theIdentifier );
    desc4.putReference( idnull, ref1 );
    var idT = charIDToTypeID( "T " );
    var desc5 = new ActionDescriptor();
    var idClr = charIDToTypeID( "Clr " );
    var desc6 = new ActionDescriptor();
    var idRd = charIDToTypeID( "Rd " );
    desc6.putDouble( idRd, theR );
    var idGrn = charIDToTypeID( "Grn " );
    desc6.putDouble( idGrn, theG );
    var idBl = charIDToTypeID( "Bl " );
    desc6.putDouble( idBl, theB );
    var idRGBC = charIDToTypeID( "RGBC" );
    desc5.putObject( idClr, idRGBC, desc6 );
    var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
    desc4.putObject( idT, idsolidColorLayer, desc5 );
    executeAction( idsetd, desc4, DialogModes.NO );
    };
    ////// hex to rgb //////
    function hex2rgb (hex) {
    try {
    hex = hex.replace("\#", "");
    r = parseInt(hex.slice(0, 2), 16);
    g = parseInt(hex.slice(2, 4), 16);
    b = parseInt(hex.slice(4, 6), 16);
    return [r, g, b]
    } catch (e) {return false}
    };

     

    c.pfaffenbichler
    Community Expert
    Community Expert
    February 14, 2026

    Maybe this thread can be of help with the Color-changing: 

     

    c.pfaffenbichler
    Community Expert
    Community Expert
    February 14, 2026

    Is there an easy way to create a derivative script that resizes selected layers by a percentage instead of a width/height pixel value?

    You can feed your percentage (from a dialog input for example) directly into the function layerDuplicateOffsetScaleRotate or into the lines 

            var horScale = theW / (theLayers[x][2][2] - theLayers[x][2][0]) * 100;
    var verScale = theH / (theLayers[x][2][3] - theLayers[x][2][1]) * 100;

     

    Also, maybe I should create a separate thread for this, but is there a possibility to create a script that changes the color of all selected shape layers to a different color (Hex value)?

    I would recommend creating a new thread. 

    The task seems perfectly Script-able but on a new thread someone else may be able to help you more quickly. 

    c.pfaffenbichler
    Community Expert
    Community Expert
    February 14, 2026

    If there is any way you can figure out how to apply the transformations directly to the selected layers instead of making copies and transforming those, it would be awesome.

    I don’t remember if the duplication of the Layers was a specific request back then but you can change the »copy«-parameter from »true« to »false« in the line that handles the transformation: 

    layerDuplicateOffsetScaleRotate(theLayers[x][1], 0, 0, horScale, verScale, 0, true);

     

    It’s great that you were able to otherwise adapt the Script to include a dialog to better meet your needs. 

    c.pfaffenbichler
    Community Expert
    Community Expert
    February 14, 2026

    Please check out the Script posted in this thread: 

    https://community.adobe.com/t5/photoshop-ecosystem-discussions/resize-selected-layers/m-p/12726182#M620620

    c.pfaffenbichler
    Community Expert
    Community Expert
    February 14, 2026

    I am afraid with Shape Layers the results may, due to anti-aliasing and the PathPoints respectively extremes of curves not lining up with the pixel grid perfectly, be one or two pixels off occasionally. 

    Are you willing to accept such eventualities? 

    Inspiring
    February 14, 2026

    Yes I am totally fine with this. Going to check the script you linked above.