Skip to main content
Envelope of Awesome
Known Participant
January 18, 2022
해결됨

Photoshop script to iterate through all layers and change shape stroke size!??!

  • January 18, 2022
  • 2 답변들
  • 2797 조회

Hello!
I'm trying to write a photoshop script that can change the size of strokes on all shape layers. 

When we increase the image size, none of the stroke widths scale with that change. This is what we are trying to fix. Is there a way to increase image size and have stroke width also scale?

 

It seems super simple and I can do it in Adobe Animate really easily, but not photoshop. 

 

So far I can get the stroke size of an active layer but not change it... (thanks to another great post)...

 

Please help!! 😮
 

 

var activeDoc = app.activeDocument;

var layers = activeDoc.layers;
var layerLength = layers.length;

//alert(layers.length);


var r = new ActionReference();    
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("AGMStrokeStyleInfo"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
//alert(executeActionGet(r).getObjectValue(stringIDToTypeID("AGMStrokeStyleInfo")).getUnitDoubleValue(stringIDToTypeID("strokeStyleLineWidth")));

var strokeSize = (executeActionGet(r).getObjectValue(stringIDToTypeID("AGMStrokeStyleInfo")).getUnitDoubleValue(stringIDToTypeID("strokeStyleLineWidth")));
alert(strokeSize);

strokeSize += 10;

 

최고의 답변: jazz-y

 

#target photoshop

var s2t = stringIDToTypeID,
    delta = 10;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p),
    lrs = [];

for (var i = 1; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).getInteger(p) == 4) {

        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        r.putIndex(s2t('layer'), i);
        lrs.push(executeActionGet(r).getInteger(p));
    }
}

if (lrs.length) {
    for (var i = 0; i < lrs.length; i++) {
        (r = new ActionReference()).putIdentifier(s2t('layer'), lrs[i]);
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('AGMStrokeStyleInfo'));
        r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
        var strokeSize = executeActionGet(r).getObjectValue(p).getUnitDoubleValue(s2t('strokeStyleLineWidth'));

        (r = new ActionReference()).putEnumerated(s2t("contentLayer"), s2t("ordinal"), s2t("targetEnum"));
        (d = new ActionDescriptor()).putReference(s2t("null"), r);
        (d1 = new ActionDescriptor()).putUnitDouble(s2t("strokeStyleLineWidth"), s2t("pixelsUnit"), strokeSize + delta);
        (d2 = new ActionDescriptor()).putObject(s2t("strokeStyle"), s2t("strokeStyle"), d1);
        d.putObject(s2t("to"), s2t("shapeStyle"), d2);
        executeAction(s2t("set"), d, DialogModes.NO);
    }
}

 

2 답변

jazz-y답변
Legend
January 19, 2022

 

#target photoshop

var s2t = stringIDToTypeID,
    delta = 10;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p),
    lrs = [];

for (var i = 1; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).getInteger(p) == 4) {

        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        r.putIndex(s2t('layer'), i);
        lrs.push(executeActionGet(r).getInteger(p));
    }
}

if (lrs.length) {
    for (var i = 0; i < lrs.length; i++) {
        (r = new ActionReference()).putIdentifier(s2t('layer'), lrs[i]);
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('AGMStrokeStyleInfo'));
        r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
        var strokeSize = executeActionGet(r).getObjectValue(p).getUnitDoubleValue(s2t('strokeStyleLineWidth'));

        (r = new ActionReference()).putEnumerated(s2t("contentLayer"), s2t("ordinal"), s2t("targetEnum"));
        (d = new ActionDescriptor()).putReference(s2t("null"), r);
        (d1 = new ActionDescriptor()).putUnitDouble(s2t("strokeStyleLineWidth"), s2t("pixelsUnit"), strokeSize + delta);
        (d2 = new ActionDescriptor()).putObject(s2t("strokeStyle"), s2t("strokeStyle"), d1);
        d.putObject(s2t("to"), s2t("shapeStyle"), d2);
        executeAction(s2t("set"), d, DialogModes.NO);
    }
}

 

Envelope of Awesome
Known Participant
January 19, 2022

WOO!
HIGH FIVE!
Thankyou so much for this. 

I ... am not familiar at all with this scripting style. AM code.

I shall have to look further into it. 

But thankyou thankyou thankyou! 🤩

Chuck Uebele
Community Expert
Community Expert
January 18, 2022

I would use scriptListener and record changing the stroke size, then make a function out of that code and replace the amount you changed the stroke size with a variable. 

Envelope of Awesome
Known Participant
January 18, 2022

Oooh thankyou Chuck!
Why have i not heard of this before!

I'll try it out!! 😁