5 Replies Latest reply on Jan 8, 2011 8:56 PM by Kenneth Evans

    Problem Filling and Selecting with Paths.jsx

    Kenneth Evans

      I have copied Paths.jsx from the Adobe Photoshop CS5 Javascript Scripting Reference, p. 141.  It works OK.  It makes a path that is the outline of an ice cream cone and strokes it with the current foreground color.

       

      I then tried to fill the path and that does not work.  I have tried 3 methods using the following additional code and changing the appropriate false's to true:

       

      // Fill the path

      fillColor = new SolidColor

      fillColor.rgb.red = 255

      fillColor.rgb.green = 0

      fillColor.rgb.blue = 0

      try {

         if(false) {

               // This works and gives a gray fill that is neither the

                    // foreground nor background color

           // Only the ice cream part of the cone is selected and filled

                  myPathItem.fillPath()

         } else if(false) {

           // With a color specified, it doesn't work

                    myPathItem.fillPath(fillColor)

             } else if(true) {

           // This makes a selection of the PathItem, selects it and fills it

           // Only the ice cream part of the cone is selected and filled

                    myPathItem.makeSelection(0, false, SelectionType.REPLACE)

                     selRef = app.activeDocument.selection;

                    selRef.fill(fillColor, ColorBlendMode.NORMAL)

             }

      } catch(ex) {

       

              msg = "Error filling path:\n" + ex.message

              alert(msg, "Exception", true);

      }

       

      They do as the comments say.  None of them does what I would expect.  For the second way, the error message is:

       

      General Photoshop error occurred.  This functionality may not be available in this version of Photoshop

      - Could not complete the command because of a program error.

       

        1. I do not understand why only the      ice cream part of the cone is filled or selected (the third array).  I can manually      make a selection from the Path in PS and it also only selects the      ice cream part.  All of the lines are stroked.  Only the top part is      filled or selected.

        2. Why does fillPath not work?

       

      I can supply the whole script if necessary.  I am an experienced programmer but new to Photoshop scripting.  Is this a bug or am I doing something wrong?

       

      Thanks for any help.

        • 1. Re: Problem Filling and Selecting with Paths.jsx
          Kenneth Evans Level 1

          This is the whole script:

           

          // Paths.jsx

           

          #target photoshop

           

          // Save the current preferences
          var startRulerUnits = app.preferences.rulerUnits
          var startTypeUnits = app.preferences.typeUnits
          var startDisplayDialogs = app.displayDialogs
          // Set Adobe Photoshop CS5 to use pixels and display no dialogs
          app.preferences.rulerUnits = Units.PIXELS
          app.preferences.typeUnits = TypeUnits.PIXELS
          app.displayDialogs = DialogModes.NO

          // First close all the open documents
          while (app.documents.length) {
          app.activeDocument.close()
          }

           

          // Create a document to work with
          var docRef = app.documents.add(5000, 7000, 72, "Simple Line")

           

          // line 1--it’s a straight line so the coordinates for anchor, left, and right
          // for each point have the same coordinates
          var lineArray = new Array()
          lineArray[0] = new PathPointInfo
          lineArray[0].kind = PointKind.CORNERPOINT
          lineArray[0].anchor = Array(100, 100)
          lineArray[0].leftDirection = lineArray[0].anchor
          lineArray[0].rightDirection = lineArray[0].anchor
          lineArray[1] = new PathPointInfo
          lineArray[1].kind = PointKind.CORNERPOINT
          lineArray[1].anchor = Array(150, 200)
          lineArray[1].leftDirection = lineArray[1].anchor
          lineArray[1].rightDirection = lineArray[1].anchor
          var lineSubPathArray = new Array()
          lineSubPathArray[0] = new SubPathInfo()
          lineSubPathArray[0].operation = ShapeOperation.SHAPEXOR
          lineSubPathArray[0].closed = false
          lineSubPathArray[0].entireSubPath = lineArray

           

          // line 2
          var lineArray2 = new Array()
          lineArray2[0] = new PathPointInfo
          lineArray2[0].kind = PointKind.CORNERPOINT
          lineArray2[0].anchor = Array(150, 200)
          lineArray2[0].leftDirection = lineArray2[0].anchor
          lineArray2[0].rightDirection = lineArray2[0].anchor
          lineArray2[1] = new PathPointInfo
          lineArray2[1].kind = PointKind.CORNERPOINT
          lineArray2[1].anchor = Array(200, 100)
          lineArray2[1].leftDirection = lineArray2[1].anchor
          lineArray2[1].rightDirection = lineArray2[1].anchor
          lineSubPathArray[1] = new SubPathInfo()
          lineSubPathArray[1].operation = ShapeOperation.SHAPEXOR
          lineSubPathArray[1].closed = false
          lineSubPathArray[1].entireSubPath = lineArray2

           

          // Ice cream curve
          // It’s a curved line, so there are 3 points, not 2
          // coordinates for the middle point (lineArray3[1]) are different.
          // The left direction is positioned "above" the anchor on the screen.
          // The right direction is positioned "below" the anchor
          // You can change the coordinates for these points to see
          // how the curve works...
          var lineArray3 = new Array()
          lineArray3[0] = new PathPointInfo
          lineArray3[0].kind = PointKind.CORNERPOINT
          lineArray3[0].anchor = Array(200, 100)
          lineArray3[0].leftDirection = lineArray3[0].anchor
          lineArray3[0].rightDirection = lineArray3[0].anchor
          lineArray3[1] = new PathPointInfo
          lineArray3[1].kind = PointKind.CORNERPOINT
          lineArray3[1].anchor = Array(150, 50)
          lineArray3[1].leftDirection = Array(100, 50)
          lineArray3[1].rightDirection = Array(200, 50)
          lineArray3[2] = new PathPointInfo
          lineArray3[2].kind = PointKind.CORNERPOINT
          lineArray3[2].anchor = Array(100, 100)
          lineArray3[2].leftDirection = lineArray3[2].anchor
          lineArray3[2].rightDirection = lineArray3[2].anchor
          lineSubPathArray[2] = new SubPathInfo()
          lineSubPathArray[2].operation = ShapeOperation.SHAPEXOR
          lineSubPathArray[2].closed = false
          lineSubPathArray[2].entireSubPath = lineArray3

           

          // Create the path item
          var myPathItem = docRef.pathItems.add("A Line", lineSubPathArray)

           

          // Stroke it so we can see something
          myPathItem.strokePath(ToolType.BRUSH)

           

          // Fill the path
          fillColor = new SolidColor
          fillColor.rgb.red = 255
          fillColor.rgb.green = 0
          fillColor.rgb.blue = 0
          try {
              if(false) {
                  // This works and gives a gray fill that is neither the
                  // foreground nor background color
                  // Only the ice cream part of the cone is selected and filled
                  myPathItem.fillPath()
              } else if(false) {
                  // With a color specified, it doesn't work
                  myPathItem.fillPath(fillColor)
              } else if(true) {
                  // This makes a selection of the PathItem, selects it and fills it
                  // Only the ice cream part of the cone is selected and filled
                  myPathItem.makeSelection(0, false, SelectionType.REPLACE)
                  selRef = app.activeDocument.selection;
                  selRef.fill(fillColor, ColorBlendMode.NORMAL)
              }
          } catch(ex) {
              msg = "Error filling path:\n" + ex.message
              alert(msg, "Exception", true);
          }

           

          // Reset the application preferences
          preferences.rulerUnits = startRulerUnits
          preferences.typeUnits = startTypeUnits
          displayDialogs = startDisplayDialogs

          • 2. Re: Problem Filling and Selecting with Paths.jsx
            Michael L Hale Level 5

            It's not a bug. The path you created has 3 open subpaths, two of which are lines. Fill does not work on open lines. It will fill open arcs and/or closed paths.

             

            One way to be able to fill the cone would be to create it as a three point closed path instead of just two lines.

            • 3. Re: Problem Filling and Selecting with Paths.jsx
              Kenneth Evans Level 1

              Yes.  I figured that out myself after going away and thinking about it.  (I didn't get back in time ;-) It takes 5 points, including the end.  But thanks, it must have been much harder for you to read my code and see that than for me.

               

              I am still left with the problem with method two:

               

              General Photoshop error occurred.  This functionality may not be available in this version of Photoshop

              - Could not complete the command because of a program error.

               

              The Object Model Viewer says the first argument is any, but the Reference Manual says it is a SolidColor.  Perhaps I am using it wrong.  In Photoshop in the Paths panel, it fills with the foreground color.  Method 1 filled it with a medium gray.  Method 2 is what I tried first and the most straightforward way, I think.

               

              Thanks again.

              • 4. Re: Problem Filling and Selecting with Paths.jsx
                Michael L Hale Level 5

                The guide does make it look like all the arguments are optional but it seems that if you supply the color you must also supply at least some of the other arguments. Your code works for me if I supply all the arguments

                 

                myPathItem.fillPath(fillColor,ColorBlendMode.NORMAL,100,false,0,true,true);

                1 person found this helpful
                • 5. Re: Problem Filling and Selecting with Paths.jsx
                  Kenneth Evans Level 1

                  It seems to work for me too.  Thanks.

                   

                  I appologize for not marking it as a correct answer.  Actually what I did was mark the first answer as correct and then the second as helpful.  Only the latter seemed to remain.  I am new at this forum.  If you can tell me how to fix it, I will.

                   

                  In any event I appreciate the help and both answers were correct.