脚本合并复制Photoshop的图层

时间:2019-01-28 04:50:13

标签: javascript photoshop-script

我是不熟悉Photoshop脚本的人,似乎并不是所有的javascript都是一样的。我想做的是,我有一个如下的图层列表:

猫图片1.jpg

Cat pic 1.jpg复制

狗图片1.jpg

狗图片1.jpg复制

....

我要合并彼此的副本,因此将“ Cat pic 1.jpg”和“ Cat pic 1.jpg副本”合并,并从这两层中创建一层,即“ Cat pic 1.jpg”。

它们都是活动的,没有形状或其他任何东西,因此它只是一层。我大约有300层,每一层都是这样。每层都有一个副本,名称末尾有副本。我写了一个脚本,但是我不知道如何将两个图层合并在一起。我的代码不起作用。它是由试图按字母顺序对图层进行排序的人拼凑而成的。

基本上,我有一个图层列表,并且水平翻转了它,现在我需要将左半部分与右半部分合并。左边没有单词“ copy”。

任何人都非常感谢您的帮助!请放心忽略我对我的零信任的所有代码。

谢谢!

#target photoshop
    var layers = activeDocument.layers;
	var layersArray = [];
	var len = layers.length;
	
	// store all layers in an array
	for (var i = 0; i < len; i++) {
		layersArray.push(layers[i]);
	}

var layersOrig= [];
 for (var i = 0; i < len; i++) {
    var cond = myIndexOf(layersArray[i], "copy");
    if (cond === -1) {
       layersOrig.push(layersArray[i]); 
       delete layersArray[i];
    }
 }
    layersOrig.sort();
	// sort layer top to bottom
	layersArray.sort();
for (var i = 0; i < len; i++) {
    layersArray[i] = merge(layersOrig[i], layersArray[i];
 }
	for (i = 0; i < len; i++) {
		layersArray[i].move(layers[i], ElementPlacement.PLACEBEFORE);
	}

function myIndexOf(array, x){
	var n=-1, N=array.length;
	while (++n<N && array[n]!==x);
	return n<N ? n : -1;
	};

1 个答案:

答案 0 :(得分:0)

也许是这样吗?

function main()
{
    var layersInfo = getAllLayersNames(); //getting myself an object of layer names and IDs

    for (layerName in layersInfo)
    {
        deselectLayers(); //deselecting all layers first

        if (layersInfo[layerName].length == 1) continue; //if there's only one layer in the object: ignore it

        for (var i = 0; i < layersInfo[layerName].length; i++)
        {
            selectById(layersInfo[layerName][i], true); //selecting layers-clones by IDs
        }

        mergeDown(); //merge down selected layers

        activeDocument.activeLayer.name = layerName; //renaming the resulting layer to original layer name
    }

    /////////////////////////////////////////////////////////////////////////////////////
    // functions

    function getAllLayersNames()
    {
        var lyrs = {};
        try
        {
            activeDocument.backgroundLayer;
            var layers = 0
        }
        catch (e)
        {
            var layers = 1;
        };
        while (true)
        {
            ref = new ActionReference();
            ref.putIndex(charIDToTypeID('Lyr '), layers);
            try
            {
                var desc = executeActionGet(ref);
            }
            catch (err)
            {
                break;
            }

            var lyr = {};
            lyr.name = desc.getString(charIDToTypeID("Nm  "));
            var nameWithoutCopy = lyr.name.replace(/\scopy.*/, '');
            lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
            if (lyrs[nameWithoutCopy] == undefined) lyrs[nameWithoutCopy] = [lyr.id]
            else lyrs[nameWithoutCopy].push(lyr.id);
            layers++;
        }
        return lyrs
    };

    function selectById(id, add)
    {
        var desc1 = new ActionDescriptor();
        var ref1 = new ActionReference();
        ref1.putIdentifier(charIDToTypeID('Lyr '), id);
        desc1.putReference(charIDToTypeID('null'), ref1);
        if (add) desc1.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
        executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
    };

    function deselectLayers()
    {
        var desc60 = new ActionDescriptor();
        var ref30 = new ActionReference();
        ref30.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
        desc60.putReference(charIDToTypeID('null'), ref30);
        executeAction(stringIDToTypeID('selectNoLayers'), desc60, DialogModes.NO);
    };

    function mergeDown()
    {
        var desc11 = new ActionDescriptor();
        executeAction(charIDToTypeID('Mrg2'), desc11, DialogModes.NO);
    };

}
app.activeDocument.suspendHistory("rename all copies", "main()");

输入>结果:

enter image description here