Skip to main content
ChristianK-Fr
Inspiring
January 21, 2021
Answered

javascript : draw lines to path

  • January 21, 2021
  • 4 replies
  • 3125 views

Hi, 

I'd like to do this : on photoshop file, draw a line, then, with a script, automatically draw to the extremities a vertical line of 54px length (width 7 px.)

Is it possible to made a script for this ?

Thanks in advance, 

Christian

This topic has been closed for replies.
Correct answer Chuck Uebele

Try this script:

#target photoshop

var oldPrefs = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.POINTS
var doc = activeDocument;
var curLay = doc.activeLayer;
var curLayName = curLay.name
var res = doc.resolution;
var len54 = 54*(72/res);

var path1 = doc.pathItems.getByName(curLay.name + ' Shape Path');
var p1 = path1.subPathItems[0]
var pt1 = p1.pathPoints[0].anchor
var pt2 = p1.pathPoints[1].anchor

var totalPath = new Array();
var newPath = new Array();

newPath[0] = new PathPointInfo
newPath[0].kind = PointKind.CORNERPOINT;    

newPath[0].anchor = Array(pt1[0],pt1[1]+len54);
newPath[0].rightDirection = Array(pt1[0],pt1[1]+len54);
newPath[0].leftDirection = Array(pt1[0],pt1[1]+len54);

newPath[1] = new PathPointInfo
newPath[1].kind = PointKind.CORNERPOINT;    

newPath[1].anchor = Array(pt1[0],pt1[1]);
newPath[1].rightDirection = Array(pt1[0],pt1[1]);
newPath[1].leftDirection = Array(pt1[0],pt1[1]);

newPath[2] = new PathPointInfo
newPath[2].kind = PointKind.CORNERPOINT;    

newPath[2].anchor = Array(pt2[0],pt2[1]);
newPath[2].rightDirection = Array(pt2[0],pt2[1]);
newPath[2].leftDirection = Array(pt2[0],pt2[1]);

newPath[3] = new PathPointInfo
newPath[3].kind = PointKind.CORNERPOINT;    

newPath[3].anchor = Array(pt2[0],pt2[1]+len54);
newPath[3].rightDirection = Array(pt2[0],pt2[1]+len54);
newPath[3].leftDirection = Array(pt2[0],pt2[1]+len54);

totalPath[0]= new SubPathInfo();
totalPath[0].closed = false;
totalPath[0].operation = ShapeOperation.SHAPEADD


totalPath[0].entireSubPath = newPath;
   
var thePath = doc.pathItems.add('My Path ',totalPath);

convertPathtoShape();
setStroke ();
doc.activeLayer.name = curLayName;
curLay.remove();
thePath.remove();


app.preferences.rulerUnits = oldPrefs

function convertPathtoShape() {
	var d = new ActionDescriptor();
	var d2 = new ActionDescriptor();
	var d3 = new ActionDescriptor();
	var d4 = new ActionDescriptor();
	var r = new ActionReference();
	r.putClass( stringIDToTypeID( "contentLayer" ));
	d.putReference( charIDToTypeID( "null" ), r );
	d4.putDouble( charIDToTypeID( "Rd  " ), 255);
	d4.putDouble( charIDToTypeID( "Grn " ), 255);
	d4.putDouble( charIDToTypeID( "Bl  " ), 255);
	d3.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), d4 );
	d2.putObject( charIDToTypeID( "Type" ), stringIDToTypeID( "solidColorLayer" ), d3 );
	d.putObject( charIDToTypeID( "Usng" ), stringIDToTypeID( "contentLayer" ), d2 );
	executeAction( charIDToTypeID( "Mk  " ), d, DialogModes.NO );
}

function setStroke(){
    var idsetd = charIDToTypeID( "setd" );
        var desc3 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idcontentLayer = stringIDToTypeID( "contentLayer" );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref1.putEnumerated( idcontentLayer, idOrdn, idTrgt );
        desc3.putReference( idnull, ref1 );
        var idT = charIDToTypeID( "T   " );
            var desc4 = new ActionDescriptor();
            var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
                var desc5 = new ActionDescriptor();
                var idstrokeStyleContent = stringIDToTypeID( "strokeStyleContent" );
                    var desc6 = new ActionDescriptor();
                    var idClr = charIDToTypeID( "Clr " );
                        var desc7 = new ActionDescriptor();
                        var idCyn = charIDToTypeID( "Cyn " );
                        desc7.putDouble( idCyn, 74.970000 );
                        var idMgnt = charIDToTypeID( "Mgnt" );
                        desc7.putDouble( idMgnt, 67.920000 );
                        var idYlw = charIDToTypeID( "Ylw " );
                        desc7.putDouble( idYlw, 67.050000 );
                        var idBlck = charIDToTypeID( "Blck" );
                        desc7.putDouble( idBlck, 90.150000 );
                    var idCMYC = charIDToTypeID( "CMYC" );
                    desc6.putObject( idClr, idCMYC, desc7 );
                var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
                desc5.putObject( idstrokeStyleContent, idsolidColorLayer, desc6 );
                var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
                desc5.putInteger( idstrokeStyleVersion, 2 );
                var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
                desc5.putBoolean( idstrokeEnabled, true );
                var idfillEnabled = stringIDToTypeID( "fillEnabled" );
                desc5.putBoolean( idfillEnabled, false );
            var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
            desc4.putObject( idstrokeStyle, idstrokeStyle, desc5 );
        var idshapeStyle = stringIDToTypeID( "shapeStyle" );
        desc3.putObject( idT, idshapeStyle, desc4 );
    executeAction( idsetd, desc3, DialogModes.NO );

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

4 replies

Chuck Uebele
Community Expert
Community Expert
February 2, 2021

To have it use the center stroke, add this code to the very end of the script, before the last brace "}" in the function setStoke().

 

var idsetd = charIDToTypeID( "setd" );
    var desc36 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref14 = new ActionReference();
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref14.putEnumerated( idcontentLayer, idOrdn, idTrgt );
    desc36.putReference( idnull, ref14 );
    var idT = charIDToTypeID( "T   " );
        var desc37 = new ActionDescriptor();
        var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
            var desc38 = new ActionDescriptor();
            var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
            var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
            var idstrokeStyleAlignCenter = stringIDToTypeID( "strokeStyleAlignCenter" );
            desc38.putEnumerated( idstrokeStyleLineAlignment, idstrokeStyleLineAlignment, idstrokeStyleAlignCenter );
            var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
            desc38.putInteger( idstrokeStyleVersion, 2 );
            var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
            desc38.putBoolean( idstrokeEnabled, true );
        var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
        desc37.putObject( idstrokeStyle, idstrokeStyle, desc38 );
    var idshapeStyle = stringIDToTypeID( "shapeStyle" );
    desc36.putObject( idT, idshapeStyle, desc37 );
executeAction( idsetd, desc36, DialogModes.NO );
ChristianK-Fr
Inspiring
February 2, 2021

Hi Chuck, thx again for your help and prompt answer ! It works great ! 

Thanks and have a nice day !

ChristianK-Fr
Inspiring
February 3, 2021

If I could ask latest latest one more thing : what I could change in the script to have other angles : 

- horizontal left, or right

- vertical up or down, 

like on screenshot in red, green and violet

(always the same lenght 54 px)

I read your script and documentation, but I didn't find (didn't understand) how to write/modify to do this.

Thanks a lot, again.... :-))

ChristianK-Fr
Inspiring
February 1, 2021

Hi Chuck, 

I apologize but can you tell me how the script works ? I try to use it but have error line 11 , 

Have to draw a line or to made a path ? When I try both, have same error.... , 

Thanks again, 

Christian

Chuck Uebele
Community Expert
Community Expert
February 1, 2021

Use the pen tool to draw a line: just set two points.

ChristianK-Fr
Inspiring
February 1, 2021

Hi Chuck, 

I try it, but it's not working.....

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
January 30, 2021

Try this script:

#target photoshop

var oldPrefs = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.POINTS
var doc = activeDocument;
var curLay = doc.activeLayer;
var curLayName = curLay.name
var res = doc.resolution;
var len54 = 54*(72/res);

var path1 = doc.pathItems.getByName(curLay.name + ' Shape Path');
var p1 = path1.subPathItems[0]
var pt1 = p1.pathPoints[0].anchor
var pt2 = p1.pathPoints[1].anchor

var totalPath = new Array();
var newPath = new Array();

newPath[0] = new PathPointInfo
newPath[0].kind = PointKind.CORNERPOINT;    

newPath[0].anchor = Array(pt1[0],pt1[1]+len54);
newPath[0].rightDirection = Array(pt1[0],pt1[1]+len54);
newPath[0].leftDirection = Array(pt1[0],pt1[1]+len54);

newPath[1] = new PathPointInfo
newPath[1].kind = PointKind.CORNERPOINT;    

newPath[1].anchor = Array(pt1[0],pt1[1]);
newPath[1].rightDirection = Array(pt1[0],pt1[1]);
newPath[1].leftDirection = Array(pt1[0],pt1[1]);

newPath[2] = new PathPointInfo
newPath[2].kind = PointKind.CORNERPOINT;    

newPath[2].anchor = Array(pt2[0],pt2[1]);
newPath[2].rightDirection = Array(pt2[0],pt2[1]);
newPath[2].leftDirection = Array(pt2[0],pt2[1]);

newPath[3] = new PathPointInfo
newPath[3].kind = PointKind.CORNERPOINT;    

newPath[3].anchor = Array(pt2[0],pt2[1]+len54);
newPath[3].rightDirection = Array(pt2[0],pt2[1]+len54);
newPath[3].leftDirection = Array(pt2[0],pt2[1]+len54);

totalPath[0]= new SubPathInfo();
totalPath[0].closed = false;
totalPath[0].operation = ShapeOperation.SHAPEADD


totalPath[0].entireSubPath = newPath;
   
var thePath = doc.pathItems.add('My Path ',totalPath);

convertPathtoShape();
setStroke ();
doc.activeLayer.name = curLayName;
curLay.remove();
thePath.remove();


app.preferences.rulerUnits = oldPrefs

function convertPathtoShape() {
	var d = new ActionDescriptor();
	var d2 = new ActionDescriptor();
	var d3 = new ActionDescriptor();
	var d4 = new ActionDescriptor();
	var r = new ActionReference();
	r.putClass( stringIDToTypeID( "contentLayer" ));
	d.putReference( charIDToTypeID( "null" ), r );
	d4.putDouble( charIDToTypeID( "Rd  " ), 255);
	d4.putDouble( charIDToTypeID( "Grn " ), 255);
	d4.putDouble( charIDToTypeID( "Bl  " ), 255);
	d3.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), d4 );
	d2.putObject( charIDToTypeID( "Type" ), stringIDToTypeID( "solidColorLayer" ), d3 );
	d.putObject( charIDToTypeID( "Usng" ), stringIDToTypeID( "contentLayer" ), d2 );
	executeAction( charIDToTypeID( "Mk  " ), d, DialogModes.NO );
}

function setStroke(){
    var idsetd = charIDToTypeID( "setd" );
        var desc3 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idcontentLayer = stringIDToTypeID( "contentLayer" );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref1.putEnumerated( idcontentLayer, idOrdn, idTrgt );
        desc3.putReference( idnull, ref1 );
        var idT = charIDToTypeID( "T   " );
            var desc4 = new ActionDescriptor();
            var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
                var desc5 = new ActionDescriptor();
                var idstrokeStyleContent = stringIDToTypeID( "strokeStyleContent" );
                    var desc6 = new ActionDescriptor();
                    var idClr = charIDToTypeID( "Clr " );
                        var desc7 = new ActionDescriptor();
                        var idCyn = charIDToTypeID( "Cyn " );
                        desc7.putDouble( idCyn, 74.970000 );
                        var idMgnt = charIDToTypeID( "Mgnt" );
                        desc7.putDouble( idMgnt, 67.920000 );
                        var idYlw = charIDToTypeID( "Ylw " );
                        desc7.putDouble( idYlw, 67.050000 );
                        var idBlck = charIDToTypeID( "Blck" );
                        desc7.putDouble( idBlck, 90.150000 );
                    var idCMYC = charIDToTypeID( "CMYC" );
                    desc6.putObject( idClr, idCMYC, desc7 );
                var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
                desc5.putObject( idstrokeStyleContent, idsolidColorLayer, desc6 );
                var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
                desc5.putInteger( idstrokeStyleVersion, 2 );
                var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
                desc5.putBoolean( idstrokeEnabled, true );
                var idfillEnabled = stringIDToTypeID( "fillEnabled" );
                desc5.putBoolean( idfillEnabled, false );
            var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
            desc4.putObject( idstrokeStyle, idstrokeStyle, desc5 );
        var idshapeStyle = stringIDToTypeID( "shapeStyle" );
        desc3.putObject( idT, idshapeStyle, desc4 );
    executeAction( idsetd, desc3, DialogModes.NO );

    var idsetd = charIDToTypeID( "setd" );
        var desc9 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref2 = new ActionReference();
            var idcontentLayer = stringIDToTypeID( "contentLayer" );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref2.putEnumerated( idcontentLayer, idOrdn, idTrgt );
        desc9.putReference( idnull, ref2 );
        var idT = charIDToTypeID( "T   " );
            var desc10 = new ActionDescriptor();
            var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
                var desc11 = new ActionDescriptor();
                var idstrokeStyleLineWidth = stringIDToTypeID( "strokeStyleLineWidth" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc11.putUnitDouble( idstrokeStyleLineWidth, idPxl, 7.000000 );
                var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
                desc11.putInteger( idstrokeStyleVersion, 2 );
                var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
                desc11.putBoolean( idstrokeEnabled, true );
            var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
            desc10.putObject( idstrokeStyle, idstrokeStyle, desc11 );
        var idshapeStyle = stringIDToTypeID( "shapeStyle" );
        desc9.putObject( idT, idshapeStyle, desc10 );
    executeAction( idsetd, desc9, DialogModes.NO );
}
JJMack
Community Expert
Community Expert
January 30, 2021

Your script works but changes the stroke color to black and the stroke weight  to 7px.

JJMack
Chuck Uebele
Community Expert
Community Expert
January 30, 2021

That's what the OP wanted, a 7px stroke. The screen shot that was posted shows a black stroke, so that's what I went with. The script actually deletes the original layer, as you can't just edit a path or shape, you have to redraw it.

JJMack
Community Expert
Community Expert
January 21, 2021

If  you create a line shape layer that has a that has stroked line paths. With that layer  active a script  could most like get  line paths info  and add  vertical 54px vertically down lines to the line end control points.

JJMack
ChristianK-Fr
Inspiring
January 29, 2021

Hi Thanks for answer ! Did you know how I can made this script, I'm noob in javascript.....

Thx again !

JJMack
Community Expert
Community Expert
January 29, 2021

If you are new to scripting you need the read the Adobe three scripting doc PDF  download and install the scriptlistener Plugin and look ate the sample scripts source code and the script the Adobe has provided in Photoshop Presets\Scripts folder.  You have a lot to learn. It will take some time. I just hack at Scripting.

JJMack