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
  • 2797 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 24, 2022

HI Jazz-y!
I know you're probably super busy, but I seem to have run into a problem and there doesn't seem to be a place to find help. 

 

This is my script as I have it so far. 
I just want to increase or decrease the line width on all layers (if they have it)
But it gets stuck on these specific layers which don't SEEM to have any "strokeStyleLineWidth"

line 39 >>
var strokeSize = executeActionGet(r).getObjectValue(p).getUnitDoubleValue(s2t('strokeStyleLineWidth'));

it just gives me the error message.
"general photoshop error occurred. this functionality may not be available in this version of photoshop"

But if I give the layer a colour and then make it transparent again.... it works??
Is there a way to skip a layer if it's line colour is set to nothing?
e.g. 
If(linewdith colour == null ){move on} 

?

Please help you are a hero on these forums!

 





#target photoshop

var s2t = stringIDToTypeID,
//var newStrokeSize = 10;
newStrokeSize = prompt("Enter the % increase for linewidth","","input value here");

newStrokeSize = newStrokeSize /100;

(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 + (strokeSize*newStrokeSize));
        (d1 = new ActionDescriptor()).putUnitDouble(s2t("strokeStyleLineWidth"), s2t("pixelsUnit"), strokeSize = newStrokeSize);
        (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 24, 2022

I can't reproduce the error on the latest photoshop. Try this variant: I added a check if the stroke is enabled and additionally check if there is a key with its width.

/**Photoshop script to iterate through all layers and change shape stroke size!??!
 * https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-to-iterate-through-all-layers-and-change-shape-stroke-size/td-p/12689665
 */
#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 stroke = executeActionGet(r).getObjectValue(p);
        if (stroke.getBoolean(s2t('strokeEnabled'))) {
            var strokeSize = stroke.hasKey(p = s2t('strokeStyleLineWidth')) ? stroke.getUnitDoubleValue(p) : 0;
            (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);
        }
    }
}

jazz-y 😁

Thankyou so much!
I also updated Photoshop and it still doesn't work! 😢
Unforuntately it must be something to do with the specific PSD files I'm working with 🤔 🤔😕😕


The layer "c3 drop copy" is the problematic one. 

It gets stuck on line 32
var stroke = executeActionGet(r).getObjectValue(p);

It's like it can't find any value for the TypeID 
p = s2t('AGMStrokeStyleInfo')

In the layer "c3 drop copy"

As soon as you change the value of the stroke it works...
but we have thousands of layers in our PSDs... so that's not feasible. 

 

I tried declaring the var stroke = 10;
...to make sure it had a value... but no success. 😢


I added in a scriptlistener CHUNK (below)
and..... that seemed to over ride the error....
but changes ALL the layers.

This seems overly complicated for something so simple... 
Is there another way to access these functions through PS Javascript ?


Any genius insights you have would be most appreciated!

/**Photoshop script to iterate through all layers and change shape stroke size!??!
 * https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-to-iterate-through-all-layers-and-change-shape-stroke-size/td-p/12689665
 */
#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 idsetd = charIDToTypeID( "setd" );
    var desc398 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref18 = new ActionReference();
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref18.putEnumerated( idcontentLayer, idOrdn, idTrgt );
    desc398.putReference( idnull, ref18 );
    var idT = charIDToTypeID( "T   " );
        var desc399 = new ActionDescriptor();
        var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
            var desc400 = new ActionDescriptor();
            var idstrokeStyleContent = stringIDToTypeID( "strokeStyleContent" );
                var desc401 = new ActionDescriptor();
                var idClr = charIDToTypeID( "Clr " );
                    var desc402 = new ActionDescriptor();
                    var idRd = charIDToTypeID( "Rd  " );
                    desc402.putDouble( idRd, 158.011679 );
                    var idGrn = charIDToTypeID( "Grn " );
                    desc402.putDouble( idGrn, 255.000000 );
                    var idBl = charIDToTypeID( "Bl  " );
                    desc402.putDouble( idBl, 0.003891 );
                var idRGBC = charIDToTypeID( "RGBC" );
                desc401.putObject( idClr, idRGBC, desc402 );
            var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
            desc400.putObject( idstrokeStyleContent, idsolidColorLayer, desc401 );
            var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
            desc400.putInteger( idstrokeStyleVersion, 2 );
            var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
            desc400.putBoolean( idstrokeEnabled, true );
        var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
        desc399.putObject( idstrokeStyle, idstrokeStyle, desc400 );
    var idshapeStyle = stringIDToTypeID( "shapeStyle" );
    desc398.putObject( idT, idshapeStyle, desc399 );
executeAction( idsetd, desc398, DialogModes.NO );        
        
        
        
        
        
        




var stroke = executeActionGet(r).getObjectValue(p);
        if (stroke.getBoolean(s2t('strokeEnabled'))) {
            var strokeSize = stroke.hasKey(p = s2t('strokeStyleLineWidth')) ? stroke.getUnitDoubleValue(p) : 0;
            (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);
        }
    }
}




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!! 😁