Skip to main content
blasta20445892
Known Participant
July 14, 2019
Answered

Swap Layers In Exact Position And Size

  • July 14, 2019
  • 3 replies
  • 9317 views

Hi,

I have some code which swap layer in exact position but not in size,

// swapPosition-mirror.jsx - Adobe Photoshop Script

// Version: 0.2.1

// Author: Anton Lyubushkin (nvkz.nemo@gmail.com)

// Website: http://lyubushkin.pro/

// ============================================================================

// Installation:

// 1. Place script in:

//    PC(32):  C:\Program Files (x86)\Adobe\Adobe Photoshop CC#\Presets\Scripts\

//    PC(64):  C:\Program Files\Adobe\Adobe Photoshop CC# (64 Bit)\Presets\Scripts\

//    Mac:     <hard drive>/Applications/Adobe Photoshop CC#/Presets/Scripts/

// 2. Restart Photoshop

// 3. Choose File > Scripts > swapPosition-mirror

// ============================================================================

#target photoshop

app.bringToFront();

var doc = app.activeDocument;

var selectedLayers = getSelectedLayersIndex(doc);

if (selectedLayers.length == 2) {

    try {

        for (var i = 0; selectedLayers.length > i; i++) {

            var sLayers = new Array();

            for (var i = 0, l = selectedLayers.length; i < l; i++) {

                selectLayerByIndex(selectedLayers, false);

                if (app.activeDocument.activeLayer.typename == "LayerSet") {

                    executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);

                    var xpos = activeDocument.activeLayer.bounds[0];

                    var ytop = activeDocument.activeLayer.bounds[1];

                    var ybot = activeDocument.activeLayer.bounds[3];

                  var xleft = activeDocument.activeLayer.bounds[0]

                  var xright = activeDocument.activeLayer.bounds[2]

                    var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];

                    app.activeDocument.activeHistoryState = app.activeDocument.historyStates[app.activeDocument.historyStates.length - 2];

                    selectLayerByIndex(selectedLayers, false);

                } else {

                    var xpos = activeDocument.activeLayer.bounds[0];

                    var ytop = activeDocument.activeLayer.bounds[1];

                    var ybot = activeDocument.activeLayer.bounds[3];

                    var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];

                  var xleft = activeDocument.activeLayer.bounds[0]

                  var xright = activeDocument.activeLayer.bounds[2]

                }

                sLayers.push([activeDocument.activeLayer, xpos, ytop, ybot, height, xleft, xright]);

            }

            var delta0_y = 0;

            var delta1_y = 0;

            function differentHeight() {

                if (sLayers[1][4] != sLayers[0][4]) {

                    return true

                } else {

                    return false

                }

            }

            function returnSmaller() {

                if (sLayers[1][4] < sLayers[0][4]) {

                    return [sLayers[1][0], sLayers[0][0]]

                } else {

                    return [sLayers[0][0], sLayers[1][0]]

                }

            }

            if (sLayers[1][2] != sLayers[0][2]) {

                if (differentHeight() == true) {

                    var smallLyr = returnSmaller();

                    if (smallLyr[0].bounds[1] < smallLyr[1].bounds[1] || smallLyr[0].bounds[3] > smallLyr[1].bounds[3]) {

                        delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2])));

                        delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2])));

                    } else {

                        delta0_y = 0;

                        delta1_y = 0;

                    }

                } else {

                    delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2])));

                    delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2])));

                }

            }

         

          if (sLayers[0][5] < sLayers[1][5] && sLayers[0][6] < sLayers[1][6]) {

            var delta0_x = sLayers[1][6] - sLayers[0][6];

            var delta1_x = sLayers[0][5] - sLayers[1][5];

          } else {

            var delta0_x = sLayers[1][5] - sLayers[0][5];

            var delta1_x = sLayers[0][6] - sLayers[1][6];

          }

           

            sLayers[0][0].translate(delta0_x, delta0_y);

            sLayers[1][0].translate(delta1_x, delta1_y);

            for (var i = 0, l = selectedLayers.length; i < l; i++) {

                selectLayerByIndex(selectedLayers, true);

            }

        }

    } catch (e) {

        alert("Oops! These layers cannot be repositioned");

    }

}

function getSelectedLayersIndex(doc) {

    var selectedLayers = [];

    var ref = new ActionReference();

    ref.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt'));

    var desc = executeActionGet(ref);

    if (desc.hasKey(sTID('targetLayers'))) {

        desc = desc.getList(sTID('targetLayers'));

        var c = desc.count;

        for (var i = 0; i < c; i++) {

            try {

                doc.backgroundLayer;

                selectedLayers.push(desc.getReference(i).getIndex());

            } catch (e) {

                selectedLayers.push(desc.getReference(i).getIndex() + 1);

            }

        }

    } else {

        var ref = new ActionReference();

        ref.putProperty(cTID('Prpr'), cTID('ItmI'));

        ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));

        try {

            doc.backgroundLayer;

            selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')) - 1);

        } catch (e) {

            selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')));

        }

    }

    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) {}

}

function cTID(s) {return app.charIDToTypeID(s);}

function sTID(s) {return app.stringIDToTypeID(s);}

What i want is swap layer position and same size, like this:

Can anyone help with this?

Thank you.

SuperMerlin

This topic has been closed for replies.
Correct answer Chuck Uebele

You can try this. It will only resize base on width.

    // swapPosition-mirror.jsx - Adobe Photoshop Script 

    // Version: 0.2.1 

    // Author: Anton Lyubushkin (nvkz.nemo@gmail.com

    // Website: http://lyubushkin.pro/ 

    // ============================================================================ 

    // Installation: 

    // 1. Place script in: 

    //    PC(32):  C:\Program Files (x86)\Adobe\Adobe Photoshop CC#\Presets\Scripts\ 

    //    PC(64):  C:\Program Files\Adobe\Adobe Photoshop CC# (64 Bit)\Presets\Scripts\ 

    //    Mac:     <hard drive>/Applications/Adobe Photoshop CC#/Presets/Scripts/ 

    // 2. Restart Photoshop 

    // 3. Choose File > Scripts > swapPosition-mirror 

    // ============================================================================ 

    

    #target photoshop 

     

    app.bringToFront(); 

     

    var doc = app.activeDocument; 

    var selectedLayers = getSelectedLayersIndex(doc); 

     

    if (selectedLayers.length == 2) { 

        try { 

            for (var i = 0; selectedLayers.length > i; i++) { 

     

                var sLayers = new Array(); 

                for (var i = 0, l = selectedLayers.length; i < l; i++) { 

                    selectLayerByIndex(selectedLayers, false); 

                    if (app.activeDocument.activeLayer.typename == "LayerSet") { 

                        executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO); 

                        var xpos = activeDocument.activeLayer.bounds[0]; 

                        var ytop = activeDocument.activeLayer.bounds[1]; 

                        var ybot = activeDocument.activeLayer.bounds[3]; 

                      var xleft = activeDocument.activeLayer.bounds[0] 

                      var xright = activeDocument.activeLayer.bounds[2] 

                        var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1]; 

                        app.activeDocument.activeHistoryState = app.activeDocument.historyStates[app.activeDocument.historyStates.length - 2]; 

                        selectLayerByIndex(selectedLayers, false); 

                    } else { 

                        var xpos = activeDocument.activeLayer.bounds[0]; 

                        var ytop = activeDocument.activeLayer.bounds[1]; 

                        var ybot = activeDocument.activeLayer.bounds[3]; 

                        var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1]; 

                      var xleft = activeDocument.activeLayer.bounds[0] 

                      var xright = activeDocument.activeLayer.bounds[2] 

                    } 

     

                    sLayers.push([activeDocument.activeLayer, xpos, ytop, ybot, height, xleft, xright]); 

                } 

     

                var delta0_y = 0; 

                var delta1_y = 0; 

     

                function differentHeight() { 

                    if (sLayers[1][4] != sLayers[0][4]) { 

                        return true 

                    } else { 

                        return false 

                    } 

                } 

     

                function returnSmaller() { 

                    if (sLayers[1][4] < sLayers[0][4]) { 

                        return [sLayers[1][0], sLayers[0][0]] 

                    } else { 

                        return [sLayers[0][0], sLayers[1][0]] 

                    } 

                } 

     

                if (sLayers[1][2] != sLayers[0][2]) { 

                    if (differentHeight() == true) { 

                        var smallLyr = returnSmaller(); 

                        if (smallLyr[0].bounds[1] < smallLyr[1].bounds[1] || smallLyr[0].bounds[3] > smallLyr[1].bounds[3]) { 

                            delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))); 

                            delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))); 

                        } else { 

                            delta0_y = 0; 

                            delta1_y = 0; 

                        } 

                    } else { 

                        delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))); 

                        delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))); 

                    } 

                } 

               

              if (sLayers[0][5] < sLayers[1][5] && sLayers[0][6] < sLayers[1][6]) { 

                var delta0_x = sLayers[1][6] - sLayers[0][6]; 

                var delta1_x = sLayers[0][5] - sLayers[1][5]; 

              } else { 

                var delta0_x = sLayers[1][5] - sLayers[0][5]; 

                var delta1_x = sLayers[0][6] - sLayers[1][6]; 

              } 

                 

     

                sLayers[0][0].translate(delta0_x, delta0_y); 

                sLayers[1][0].translate(delta1_x, delta1_y); 

               

                ///////////////////////////////////////////////////////

                var width1 = sLayers[0][0].bounds[2] - sLayers[0][0].bounds[0]; 

                var width2 = sLayers[1][0].bounds[2] - sLayers[1][0].bounds[0]; 

               

                var ratio1 = width1/width2;

                var ratio2 = width2/width1;

                var layer1= doc.activeLayer = sLayers[0][0]

                layer1.resize (100*ratio2, 100* ratio2)

              

                var layer2= doc.activeLayer = sLayers[1][0]

                layer2.resize (100*ratio1, 100* ratio1)

                ///////////////////////////////////////////////////////

     

                for (var i = 0, l = selectedLayers.length; i < l; i++) { 

                    selectLayerByIndex(selectedLayers, true); 

                } 

            } 

        } catch (e) { 

            alert("Oops! These layers cannot be repositioned"); 

        } 

    } 

     

    function getSelectedLayersIndex(doc) { 

        var selectedLayers = []; 

        var ref = new ActionReference(); 

        ref.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt')); 

        var desc = executeActionGet(ref); 

        if (desc.hasKey(sTID('targetLayers'))) { 

            desc = desc.getList(sTID('targetLayers')); 

            var c = desc.count; 

            for (var i = 0; i < c; i++) { 

                try { 

                    doc.backgroundLayer; 

                    selectedLayers.push(desc.getReference(i).getIndex()); 

                } catch (e) { 

                    selectedLayers.push(desc.getReference(i).getIndex() + 1); 

                } 

            } 

        } else { 

            var ref = new ActionReference(); 

            ref.putProperty(cTID('Prpr'), cTID('ItmI')); 

            ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt')); 

            try { 

                doc.backgroundLayer; 

                selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')) - 1); 

            } catch (e) { 

                selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI'))); 

            } 

        } 

        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) {} 

    } 

     

    function cTID(s) {return app.charIDToTypeID(s);} 

    function sTID(s) {return app.stringIDToTypeID(s);} 

3 replies

Chuck Uebele
Community Expert
Community Expert
July 25, 2019

JJ is correct in his explanation of the difficulty is organizing the layers. You might want to try this script. It's one I wrote a while ago that sorts layers based on their size, so smaller layers don't get hidden by larger layers.

var docRef = activeDocument

var sizeArray = new Array()

for(var i=0;i<docRef.layers.length;i++){

    docRef.activeLayer = docRef.layers

    var area = (Number(docRef.activeLayer.bounds[2]) -Number(docRef.activeLayer.bounds[0]))*(Number(docRef.activeLayer.bounds[3]) -Number(docRef.activeLayer.bounds[1]))

    var area2 = area + 1000000000000

    sizeArray = area2+':'+docRef.activeLayer.name

  

    sizeArray.sort()

   

    }

var layLen = docRef.layers.length

for(var j=0;j<sizeArray.length;j++){

    var layName = sizeArray.split(':')[1];

   

    docRef.activeLayer = docRef.layers.getByName (layName)

    docRef.activeLayer.move(docRef.layers[layLen-1],ElementPlacement.PLACEAFTER)

    }

July 25, 2019

Thanks chuck for the script, but i get error when using the script, i using PS cc 2017

Chuck Uebele
Community Expert
Community Expert
July 25, 2019

The reason you're getting the error is that you have a background layer. I made this script for my use, and didn't have a bg layer. It might work okay if you change the PLACEAFTER to PLACEBEFORE. However, that might reverse the layer order.

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
July 15, 2019

You can try this. It will only resize base on width.

    // swapPosition-mirror.jsx - Adobe Photoshop Script 

    // Version: 0.2.1 

    // Author: Anton Lyubushkin (nvkz.nemo@gmail.com

    // Website: http://lyubushkin.pro/ 

    // ============================================================================ 

    // Installation: 

    // 1. Place script in: 

    //    PC(32):  C:\Program Files (x86)\Adobe\Adobe Photoshop CC#\Presets\Scripts\ 

    //    PC(64):  C:\Program Files\Adobe\Adobe Photoshop CC# (64 Bit)\Presets\Scripts\ 

    //    Mac:     <hard drive>/Applications/Adobe Photoshop CC#/Presets/Scripts/ 

    // 2. Restart Photoshop 

    // 3. Choose File > Scripts > swapPosition-mirror 

    // ============================================================================ 

    

    #target photoshop 

     

    app.bringToFront(); 

     

    var doc = app.activeDocument; 

    var selectedLayers = getSelectedLayersIndex(doc); 

     

    if (selectedLayers.length == 2) { 

        try { 

            for (var i = 0; selectedLayers.length > i; i++) { 

     

                var sLayers = new Array(); 

                for (var i = 0, l = selectedLayers.length; i < l; i++) { 

                    selectLayerByIndex(selectedLayers, false); 

                    if (app.activeDocument.activeLayer.typename == "LayerSet") { 

                        executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO); 

                        var xpos = activeDocument.activeLayer.bounds[0]; 

                        var ytop = activeDocument.activeLayer.bounds[1]; 

                        var ybot = activeDocument.activeLayer.bounds[3]; 

                      var xleft = activeDocument.activeLayer.bounds[0] 

                      var xright = activeDocument.activeLayer.bounds[2] 

                        var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1]; 

                        app.activeDocument.activeHistoryState = app.activeDocument.historyStates[app.activeDocument.historyStates.length - 2]; 

                        selectLayerByIndex(selectedLayers, false); 

                    } else { 

                        var xpos = activeDocument.activeLayer.bounds[0]; 

                        var ytop = activeDocument.activeLayer.bounds[1]; 

                        var ybot = activeDocument.activeLayer.bounds[3]; 

                        var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1]; 

                      var xleft = activeDocument.activeLayer.bounds[0] 

                      var xright = activeDocument.activeLayer.bounds[2] 

                    } 

     

                    sLayers.push([activeDocument.activeLayer, xpos, ytop, ybot, height, xleft, xright]); 

                } 

     

                var delta0_y = 0; 

                var delta1_y = 0; 

     

                function differentHeight() { 

                    if (sLayers[1][4] != sLayers[0][4]) { 

                        return true 

                    } else { 

                        return false 

                    } 

                } 

     

                function returnSmaller() { 

                    if (sLayers[1][4] < sLayers[0][4]) { 

                        return [sLayers[1][0], sLayers[0][0]] 

                    } else { 

                        return [sLayers[0][0], sLayers[1][0]] 

                    } 

                } 

     

                if (sLayers[1][2] != sLayers[0][2]) { 

                    if (differentHeight() == true) { 

                        var smallLyr = returnSmaller(); 

                        if (smallLyr[0].bounds[1] < smallLyr[1].bounds[1] || smallLyr[0].bounds[3] > smallLyr[1].bounds[3]) { 

                            delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))); 

                            delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))); 

                        } else { 

                            delta0_y = 0; 

                            delta1_y = 0; 

                        } 

                    } else { 

                        delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))); 

                        delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))); 

                    } 

                } 

               

              if (sLayers[0][5] < sLayers[1][5] && sLayers[0][6] < sLayers[1][6]) { 

                var delta0_x = sLayers[1][6] - sLayers[0][6]; 

                var delta1_x = sLayers[0][5] - sLayers[1][5]; 

              } else { 

                var delta0_x = sLayers[1][5] - sLayers[0][5]; 

                var delta1_x = sLayers[0][6] - sLayers[1][6]; 

              } 

                 

     

                sLayers[0][0].translate(delta0_x, delta0_y); 

                sLayers[1][0].translate(delta1_x, delta1_y); 

               

                ///////////////////////////////////////////////////////

                var width1 = sLayers[0][0].bounds[2] - sLayers[0][0].bounds[0]; 

                var width2 = sLayers[1][0].bounds[2] - sLayers[1][0].bounds[0]; 

               

                var ratio1 = width1/width2;

                var ratio2 = width2/width1;

                var layer1= doc.activeLayer = sLayers[0][0]

                layer1.resize (100*ratio2, 100* ratio2)

              

                var layer2= doc.activeLayer = sLayers[1][0]

                layer2.resize (100*ratio1, 100* ratio1)

                ///////////////////////////////////////////////////////

     

                for (var i = 0, l = selectedLayers.length; i < l; i++) { 

                    selectLayerByIndex(selectedLayers, true); 

                } 

            } 

        } catch (e) { 

            alert("Oops! These layers cannot be repositioned"); 

        } 

    } 

     

    function getSelectedLayersIndex(doc) { 

        var selectedLayers = []; 

        var ref = new ActionReference(); 

        ref.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt')); 

        var desc = executeActionGet(ref); 

        if (desc.hasKey(sTID('targetLayers'))) { 

            desc = desc.getList(sTID('targetLayers')); 

            var c = desc.count; 

            for (var i = 0; i < c; i++) { 

                try { 

                    doc.backgroundLayer; 

                    selectedLayers.push(desc.getReference(i).getIndex()); 

                } catch (e) { 

                    selectedLayers.push(desc.getReference(i).getIndex() + 1); 

                } 

            } 

        } else { 

            var ref = new ActionReference(); 

            ref.putProperty(cTID('Prpr'), cTID('ItmI')); 

            ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt')); 

            try { 

                doc.backgroundLayer; 

                selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')) - 1); 

            } catch (e) { 

                selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI'))); 

            } 

        } 

        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) {} 

    } 

     

    function cTID(s) {return app.charIDToTypeID(s);} 

    function sTID(s) {return app.stringIDToTypeID(s);} 

Geppetto Luis
Legend
July 16, 2019

Chuck Uebele

A very interesting script

when I use it I have a problem the images exchanged move to the left or right

how to avoid this problem.

before

1.png

after

2.png

JJMack
Community Expert
Community Expert
July 17, 2019

You could put a warning that 2 levels must be selected to work.


It would not be a warning the script could state that two layers are required for operation. change line 12 from

if (selectedLayers.length == 2) {

}

to

if (selectedLayers.length != 2) alert("Target the two layers you want swap positions"");

else {

}

The script will fail if you target a Background layer as one of the two layers for they do not support transparency.  Convert the background to a normal or smart object layer first.

JJMack
JJMack
Community Expert
Community Expert
July 15, 2019

The two layer you show have 1:1 aspect ratio  so what you want to do may be easy to do.  However,  If the two layers do not have the same aspect ratio you have a big problem. Do you want to distort or crop the layers which aspect ratios do  you want the resize layer to have. What kind of layer do you want to be able to process? For example smart object layer have associated transforn that size, position and can distort the layer.   They may be particularly hard to handle.,

JJMack
blasta20445892
Known Participant
July 15, 2019

What I want is of course all kind of layers ,

but if it can't be all kind of layers,

what kinds of layers can be used to suit the purpose I want?

then what should be added to the above script?

Thanks JJMack

Chuck Uebele
Community Expert
Community Expert
July 15, 2019

Just get the sizes of both layers, the calculate the ratio to size in each direction (one layer size to the other and the other to the first), then add a resize line of code for each layer.