Skip to main content
Inspiring
February 14, 2026
Question

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

  • February 14, 2026
  • 5 replies
  • 0 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.
 

    5 replies

    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.