photoshopextendscriptphotoshop-scriptuxp-script

How to fill a path in Adobe Photoshop with ExtendScript


I have a function that I used to draw lines and then stroke the path.

export const getSolidColor = (hex) => {
    const color = new SolidColor()
    color.rgb.hexValue = hex
    return color
}

export const drawLines = (doc, coordinates, title) => {
    const lines = []
    for (i = 0; i < coordinates.length; i++) {
        const { anchor, kind, ldr, rdr } = coordinates[i]
        const point = [anchor.x, anchor.y]
        const line = new PathPointInfo()
        line.kind = kind
        line.anchor = point
        line.leftDirection = ldr ? [ldr.x, ldr.y] : point
        line.rightDirection = rdr ? [rdr.x, rdr.y] : point
        lines.push(line)
    }

    const subPath = new SubPathInfo()
    subPath.closed = true
    subPath.entireSubPath = lines
    subPath.operation = ShapeOperation.SHAPEXOR

    const item = doc.pathItems.add(title, [subPath])
    item.deselect()
    return item
}

When I call this function to strokePath it works fine.

// Create a new document
const width = 1920
const height = 1080
const ppi = 72
const title = "Character 1"
const mode = NewDocumentMode.RGB
const newDoc = app.documents.add(width, height, ppi, title, mode)

const brush = ToolType.BRUSH
drawLines(newDoc, coords, "Line 1").strokePath(brush)

But, when I try to fill the path using the fillPath method, the layer just appears empty.

export const convertPoints = (points) => {
    const newPoints = []
    for (i = 0; i < points.length; i++) {
        newPoints.push({
            kind: PointKind.CORNERPOINT,
            anchor: {
                x: points[i][0],
                y: points[i][1]
            },
            ldr: null,
            rdr: null
        })
    }
    return newPoints
}

const newPoints = convertPoints([
    [width * 0.33, height * 0],
    [width * 0.33, height * 1],
    [width * 0.5, height * 1],
    [width * 0.5, height * 0]
])
const fillColor = getSolidColor("0080ff")
const opacity = 90
const fillMode = ColorBlendMode.NORMAL
const preserveTransparency = true
const feather = 0
const wholePath = true
const antiAlias = true
drawLines(newDoc, newPoints, "Line 2").fillPath(
    fillColor,
    fillMode,
    opacity,
    preserveTransparency,
    feather,
    wholePath,
    antiAlias
)

The shape appears along with the anchor points on the Photoshop document but its not colored or filled inside. Does anyone know how to do with with ExtendScript?

Thank you!


Solution

  • You can fill a path (once selected) with the foreground colour using:

    var idFl = charIDToTypeID( "Fl  " );
    var desc43 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    var ref8 = new ActionReference();
    var idPath = charIDToTypeID( "Path" );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref8.putEnumerated( idPath, idOrdn, idTrgt );
    desc43.putReference( idnull, ref8 );
    var idWhPt = charIDToTypeID( "WhPt" );
    desc43.putBoolean( idWhPt, true );
    executeAction( idFl, desc43, DialogModes.NO );