• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
3

Change multiple layers scale size by script

Explorer ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

I wrote following photoshop script using chatGPT for change multiple layers scale size: 

 

// Function to resize the selected layers
function resizeSelectedLayers() {
    var doc = app.activeDocument;
    var selectedLayers = [];

    // Prompt user to select layers
    var selectedLayerIndices = prompt("Enter the indices of the layers you want to resize (comma-separated):", "");
    if (selectedLayerIndices === null || selectedLayerIndices === "") {
        alert("No layers selected.");
        return;
    }

    // Convert comma-separated indices to array
    var indicesArray = selectedLayerIndices.split(",");
    for (var i = 0; i < indicesArray.length; i++) {
        var index = parseInt(indicesArray[i]);
        if (!isNaN(index) && index >= 0 && index < doc.layers.length) {
            selectedLayers.push(doc.layers[index]);
        } else {
            alert("Invalid layer index: " + indicesArray[i]);
            return;
        }
    }

    // Check if there are selected layers
    if (selectedLayers.length > 0) {
        // Loop through each selected layer
        for (var j = 0; j < selectedLayers.length; j++) {
            var selectedLayer = selectedLayers[j];
            doc.activeLayer = selectedLayer;

            // Calculate scaling factor
            var scalePercentage = 76.39; // Percentage size
            var scaleFactor = scalePercentage / 100;

            // Get current dimensions
            var width = selectedLayer.bounds[2] - selectedLayer.bounds[0];
            var height = selectedLayer.bounds[3] - selectedLayer.bounds[1];

            // Calculate new dimensions with locked aspect ratio
            var newWidth = width * scaleFactor;
            var newHeight = height * scaleFactor;

            // Calculate the scaling ratio
            var ratio = newWidth / width;

            // Set up the transformation
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
            desc.putReference(charIDToTypeID('null'), ref);

            // Set transformation parameters
            desc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));
            desc.putUnitDouble(charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), 100 * ratio);
            desc.putUnitDouble(charIDToTypeID('Hght'), charIDToTypeID('#Prc'), 100 * ratio);
            desc.putEnumerated(charIDToTypeID('Intr'), charIDToTypeID('Intp'), charIDToTypeID('Bcbc'));

            // Execute transformation
            executeAction(charIDToTypeID('Trnf'), desc, DialogModes.NO);
        }
    } else {
        alert("Please select one or more layers.");
    }
}

// Check if there is an active document
if (app.documents.length > 0) {
    // Call the resizeSelectedLayers function
    resizeSelectedLayers();
} else {
    alert("No documents are open.");
}

 

 

The script above is functioning well, but it doesn't provide accurate results. For instance, it's expected to change both the width (W) and height (H) to a scale size of 76.39%. However, during testing, I noticed that the width changed to 76.92% and the height changed to 75.76%.

What could be causing this discrepancy in the script? It's worth noting that maintaining the aspect ratio is essential.

 

TOPICS
Actions and scripting , Windows

Views

226

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
community guidelines
Adobe
Community Expert ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

I tested with a 10K square pixel layer and all was good – 7639 px for both width and height.

 

If you are transforming a rectangular layer, then you may get some rounding depending on the original pixel values and how these are scaled via %.

 

Votes

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
community guidelines
Community Expert ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

You could also try an alternative script to see if you get the same or different result:

 

/*
Scale Selected Layers Independently v1.jsx
26th August 2022, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-do-i-scale-multiple-layers-while-keeping-them-anchored-in-place/td-p/13157052
*/

#target photoshop

////////// SCRIPT UI START //////////

/*
With thanks - https://scriptui.joonas.me
*/

// DIALOG
var theGUI = new Window("dialog");
    theGUI.text = "Scale Selected Layers Independently - v1.0";
    theGUI.orientation = "row";
    theGUI.alignChildren = ["center","top"];
    theGUI.spacing = 10;
    theGUI.margins = 20;

var scaleLabel = theGUI.add("statictext", undefined, undefined, {name: "scaleLabel"});
    scaleLabel.text = "Scale %";

// SCALE GROUP
var scaleGroup = theGUI.add("group", undefined, {name: "scaleGroup"});
    scaleGroup.orientation = "row";
    scaleGroup.alignChildren = ["left","center"];
    scaleGroup.spacing = 10;
    scaleGroup.margins = 0;

// SCALE WIDTH GROUP
var scaleWidthGroup = scaleGroup.add("group", undefined, {name: "scaleWidthGroup"});
    scaleWidthGroup.orientation = "column";
    scaleWidthGroup.alignChildren = ["left","center"];
    scaleWidthGroup.spacing = 10;
    scaleWidthGroup.margins = 0;

    var widthLabel = scaleWidthGroup.add("statictext", undefined, undefined, {name: "widthLabel"});
    widthLabel.text = "Width:";

    ////////// Use editnumber instead of edittext for numerical fields! //////////
    var widthField = scaleWidthGroup.add('editnumber {properties: {name: "widthField"}}');
        widthField.text = "76.39";
    widthField.preferredSize.width = 50;
   
    ////////// Preset the width field as active - Peter Kahrel, scriptUI for dummies //////////
    widthField.active = true;
    ////////// Initially use the width value for the height value //////////
    ////////// Callback: onChange - Peter Kahrel, scriptUI for dummies //////////
    widthField.onChange = function () {heightField.text = widthField.text}
    //////////

// SCALE HEIGHT GROUP
var scaleHeightGroup = scaleGroup.add("group", undefined, {name: "scaleHeightGroup"});
    scaleHeightGroup.orientation = "column";
    scaleHeightGroup.alignChildren = ["left","center"];
    scaleHeightGroup.spacing = 10;
    scaleHeightGroup.margins = 0;

var heightLabel = scaleHeightGroup.add("statictext", undefined, undefined, {name: "heightLabel"});
    heightLabel.text = "Height:";

////////// Use editnumber instead of edittext for numerical fields! //////////
var heightField = scaleHeightGroup.add('editnumber {properties: {name: "heightField"}}');
    heightField.text = "76.39";
    heightField.preferredSize.width = 50;

// DIVIDER 1
//var divOne = scaleGroup.add("panel", undefined, undefined, {name: "divOne"});
    //divOne.alignment = "fill";

// RADIO PARENT GROUP
var radioGroup = theGUI.add("group", undefined, {name: "radioGroup"});
    radioGroup.orientation = "row";
    radioGroup.alignChildren = ["left","center"];
    radioGroup.spacing = 10;
    radioGroup.margins = 0;

// RADIO GROUP LEFT
var radioGroupLeft = radioGroup.add("group", undefined, {name: "radioGroupLeft"});
    radioGroupLeft.orientation = "column";
    radioGroupLeft.alignChildren = ["left","center"];
    radioGroupLeft.spacing = 10;
    radioGroupLeft.margins = 0;

var topLeft = radioGroupLeft.add("radiobutton", undefined, undefined, {name: "topLeft"});
    topLeft.text = "TL";

var middleLeft = radioGroupLeft.add("radiobutton", undefined, undefined, {name: "middleLeft"});
    middleLeft.text = "ML";

var bottomLeft = radioGroupLeft.add("radiobutton", undefined, undefined, {name: "bottomLeft"});
    bottomLeft.text = "BL";

// RADIO GROUP CENTER
var radioGroupCenter = radioGroup.add("group", undefined, {name: "radioGroupCenter"});
    radioGroupCenter.orientation = "column";
    radioGroupCenter.alignChildren = ["left","center"];
    radioGroupCenter.spacing = 10;
    radioGroupCenter.margins = 0;

var topCenter = radioGroupCenter.add("radiobutton", undefined, undefined, {name: "topCenter"});
    topCenter.text = "TC";

var middleCenter = radioGroupCenter.add("radiobutton", undefined, undefined, {name: "middleCenter"});
    middleCenter.text = "MC";
    ////////// Preset the radio button as active - Peter Kahrel, scriptUI for dummies //////////
    middleCenter.value = true;
    //////////

var bottomCenter = radioGroupCenter.add("radiobutton", undefined, undefined, {name: "bottomCenter"});
    bottomCenter.text = "BC";

// RADIO GROUP RIGHT
var radioGroupRight = radioGroup.add("group", undefined, {name: "radioGroupRight"});
    radioGroupRight.orientation = "column";
    radioGroupRight.alignChildren = ["left","center"];
    radioGroupRight.spacing = 10;
    radioGroupRight.margins = 0;

    var topRight = radioGroupRight.add("radiobutton", undefined, undefined, {name: "topRight"});
        topRight.text = "TR";

    var middleRight = radioGroupRight.add("radiobutton", undefined, undefined, {name: "middleRight"});
        middleRight.text = "MR";

    var bottomRight = radioGroupRight.add("radiobutton", undefined, undefined, {name: "bottomRight"});
    bottomRight.text = "BR";
   
    ////////// Make multiple groups act as one group - Peter Kahrel, scriptUI for dummies //////////
    radioGroupLeft.addEventListener("click", function () {
        for (var i = 0; i < radioGroupCenter.children.length; i++)
            radioGroupCenter.children[i].value = false;
    });
    radioGroupLeft.addEventListener("click", function () {
        for (var i = 0; i < radioGroupRight.children.length; i++)
            radioGroupRight.children[i].value = false;
    });
    radioGroupCenter.addEventListener("click", function () {
        for (var i = 0; i < radioGroupLeft.children.length; i++)
            radioGroupLeft.children[i].value = false;
    });
    radioGroupCenter.addEventListener("click", function () {
        for (var i = 0; i < radioGroupRight.children.length; i++)
            radioGroupRight.children[i].value = false;
    });
    radioGroupRight.addEventListener("click", function () {
        for (var i = 0; i < radioGroupCenter.children.length; i++)
            radioGroupCenter.children[i].value = false;
    });
    radioGroupRight.addEventListener("click", function () {
        for (var i = 0; i < radioGroupLeft.children.length; i++)
            radioGroupLeft.children[i].value = false;
    });
    //////////

// DIVIDER 2
//var divTwo = theGUI.add("panel", undefined, undefined, {name: "divTwo"});
    //divTwo.alignment = "fill";

// BUTTON GROUP
var buttonGroup = theGUI.add("group", undefined, {name: "buttonGroup"});
    buttonGroup.orientation = "column";
    buttonGroup.alignChildren = ["left","center"];
    buttonGroup.spacing = 10;
    buttonGroup.margins = 0;

var okButton = buttonGroup.add("button", undefined, undefined, {name: "okButton"});
    okButton.text = "OK";
    okButton.alignment = ["fill","center"];

var cancelButton = buttonGroup.add("button", undefined, undefined, {name: "cancelButton"});
    cancelButton.text = "Cancel";
    cancelButton.alignment = ["fill", "center"];

// RENDER THE DIALOG
theGUI.show();

////////// SCRIPT UI END //////////


function main() {

            var s2t = stringIDToTypeID;
            (r = new ActionReference).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
            r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
            var lrs = executeActionGet(r).getList(p),
                sel = new ActionReference();

            for (var i = 0; i < lrs.count; i++) {
                sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
                (r = new ActionReference).putIdentifier(s2t('layer'), p);
                (d = new ActionDescriptor()).putReference(s2t("target"), r);
                executeAction(s2t('select'), d, DialogModes.NO);
               
                ////////// Link the radio buttons to the resize  //////////
                if (topLeft.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.TOPLEFT);
                }
                if (topCenter.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.TOPCENTER);
                }
                if (topRight.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.TOPRIGHT);
                }
                if (middleLeft.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.MIDDLELEFT);
                }
                if (middleCenter.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.MIDDLECENTER);
                }
                if (middleRight.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.MIDDLERIGHT);
                }
                if (bottomLeft.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.BOTTOMLEFT);
                }
                if (bottomCenter.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.BOTTOMCENTER);
                }
                if (bottomRight.value === true) {
                    activeDocument.activeLayer.resize(widthField.text, heightField.text, AnchorPosition.BOTTOMRIGHT);
                }
            }
}

activeDocument.suspendHistory("Scale Selected Layers Independently.jsx", "main()");

 

Votes

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
community guidelines
Explorer ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

tnq but it not working and i still have that problem!

Votes

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
community guidelines
Community Expert ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

Perhaps if you supplied detailed pertinent information...

Votes

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
community guidelines
People's Champ ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

I think it's just a Photoshop glitch. Transformation of selected smart objects fails. The screenshot (CS6) shows the transformation of two identical selected smart objects from 100% to 9% percent. It can be seen that the transformation boundary and the boundaries of the objects themselves do not coincide in this process.

rbin_0-1711988356337.png

 

Votes

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
community guidelines
Explorer ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

LATEST

oh no - photoshop team must fix this issue!

Votes

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
community guidelines