4 Replies Latest reply on Mar 10, 2011 3:54 PM by Michael L Hale

    Drawing Lines Photoshop CS4 using JavaScript

    sports_fan72

      I am new to javascript and have read a manuel on the basics of javascript in regards to photoshop. I was just wondering how to draw a straight line using javascript. Thanks.

        • 1. Re: Drawing Lines Photoshop CS4 using JavaScript
          Michael L Hale Level 5

          If you mean scripting the brush tool to make a line you can't. You can do workarounds like using the line tool or making a path then stroking the path.

          1 person found this helpful
          • 2. Re: Drawing Lines Photoshop CS4 using JavaScript
            sports_fan72 Level 1

            How would you code that?


            • 3. Re: Drawing Lines Photoshop CS4 using JavaScript
              JJMack Most Valuable Participant

              You could start by looking at the sample scripts that ship with photoshop to see a coding sample of stroking a selection. Then look at the doc on making selection and paths etc.

               

              // Copyright 2002-2007.  Adobe Systems, Incorporated.  All rights reserved.

              // Create a stroke around the current selection.

              // Set the stroke color and width of the new stroke.

               

              // enable double clicking from the Macintosh Finder or the Windows Explorer

              #target photoshop

               

              // in case we double clicked the file

              app.bringToFront();

               

              // debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)

              // $.level = 0;

              // debugger; // launch debugger on next line

               

              if (app.documents.length > 0)

              {

                  if (app.activeDocument.activeLayer.isBackgroundLayer == false)

                  {

                      var strtRulerUnits = app.preferences.rulerUnits;

                      app.preferences.rulerUnits = Units.PIXELS;

                      var selRef = app.activeDocument.selection;

                      var offset = 10;

                      var selBounds = Array(Array(offset, offset), Array(app.activeDocument.width - offset, offset), Array(app.activeDocument.width - offset, app.activeDocument.height - offset), Array(offset, app.activeDocument.height - 10));

                      selRef.select(selBounds);

                      selRef.selectBorder(5);

               

                      var strokeColor = new SolidColor();

                      strokeColor.cmyk.cyan = 20;

                      strokeColor.cmyk.magenta = 90;

                      strokeColor.cmyk.yellow = 50;

                      strokeColor.cmyk.black = 50;

               

                      app.displayDialogs = DialogModes.NO;

                      selRef.stroke(strokeColor, 2, StrokeLocation.OUTSIDE, ColorBlendMode.VIVIDLIGHT, 75, true);

               

                      app.preferences.rulerUnits = strtRulerUnits;

                      selRef = null;

                      strokeColor = null;

                      selBounds = null;

                  }

                  else

                  {

                      alert("Operation cannot be performed on background layer");

                  }

              }

              else

              {

                  alert("Create a document with an active selection before running this script!");

              }

              1 person found this helpful
              • 4. Re: Drawing Lines Photoshop CS4 using JavaScript
                Michael L Hale Level 5

                Here is an example of using the line shape tool to draw a horz line. There is no error checking so you will need to make sure there is an open document and the activeLayer is an artLayer that is not pixel locked, etc.

                // two element array of numbers for x,y start of line, 
                // two element array of numbers for x,y endof line,
                //number;line width in pixels
                // uses foreground color
                function drawLine( startXY, endXY, width ) {
                     var desc = new ActionDescriptor();
                        var lineDesc = new ActionDescriptor();
                            var startDesc = new ActionDescriptor();
                            startDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), startXY[0] );
                            startDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), startXY[1] );
                        lineDesc.putObject( charIDToTypeID('Strt'), charIDToTypeID('Pnt '), startDesc );
                            var endDesc = new ActionDescriptor();
                            endDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), endXY[0] );
                            endDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), endXY[1] );
                        lineDesc.putObject( charIDToTypeID('End '), charIDToTypeID('Pnt '), endDesc );
                        lineDesc.putUnitDouble( charIDToTypeID('Wdth'), charIDToTypeID('#Pxl'), width );
                    desc.putObject( charIDToTypeID('Shp '), charIDToTypeID('Ln  '), lineDesc );
                    desc.putBoolean( charIDToTypeID('AntA'), true );
                    executeAction( charIDToTypeID('Draw'), desc, DialogModes.NO );
                };
                var startPoint = [118,434];
                var endPoint = [335,434];
                var lineWidth = 2;
                drawLine( startPoint, endPoint, lineWidth );