Layer Transformation in a For Loop

Yeah. just kind of long.


async function transformHandle() {
    if (doc.activeLayers.length === 1) {
        async function resize() {
            return await batchPlay(transform(doc.activeLayers[0]), {});
        }
        core.executeAsModal(resize, {"commandName": "Scale One Layer"});
    } else {
        for (let i = 0; i < doc.activeLayers.length; i++) {
            async function resizeAll() {
                await batchPlay(transform(doc.activeLayers[i], i), {});
            }
            await core.executeAsModal(resizeAll, {"commandName": "Scale Multiple Layers"});
        }
    }
}

Here’s the transform in a different module


const transform = (el) => { 

    let {width, height} = calcDim(el);
    let widthPercent = (300 * 100) / Math.abs(width);
    let heightPercent = widthPercent;

    return [{
            _obj: "transform",
            _target: [
                {
                    _ref: "layer",
                    _enum: "ordinal",
                    _value: "targetEnum"
                }
            ],
            freeTransformCenterState: {
                _enum: "quadCenterState",
                _value: "QCSAverage"
            },
            width: {
                _unit: "percentUnit",
                _value: widthPercent
            },
            height: {
                _unit: "percentUnit",
                _value: heightPercent
            },
            interfaceIconFrameDimmed: {
                _enum: "interpolationType",
                _value: "bicubic"
            },
            _options: {
                dialogOptions: "dontDisplay"
            }
        }]
}

And the calc function (in a different module)


function calcDim(lay) {
    return {
        "width": lay.bounds.right - lay.bounds.left,
        "height": lay.bounds.top - lay.bounds.bottom
    }
}