用于复制和重命名图层的Photoshop脚本

时间:2017-09-27 17:43:54

标签: scripting photoshop jsx photoshop-script

在创建一个可以自动完成我在Photoshop上开始处理新图片时所做的所有不同任务的脚本时,我遇到了以下问题。

我想在这些组中创建不同的组和不同的层。在此之前一切都很顺利:

#target photoshop

app.bringToFront();

var doc = app.activeDocument;

newCurve();

var clippingHelpLayerLight = doc.activeLayer;
clippingHelpLayerLight.blendMode = BlendMode.SCREEN;
clippingHelpLayerLight.name = "Clipping Help Layer - Light";
clippingHelpLayerLight.visible = false;
clippingHelpLayerLight.duplicate();

var clippingHelpLayerLighter = doc.activeLayer;
clippingHelpLayerLighter.name = "Clipping Help Layer - Lighter";
clippingHelpLayerLighter.visible = false;

function newCurve() {
    var c_ADJ_LAYER = charIDToTypeID("AdjL");
    var c_ADJUSTMENT = charIDToTypeID("Adjs");
    var c_CHANNEL = charIDToTypeID("Chnl");
    var c_COMPOSITE = charIDToTypeID("Cmps");
    var c_CURVE = charIDToTypeID("Crv ");
    var c_CURVE_A = charIDToTypeID("CrvA");
    var c_CURVES = charIDToTypeID("Crvs");
    var c_HORIZONTAL = charIDToTypeID("Hrzn");
    var c_MAKE = charIDToTypeID("Mk  ");
    var c_NULL = charIDToTypeID("null");
    var c_POINT = charIDToTypeID("Pnt ");
    var c_TYPE = charIDToTypeID("Type");
    var c_USING = charIDToTypeID("Usng");
    var c_VERTICAL = charIDToTypeID("Vrtc");

    var d_CURVES_LAYER = new ActionDescriptor();
    // Contains all the information necessary to perform the "MAKE" action
    var r_CLASS = new ActionReference();
    r_CLASS.putClass(c_ADJ_LAYER);
    d_CURVES_LAYER.putReference(c_NULL, r_CLASS);
    // Class of make action is of an ajdustment layer
    var d_TYPE_CURVES = new ActionDescriptor();
    // Contains all the information about all the curves
    var d_CHANNEL_CURVES = new ActionDescriptor();
    var l_CHANNEL_CURVES = new ActionList();
    // Contains a list of channel curves
    var d_CHANNEL_CURVE = new ActionDescriptor();
    // Information for 1 channel curve
    var r_CHANNEL = new ActionReference();
    r_CHANNEL.putEnumerated(c_CHANNEL, c_CHANNEL, c_COMPOSITE);
    // This curve is for the composite channel - VARIES
    d_CHANNEL_CURVE.putReference(c_CHANNEL, r_CHANNEL);
    // Contains the point list
    var l_POINTS = new ActionList();
    // List of points for this channel - LENGTH VARIES
    var d_POINT = new ActionDescriptor();
    // One point on the curve, has INPUT and OUTPUT value
    d_POINT.putDouble(c_HORIZONTAL, 0.000000);
    d_POINT.putDouble(c_VERTICAL, 0.000000);
    l_POINTS.putObject(c_POINT, d_POINT);
    //var d_POINT3 = new ActionDescriptor();
    d_POINT.putDouble(c_HORIZONTAL, 255.000000);
    d_POINT.putDouble(c_VERTICAL, 255.000000);
    l_POINTS.putObject(c_POINT, d_POINT);
    // Made the list of points
    d_CHANNEL_CURVE.putList(c_CURVE, l_POINTS);
    // Now have a list of points for a specific channel
    l_CHANNEL_CURVES.putObject(c_CURVE_A, d_CHANNEL_CURVE);
    // Add to the list of channel curves
    d_CHANNEL_CURVES.putList(c_ADJUSTMENT, l_CHANNEL_CURVES);
    // All the channel curves are inside here
    d_TYPE_CURVES.putObject(c_TYPE, c_CURVES, d_CHANNEL_CURVES);
    // .....
    d_CURVES_LAYER.putObject(c_USING, c_ADJ_LAYER, d_TYPE_CURVES);
    // package the curves and definition of the adjustment layer type
    executeAction(c_MAKE, d_CURVES_LAYER, DialogModes.NO);
}

我实际上想创建一个名为&#34的第一层;剪切帮助图层 - 灯光",混合模式:屏幕并将其关闭。然后复制它,将新图层的名称更改为"剪切帮助图层 - 打火机"并将其关闭。

像这样:Screenshot of what I would like to do

它确实创建了2层,但第一层有"拷贝"在它的名字的末尾,它保持打开状态。

Screenshot of the actual result

为什么?

我无法理解为什么它没有按预期工作,也无法修复它。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我相信您遇到的问题与doc.activeLayer有关。复制“剪切帮助图层 - 光”后,脚本似乎不会更改doc.activeLayer指向的内容,因此当您尝试将其分配给clippingHelpLayerLighter时,您将指向未定义的图层。虽然我不确切知道幕后发生了什么,但我相信这会解决你的问题:

#target photoshop

app.bringToFront();

var doc = app.documents.add( 4, 4 );

doc = app.activeDocument;

var clippingHelpLayerLight = doc.activeLayer;
clippingHelpLayerLight.blendMode = BlendMode.SCREEN;
clippingHelpLayerLight.name = "Clipping Help Layer - Light";
clippingHelpLayerLight.visible = false;
clippingHelpLayerLight.duplicate();

doc.activeLayer = doc.layers[ "Clipping Help Layer - Light copy" ];
doc.activeLayer.name = "Clipping Help Layer - Lighter";
doc.activeLayer.visible = false;

//I am not sure if you need this pointer to be called upon later in your 
//code. If you do not, just leave this line out.
var clippingHelpLayerLighter = doc.activeLayer;

希望这有帮助!如果您有任何疑问,请告诉我,我绝不是专家,但我经常使用脚本。