Skip to main content
Mohamed Hameed21513110
Inspiring
November 27, 2022
Answered

Align the image layers laterally through the group layer

  • November 27, 2022
  • 1 reply
  • 4342 views

Hello everyone and wonderful forum experts

- Sometimes I divide a number of image layers into groups

- I want a script that will align images sideways, as shown in the attached image

This topic has been closed for replies.
Correct answer Stephen Marsh

@Mohamed Hameed21513110 

 

I am not 100% happy with the following script, however, it has taken longer to get to this point than I expected. I believe that it works as required, let me know how you go.

 

/*
Distribute Grouped Layer Stack Horizontally From Left to Right.jsx
v1.0, 28th November 2022, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/align-the-image-layers-laterally-through-the-group-layer/td-p/13375249
*/

#target photoshop

function main() {

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

        var setName = activeDocument.activeLayer.name;
        var count = activeDocument.activeLayer.layers.length;

        activeDocument.activeLayer = activeDocument.layerSets[setName].layers[count - 1];
        var lyrRight = activeDocument.activeLayer.bounds[2].value;

        for (var i = count - 1; i >= 0; i--) {
            selectForwardORBackwardLayer(false, "forwardEnum");
            activeDocument.activeLayer.translate((lyrRight));
            var lyrRight = activeDocument.activeLayer.bounds[2].value;
        }

        // It's a hack, however, it will have to do for now!
        align2SelectAll('AdLf');

        //activeDocument.revealAll();

    } else {
        alert("Please select a layer group containing the layers to distribute from left to right!");
    }
}
activeDocument.suspendHistory("Undo script...", "main()");


// Functions

function selectForwardORBackwardLayer(makeVisible, forwardORbackward) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();
    // "forwardEnum" or "backwardEnum"
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t(forwardORbackward));
    descriptor.putReference(s2t("null"), reference);
    // true or false
    descriptor.putBoolean(s2t("makeVisible"), makeVisible);
    list.putInteger(15);
    descriptor.putList(s2t("layerID"), list);
    executeAction(s2t("select"), descriptor, DialogModes.NO);
}

function align2SelectAll(method) {
   /* https://gist.github.com/MarshySwamp/df372e342ac87854ffe08e79cbdbcbb5 */
   
    /* 
    AdLf = Align Left
    AdRg = Align Right
    AdCH = Align Centre Horizontal
    AdTp = Align Top
    AdBt = Align Bottom
    AdCV = Align Centre Vertical
    */

    app.activeDocument.selection.selectAll();
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    desc.putReference(charIDToTypeID("null"), ref);
    desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
    try {
        executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
    } catch (e) {}
    app.activeDocument.selection.deselect();
}

 

 

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
November 28, 2022

@Mohamed Hameed21513110 

 

I am not 100% happy with the following script, however, it has taken longer to get to this point than I expected. I believe that it works as required, let me know how you go.

 

/*
Distribute Grouped Layer Stack Horizontally From Left to Right.jsx
v1.0, 28th November 2022, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/align-the-image-layers-laterally-through-the-group-layer/td-p/13375249
*/

#target photoshop

function main() {

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

        var setName = activeDocument.activeLayer.name;
        var count = activeDocument.activeLayer.layers.length;

        activeDocument.activeLayer = activeDocument.layerSets[setName].layers[count - 1];
        var lyrRight = activeDocument.activeLayer.bounds[2].value;

        for (var i = count - 1; i >= 0; i--) {
            selectForwardORBackwardLayer(false, "forwardEnum");
            activeDocument.activeLayer.translate((lyrRight));
            var lyrRight = activeDocument.activeLayer.bounds[2].value;
        }

        // It's a hack, however, it will have to do for now!
        align2SelectAll('AdLf');

        //activeDocument.revealAll();

    } else {
        alert("Please select a layer group containing the layers to distribute from left to right!");
    }
}
activeDocument.suspendHistory("Undo script...", "main()");


// Functions

function selectForwardORBackwardLayer(makeVisible, forwardORbackward) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();
    // "forwardEnum" or "backwardEnum"
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t(forwardORbackward));
    descriptor.putReference(s2t("null"), reference);
    // true or false
    descriptor.putBoolean(s2t("makeVisible"), makeVisible);
    list.putInteger(15);
    descriptor.putList(s2t("layerID"), list);
    executeAction(s2t("select"), descriptor, DialogModes.NO);
}

function align2SelectAll(method) {
   /* https://gist.github.com/MarshySwamp/df372e342ac87854ffe08e79cbdbcbb5 */
   
    /* 
    AdLf = Align Left
    AdRg = Align Right
    AdCH = Align Centre Horizontal
    AdTp = Align Top
    AdBt = Align Bottom
    AdCV = Align Centre Vertical
    */

    app.activeDocument.selection.selectAll();
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    desc.putReference(charIDToTypeID("null"), ref);
    desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
    try {
        executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
    } catch (e) {}
    app.activeDocument.selection.deselect();
}

 

 

Mohamed Hameed21513110
Inspiring
November 28, 2022

@Stephen Marsh 

Thank you very much

I can't find anything I can say to thank you for your amazing effort

I wish you success always and forever