Skip to main content
Envelope of Awesome
Known Participant
January 18, 2022
Answered

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

  • January 18, 2022
  • 2 replies
  • 2811 views

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;

 

Correct answer 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 replies

jazz-yCorrect answer
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 19, 2022

Hmmm It seems to be what I want.... but I can't wrap my head around why it's not working.... 
If i paste the code I got from the listener and run it... I just get the error: 
"stringIDToTypeID is not a function"

If I then make it a function.. The script runs... but nothing happens. 
I think I must be missing something?

// =======================================================
var idhistoryStateChanged = stringIDToTypeID( "historyStateChanged" );
    var desc298 = new ActionDescriptor();
    var idDocI = charIDToTypeID( "DocI" );
    desc298.putInteger( idDocI, 251 );
    var idIdnt = charIDToTypeID( "Idnt" );
    desc298.putInteger( idIdnt, 278 );
    var idNm = charIDToTypeID( "Nm  " );
    desc298.putString( idNm, """Set Shape Layer Stroke""" );
    var idhasEnglish = stringIDToTypeID( "hasEnglish" );
    desc298.putBoolean( idhasEnglish, true );
    var idItmI = charIDToTypeID( "ItmI" );
    desc298.putInteger( idItmI, 11 );
executeAction( idhistoryStateChanged, desc298, DialogModes.NO );

// =======================================================
var idsetd = charIDToTypeID( "setd" );
    var desc299 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref16 = new ActionReference();
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref16.putEnumerated( idcontentLayer, idOrdn, idTrgt );
    desc299.putReference( idnull, ref16 );
    var idT = charIDToTypeID( "T   " );
        var desc300 = new ActionDescriptor();
        var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
            var desc301 = new ActionDescriptor();
            var idstrokeStyleLineWidth = stringIDToTypeID( "strokeStyleLineWidth" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc301.putUnitDouble( idstrokeStyleLineWidth, idPxl, 5.200000 );
            var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
            desc301.putInteger( idstrokeStyleVersion, 2 );
            var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
            desc301.putBoolean( idstrokeEnabled, true );
        var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
        desc300.putObject( idstrokeStyle, idstrokeStyle, desc301 );
    var idshapeStyle = stringIDToTypeID( "shapeStyle" );
    desc299.putObject( idT, idshapeStyle, desc300 );
executeAction( idsetd, desc299, DialogModes.NO );


 

Envelope of Awesome
Known Participant
January 19, 2022

WAIT! I worked it out..... I'm an idiot.. 
I just wasn't pointing to photoshop....... 

I can successfully change the width of the selected layer. 

But I can't find an easy solution to cycle through ALL the layers ... (even the nested ones)

 

Please help! 🥰