Path

A path object. New paths can be created using a Document's addPath function. The path of an existing PathShape can be modified, and new PathShapes can be created using a Layer's or ShapeGroup's addPathShape: function.
 
Elements

Appending Elements

Bounds / Length

Creating Copies

addClose

returns PathElement

Closes the current subpath. Returns the new path element object.
 

Example: create a rectangle path; the start and end points are linked by the final close element

var doc = [app activeDocument]
var path = [doc addPath]
var pnt = CGPointMake(20, 20)
var size = CGSizeMake(100, 200)

[path addMoveTo:CGPointMake(pnt.x, pnt.y)]
[path addLineTo:CGPointMake(pnt.x + size.width, pnt.y)]
[path addLineTo:CGPointMake(pnt.x + size.width, pnt.y + size.height)]
[path addLineTo:CGPointMake(pnt.x, pnt.y + size.height)]
[path addClose]

[[doc activeLayer] addPathShape:path]

addCurveTo: controlPoint1: controlPoint2:

returns PathElement; input CGPoint; input CGPoint; input CGPoint

 
Adds a cubic curve element to the path, with the destination point and two bezier control points. Returns the new path element object.
 

Example: create a teardrop path using a "curveTo" element

var doc = [app activeDocument]
var path = [doc addPath]

[path addMoveTo:CGPointMake(84, 144)]
[path addCurveTo:CGPointMake(84, 144) controlPoint1:CGPointMake(12, 264) controlPoint2:CGPointMake(156, 264)]
[path addClose]

[[doc activeLayer] addPathShape:path]

addLineTo:

returns PathElement; input CGPoint

Adds a line element to the path. Returns the new path element object.
 

Example: create a path of a sine wave using a series of lineTo elements

var doc = [app activeDocument]
var path = [doc addPath]
var increment = Math.PI * 2 / 100
var counter = 0

for(i = 0; i <= 2; i += 0.01)
{
    x = i;
    y = Math.sin(counter) / 2 + 0.5
    counter += increment

    if(i == 0)
        [path addMoveTo:CGPointMake(0, y * 100)]
    else
        [path addLineTo:CGPointMake(x * 200, y * 100)]
}

[[doc activeLayer] addPathShape:path]

addMoveTo:

returns PathElement; input CGPoint

Begin a subpath at the passed-in point. Each path object must start with a moveTo element; moveTo elements added after the first element will begin new subpaths within the path object (creating a compound path). Returns the new path element object.

addQuadCurveTo: controlPoint:

returns PathElement; input CGPoint; input CGPoint

Adds a cubic curve element to the path, with the destination point and two bezier control points. Returns the new path element object.

bounds

returns CGRect

Gets the geometric bounding box of the path.

controlPointBounds

returns CGRect

Gets the bounding box that encloses all of the points in the path, including the control points.

duplicate

returns Path

Creates and returns a duplicate of this path object.

elements

returns Array

Gets the array of elements in the path.

length

returns Float

Gets the length of the contour of the path.


 

PathElement

A path element object.
 
Points

Metrics

Removing

Type

anchorPoint

returns CGPoint; settable

Gets / Sets the anchor point of the path element.

controlPoint1

returns CGPoint; settable

Gets / Sets the first control point of the path element. Note: only used by curveTo and quadCurveTo elements.

controlPoint2

returns CGPoint; settable

Gets / Sets the second control point of the path element. Note: only used by curveTo elements.

length

returns Float

Gets the length of the path element.

remove

Removes this element from the parent path.

split:

returns Array; input Float

Splits this element at the passed in value, 0-1 designating the location along the element, creating two new elements. This element is removed from the parent path and replaced with the new returned elements.

Note: "close" elements can be split, but only if the start and end points of the subpath are not the same. In that case, this function will return three new elements: two "lineTo" elements linking the previous start/end points of the subpath, and a new "close" element. Calling this function on a "moveTo" element does nothing.
 

Example: split every element of each selected path shape's path at its midpoint

var doc = [app activeDocument]
var shapes = [doc selectedShapes]

for(var i = 0; i < [shapes count]; i++)
{    
    var shape = shapes[i];

    if([shape type] == "pathShape")
    {
        var path = [shape path]
        var elements = [path elements]

        for(var j = 0; j < [elements count]; j++)
        {
            var element = elements[j]

            if([element type] != "moveTo")
                elements[j].split(0.5)
        }
    }
}

type

returns String; settable

Gets the type of this element - "moveTo", "lineTo", "curveTo", "quadCurveTo", or "close"
 

Next: Color