创建动作以将x轴上的图层移动到所选图层的最宽宽度吗?

时间:2019-05-25 02:56:21

标签: photoshop photoshop-script

我想做一个可变的动作,根据那些选定图层之一的最宽宽度来分隔一系列图层。 4个精灵分别为100、100、100、200像素宽。它在x轴上将所有图层分开200像素,使图像的总宽度为800像素。

如果所有层都相同,我就能轻松地做到这一点,但不能使其具有可变性。不知道是否有任何photoshop向导愿意花我时间来制作脚本,但这会非常有帮助!

1 个答案:

答案 0 :(得分:1)

我会做这样的事情。这假定没有组,没有背景层,各层是普通层,并且所有文档层都已使用。前后(最大150px):

enter image description here

如果不需要它们更改Y的位置,请在第13行将0 - layersInfo[i].top更改为0

function main()
{
    var doc = activeDocument,
        layers = doc.layers, //getting all top layers
        layersInfo = getInfo().reverse(), //getting layers info and reversing the array because DOM indexes are different order than AM indexes
        elWidth = getWidestElement(layersInfo); // getting widest element

    doc.resizeCanvas(elWidth * layers.length, doc.height.as("px"), AnchorPosition.TOPLEFT); // resizing canvas size to new width: widest element * number of elements

    for (var i = 0; i < layers.length; i++) // for every top layer...
    {
        doc.activeLayer = layers[i]; // selecting the layer
        layers[i].translate(i * elWidth - layersInfo[i].left, 0 - layersInfo[i].top) // moving it to top left corner of each block
    }

    function getInfo()
    {
        var layers = 1,
            lyrs = [];
        while (true)
        {
            ref = new ActionReference();
            ref.putIndex(charIDToTypeID('Lyr '), layers);
            try
            {
                var desc = executeActionGet(ref);
            }
            catch (err)
            {
                break;
            }

            var lyr = {},
                bounds = desc.getObjectValue(stringIDToTypeID("bounds"));;

            lyr.top = bounds.getDouble(stringIDToTypeID("top"));
            lyr.left = bounds.getDouble(stringIDToTypeID("left"));
            lyr.width = bounds.getDouble(stringIDToTypeID("width"));

            lyrs.push(lyr)
            layers++;
        }
        return lyrs
    }; // end of getInfo()

    function getWidestElement(layers)
    {
        var curWidth = 0;
        for (var i = 0; i < layers.length; i++)
        {
            if (layers[i].width > curWidth) curWidth = layers[i].width;
        }
        return curWidth
    }; // end of getWidestElement()


}

var curUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
try
{
    app.activeDocument.suspendHistory("temp", "main()");
}
catch (e)
{
    alert(e + '\nLine: ' + e.line)
}
app.preferences.rulerUnits = curUnits;