Skip to main content
Mohamed Hameed21513110
Inspiring
January 5, 2022
Answered

repeats the layer , Sort Layer and Auto Arrange by position

  • January 5, 2022
  • 4 replies
  • 1697 views

welcome everybody
- First: I want a code that repeats the layer according to a certain number.

Second: I have a number of layers that I want to sort by position
I have attached a picture of exactly what I want
Thanks for the interest

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

A very simple for loop to repeat the active layer by a variable amount of times in my script here:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/creating-duplicate-images-for-multi-packs-on-online-marketplaces-within-photoshop/td-p/12511723

4 replies

Legend
January 5, 2022

I have a code that I wrote for a completely different task (automatic align and fit selected layers to frames), but after a slight modification it can solve your problem.

Input state:

1. The layers to be sorted and the frames must go in the same order.

2. Layers and frames must be marked with the same label color

3. Before starting work, the layers that need to be placed in frames must be selected.

 

 

#target photoshop

var lr = new AM('layer'),
    doc = new AM('document'),
    previousLayer = new AM('layer', 'backwardEnum');

activeDocument.suspendHistory('Align and fit subject', 'alignAndFit()')
function alignAndFit() { app.doProgress('', 'main ()') }

function main() {

    var subjects = [],
        subjectColors = {},
        subjectIds = {},
        frames = [],
        frameColors = {},
        targetIds = doc.getProperty('targetLayersIDs');

    app.updateProgress(1, 4)

    for (var i = 0; i < targetIds.count; i++) {
        var subject = lr.descToObject(lr.getProperty('bounds', false, (id = targetIds.getReference(i).getIdentifier('layerID'))))

        with (subject) {
            subject.width = right - left
            subject.height = bottom - top
            subject.center = { y: top + height / 2, x: left + width / 2 }
            subject.vertical = bottom - top > right - left
            subject.ratio = width / height
        }

        subject.color = lr.getProperty('color')[1]
        subject.id = id

        if (!subjectColors[subject.color]) subjectColors[subject.color] = []
        subjectColors[subject.color].push(id)
        subjectIds[id] = subject.color
        subjects.push(subject)

        if (subject.color == 'none') break;
    }

    subjects.sort(function (a, b) { return a.ratio > b.ratio ? 1 : -1 })

    if (subjects.length > 1) {
        var from = doc.getProperty('hasBackgroundLayer') ? 0 : 1,
            len = doc.getProperty('numberOfLayers');

        for (var i = from; i <= len; i++) {
            if (lr.getProperty('layerSection', false, i, true)[1] == 'layerSectionEnd') continue;
            if (subjectIds[id = lr.getProperty('layerID', false, i, true)]) continue;
            var color = lr.getProperty('color', false, i, true)[1];
            if (color != 'none') {
                if (!frameColors[color]) frameColors[color] = []
                frameColors[color].push(id)
            }
        }
    } else {
        frameColors[color = previousLayer.getProperty('color')[1]] = []
        frameColors[color].push(previousLayer.getProperty('layerID'))
    }
    for (var a in subjectColors) {
        if (frameColors[a]) {
            frames = frames.concat(frameColors[a])
        }
    }

    app.updateProgress(2, 4)

    for (var i = 0; i < frames.length; i++) {
        var frame = lr.descToObject(lr.getProperty('bounds', false, frames[i]))

        with (frame) {
            frame.height = bottom - top
            frame.width = right - left
            frame.center = { y: top + height / 2, x: left + width / 2 }
            frame.vertical = bottom - top > right - left
            frame.ratio = width / height
        }

        frame.color = lr.getProperty('color', false, frames[i])[1]
        frame.id = lr.getProperty('layerID', false, frames[i])
        frame.processed = false
        frames[i] = frame
    }

    frames.sort(function (a, b) { return a.ratio > b.ratio ? 1 : -1 })

    app.updateProgress(3, 4)

    var offset = doc.getProperty('hasBackgroundLayer') ? 1 : 0
    for (var i = 0; i < subjects.length; i++) {
        app.changeProgressText('Align layer: ' + lr.getProperty('name', false, subjects[i].id))
        for (var x = 0; x < frames.length; x++) {
            if (frames[x].processed) continue;
            if (frames[x].color != subjects[i].color) continue;

            doc.moveLayer(lr.getProperty('itemIndex', false, subjects[i].id) - offset, lr.getProperty('itemIndex', false, frames[x].id) >= lr.getProperty('count', false, frames[x].id) ? lr.getProperty('count', false, frames[x].id) - 1 : lr.getProperty('itemIndex', false, frames[x].id))

            lr.selectLayer(subjects[i].id)
            if (!lr.getProperty('group', false, subjects[i].id)) lr.groupCurrentLayer()
            alignLayer(subjects[i], frames[x])
            frames[x].processed = true
            break;
        }
    }

}

function alignLayer(subject, frame) {
    var dH = frame.center.x - subject.center.x,
        dV = frame.center.y - subject.center.y,
        sW = frame.width / subject.width * 100,
        sH = frame.height / subject.height * 100,
        scale = sW > sH ? sW : sH

    lr.transform(dH, dV, scale, subject.center.x, subject.center.y)
}

function AM(target, order) {
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID;

    target = s2t(target)

    this.getProperty = function (property, descMode, id, idxMode) {
        property = s2t(property);
        (r = new ActionReference()).putProperty(s2t('property'), property);
        id != undefined ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id)) :
            r.putEnumerated(target, s2t('ordinal'), order ? s2t(order) : s2t('targetEnum'));
        return descMode ? executeActionGet(r) : getDescValue(executeActionGet(r), property)
    }

    this.descToObject = function (d) {
        var o = {}
        for (var i = 0; i < d.count; i++) {
            var k = d.getKey(i)
            o[t2s(k)] = getDescValue(d, k)
        }
        return o
    }

    this.selectLayer = function (id, add) {
        add = (add == undefined) ? add = false : add;
        (r = new ActionReference()).putIdentifier(s2t('layer'), id);
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        if (add) { d.putEnumerated(s2t('selectionModifier'), s2t('selectionModifierType'), s2t('addToSelection')) }
        d.putBoolean(s2t('makeVisible'), false)
        executeAction(s2t('select'), d, DialogModes.NO)
    }

    this.moveLayer = function (from, to) {
        (r = new ActionReference()).putIndex(s2t('layer'), from);
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        (r1 = new ActionReference()).putIndex(s2t('layer'), to);
        d.putReference(s2t('to'), r1);
        executeAction(s2t('move'), d, DialogModes.NO);
    }

    this.groupCurrentLayer = function () {
        (r = new ActionReference()).putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        executeAction(s2t('groupEvent'), d, DialogModes.NO);
    }

    this.transform = function (dX, dY, scale, x, y) {
        (r = new ActionReference()).putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        d.putEnumerated(s2t('freeTransformCenterState'), s2t('quadCenterState'), s2t('QCSIndependent'));
        ((d1 = new ActionDescriptor())).putUnitDouble(s2t('horizontal'), s2t('pixelsUnit'), x);
        d1.putUnitDouble(s2t('vertical'), s2t('pixelsUnit'), y);
        d.putObject(s2t('position'), s2t('paint'), d1);
        (d2 = new ActionDescriptor()).putUnitDouble(s2t('horizontal'), s2t('pixelsUnit'), dX);
        d2.putUnitDouble(s2t('vertical'), s2t('pixelsUnit'), dY);
        d.putObject(s2t('offset'), s2t('offset'), d2);
        d.putUnitDouble(s2t('width'), s2t('percentUnit'), scale);
        d.putUnitDouble(s2t('height'), s2t('percentUnit'), scale);
        d.putEnumerated(s2t('interfaceIconFrameDimmed'), s2t('interpolationType'), s2t('bicubic'));
        executeAction(s2t('transform'), d, DialogModes.NO);
    }

    function getDescValue(d, p) {
        switch (d.getType(p)) {
            case DescValueType.OBJECTTYPE: return (d.getObjectValue(p));
            case DescValueType.LISTTYPE: return d.getList(p);
            case DescValueType.REFERENCETYPE: return d.getReference(p);
            case DescValueType.BOOLEANTYPE: return d.getBoolean(p);
            case DescValueType.STRINGTYPE: return d.getString(p);
            case DescValueType.INTEGERTYPE: return d.getInteger(p);
            case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(p);
            case DescValueType.DOUBLETYPE: return d.getDouble(p);
            case DescValueType.ALIASTYPE: return d.getPath(p);
            case DescValueType.CLASSTYPE: return d.getClass(p);
            case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(p));
            case DescValueType.ENUMERATEDTYPE: return [t2s(d.getEnumerationType(p)), t2s(d.getEnumerationValue(p))];
            default: break;
        };
    }
}

 

 

Stephen Marsh
Community Expert
Community Expert
January 5, 2022

A good alternative option is create a template, then use one of JJMack's scripts to populate the template with the images.

 

http://www.mouseprints.net/old/dpr/PhotoCollageToolkit.html

 

Mohamed Hameed21513110
Inspiring
January 5, 2022

@Stephen Marsh @Kukurykus @jazz-y 

Well, I will suggest an idea, but the implementation I don't know how to do
What if we select and name the layers, choose layer number 1, and move it to the first design from the top left, then repeat the same movement next to the position of the first layer, then the second, then the third and so on
Is this possible or not?
If it was difficult, well, thank you for your concern and effort

Kukurykus
Legend
January 5, 2022

Don't use our names this way, unless you see anyone not attending here is willing to help.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
January 5, 2022
Mohamed Hameed21513110
Inspiring
January 5, 2022

@Stephen Marsh 

The code is really nice, but I don't want the layers to be merged after it's repeated

 

Is it possible to use this symbol on selected or existing layers

not the layer that has been duplicated?

Like the picture attached to this article

Stephen Marsh
Community Expert
Community Expert
January 5, 2022

1) Your post reads as two separate questions. 

 

You wrote:

"First: I want a code that repeats the layer according to a certain number."

 

I was referring to one specific "for" loop function to repeat an active layer N number of times, not the entire script. All the function does is duplicate the layer from a variable number input.

 

Specifically:

 

var count = 5;
    for (var i = 0; i < count; i++) {
        doc.activeLayer.duplicate();
    }

 

What you do after that is up to you.

 

2) Your second question has many different approaches, depending on the structure of your layers. This is impossible to comment on from a screenshot. This is covered in the link provided by jazz-y to another forum post.

 

You would be best posting a link to the layered PSD so that the layer structure can be examined from a programming viewpoint (not a visual one).

 

Mohamed Hameed21513110
Inspiring
January 5, 2022

Legend
January 5, 2022
Kukurykus
Legend
January 5, 2022

Loll I thought of the same thread, but couldn't find it.