• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

javascript : draw lines to path

Participant ,
Jan 21, 2021 Jan 21, 2021

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 29, 2021 Jan 29, 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 Pat
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 21, 2021 Jan 21, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 29, 2021 Jan 29, 2021

Copy link to clipboard

Copied

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

Thx again !

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2021 Jan 29, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2021 Jan 29, 2021

Copy link to clipboard

Copied

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 );
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2021 Jan 29, 2021

Copy link to clipboard

Copied

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

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2021 Jan 29, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Thanks for you script, thanks you foryour help, I test it ans see how it works, it's a great job ! 

Best Regards,

Christian

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Hi Chuck, 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

The Current targeted layer need to be your Shape layer that your two point line shape layer you created with the Line Shape tool. When you run the script. Your document does not have a shape layer.

Capture.jpg

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

As r-bin pointed out, I didn't think about your computer not using English. The line that fails assumes that it's English and any shape layer's path would have the layer name appended with "Shape Path." So the script either needs to be localized, which substitutes the correct language, or the words " Shape Path" needs to be replace with whatever Photoshop generates in your language.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Didn't delve into the script, but try to replace the line 

var path1 = doc.pathItems.getByName(curLay.name + ' Shape Path');

with line

var path1 = doc.pathItems.getByName(curLay.name + " " + localize("$$$/MaskPanel/MaskSelection/ShapePath"));

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Thanks, yea, I didn't think about needing localization.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 02, 2021 Feb 02, 2021

Copy link to clipboard

Copied

it works ! I've found what's wrong, thanks JJMack to give a way to find it : it's about effectively English vs French : I'v made this and it' works : 

var path1 = doc.pathItems.getByName('Tracé de la forme ' + curLay.name);

Thanks for all nameshapefrench.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 02, 2021 Feb 02, 2021

Copy link to clipboard

Copied

One more and last thing, is it possible to have this option for have a nice angle, and it will be perfect. Is it strokeStyle ? middleoption.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 02, 2021 Feb 02, 2021

Copy link to clipboard

Copied

You Stroke option has the Inside checked. There no in side the a line path.    Prior Version of Photoshop Supported four point line shape layers where the inside path width was line weight.  Adobe removed that feature.    Also this script would not work with a four point line shape layer.

Capture.jpg

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 02, 2021 Feb 02, 2021

Copy link to clipboard

Copied

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 );

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 02, 2021 Feb 02, 2021

Copy link to clipboard

Copied

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

Thanks and have a nice day !

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 03, 2021 Feb 03, 2021

Copy link to clipboard

Copied

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.... :-))

pathpoint.pngangles.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 03, 2021 Feb 03, 2021

Copy link to clipboard

Copied

LATEST

It's OK, I found ! Thanks ! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines