Skip to main content
Known Participant
February 2, 2019
Answered

How to display vertex coordinates of Polygonal Lasso Tool?

  • February 2, 2019
  • 3 replies
  • 2021 views

I use Polygonal Lasso Tool to form a selection like the screenshot below:

Now I want to record the accurate vertex coordinates of the Polygonal Lasso Tool. Do you know how to display these coordinates somewhere in Photoshop so that I can record them manually in a notebook or automatically as a file? Thanks a lot.

PS: I'm using Photoshop CC 2019 on Windows 10.

Correct answer JJMack

The price is a bottle of red wine don't go overboard a $15 bottle will do.

// enable double-clicking from Mac Finder or Windows Explorer

#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events

app.bringToFront();

// Save the current preferences

var startRulerUnits = app.preferences.rulerUnits;

// Set Photoshop to use pixels

app.preferences.rulerUnits = Units.PIXELS;

var thePath = selectedPath();

if (thePath == undefined) {

   alert("No Path Selected");

}

else {

   var myPathInfo = extractSubPathInfo(thePath); 

  var pathPoints = "Path Points\n"

   for(var k=0;k<myPathInfo.length;k++){ 

       pathPoints = pathPoints + "Path" + (k+1) + "\n";

       for(var j=0;j<myPathInfo.entireSubPath.length;j++){

           pathPoints = pathPoints  + "X " + myPathInfo.entireSubPath.anchor[0] + " Y " + myPathInfo.entireSubPath.anchor[1] +"\n";

           MarkX(myPathInfo.entireSubPath.anchor[0]);

          MarkY(myPathInfo.entireSubPath.anchor[1]);

      }

   }

   alert(pathPoints);

}

////// determine selected path //////

function selectedPath () {

   try {

      var ref = new ActionReference();

      ref.putEnumerated( charIDToTypeID("Path"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

      var desc = executeActionGet(ref);

      var theName = desc.getString(charIDToTypeID("PthN"));

      return app.activeDocument.pathItems.getByName(theName)

   }

   catch (e) {

      return undefined

   }

};

// Return the app preferences

app.preferences.rulerUnits = startRulerUnits;

////// michael l hale’s code //////

function extractSubPathInfo(pathObj){ 

    var pathArray = new Array();// each element can be used as the second arugment in pathItems.add ie doc.pathItems.add("myPath1", [pathArray[0]]); 

    var pl = pathObj.subPathItems.length; 

    for(var s=0;s<pl;s++){ 

        var pArray = new Array(); 

          for(var i=0;i<pathObj.subPathItems.pathPoints.length;i++){ 

             pArray = new PathPointInfo; 

             pArray.kind = pathObj.subPathItems.pathPoints.kind; 

             pArray.anchor = pathObj.subPathItems.pathPoints.anchor; 

//alert("Anchor " + pathObj.subPathItems.pathPoints.anchor );

             pArray.leftDirection = pathObj.subPathItems.pathPoints.leftDirection; 

             pArray.rightDirection = pathObj.subPathItems.pathPoints.rightDirection; 

          }; 

        pathArray[pathArray.length] = new Array(); 

        pathArray[pathArray.length - 1] = new SubPathInfo(); 

        pathArray[pathArray.length - 1].operation = pathObj.subPathItems.operation; 

        pathArray[pathArray.length - 1].closed = pathObj.subPathItems.closed; 

        pathArray[pathArray.length - 1].entireSubPath = pArray; 

    }; 

    return pathArray; 

};

function MarkX(x) {

// =======================================================

var idMk = charIDToTypeID( "Mk  " );

    var desc61 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

        var desc62 = new ActionDescriptor();

        var idPstn = charIDToTypeID( "Pstn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc62.putUnitDouble( idPstn, idPxl, x);

        var idOrnt = charIDToTypeID( "Ornt" );

        var idOrnt = charIDToTypeID( "Ornt" );

        var idVrtc = charIDToTypeID( "Vrtc" );

        desc62.putEnumerated( idOrnt, idOrnt, idVrtc );

    var idGd = charIDToTypeID( "Gd  " );

    desc61.putObject( idNw, idGd, desc62 );

executeAction( idMk, desc61, DialogModes.NO );

}

function MarkY(y) {

// =======================================================

var idMk = charIDToTypeID( "Mk  " );

    var desc63 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

        var desc64 = new ActionDescriptor();

        var idPstn = charIDToTypeID( "Pstn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc64.putUnitDouble( idPstn, idPxl, y );

        var idOrnt = charIDToTypeID( "Ornt" );

        var idOrnt = charIDToTypeID( "Ornt" );

        var idHrzn = charIDToTypeID( "Hrzn" );

        desc64.putEnumerated( idOrnt, idOrnt, idHrzn );

    var idGd = charIDToTypeID( "Gd  " );

    desc63.putObject( idNw, idGd, desc64 );

executeAction( idMk, desc63, DialogModes.NO );

}

3 replies

Participant
April 24, 2023

Is there updated code for this?

Participant
March 17, 2025
#target photoshop

if (app.documents.length > 0) {
    var doc = app.activeDocument;
    var validPath = null;
    
    // Loop through all paths to find one that has points
    for (var i = 0; i < doc.pathItems.length; i++) {
        var tempPath = doc.pathItems[i];
        // Check for compound paths first
        if (tempPath.hasOwnProperty("subPathItems") && tempPath.subPathItems.length > 0) {
            validPath = tempPath;
            break;
        }
        // Then check for a simple path with pathPoints
        if (tempPath.hasOwnProperty("pathPoints") && tempPath.pathPoints.length > 0) {
            validPath = tempPath;
            break;
        }
    }
    
    if (!validPath) {
        alert("No path with points found in the active document.\n\nMake sure you have a visible path or a saved Work Path.");
    } else {
        var coordinates = [];
        
        // If the path is compound (has subPathItems)
        if (validPath.hasOwnProperty("subPathItems") && validPath.subPathItems.length > 0) {
            for (var i = 0; i < validPath.subPathItems.length; i++) {
                var subPath = validPath.subPathItems[i];
                for (var j = 0; j < subPath.pathPoints.length; j++) {
                    var pt = subPath.pathPoints[j];
                    coordinates.push([pt.anchor[0], pt.anchor[1]]);
                }
            }
        }
        // Otherwise, use the pathPoints property directly
        else if (validPath.hasOwnProperty("pathPoints") && validPath.pathPoints.length > 0) {
            for (var i = 0; i < validPath.pathPoints.length; i++) {
                var pt = validPath.pathPoints[i];
                coordinates.push([pt.anchor[0], pt.anchor[1]]);
            }
        }
        else {
            alert("No path points found in the selected path.");
        }
        
        // Build the output string manually
        var output = "var coordinates = [\n";
        for (var i = 0; i < coordinates.length; i++){
            output += "  [" + coordinates[i][0] + ", " + coordinates[i][1] + "]";
            if (i < coordinates.length - 1) {
                output += ",\n";
            }
        }
        output += "\n];";
        
        alert(output);
    }
} else {
    alert("No document is open.");
}
JJMack
Community Expert
Community Expert
February 2, 2019

Open the path panel and click on the icon in the bottom to convert the selection to a path the message states you have no path.

JJMack
Known Participant
February 2, 2019

It is working now! I'm so grateful for your help. Thank you!!!

JJMack
Community Expert
Community Expert
February 2, 2019

Send the red my way.

JJMack
JJMack
Community Expert
Community Expert
February 2, 2019

You may be able to write a Photoshop Script to do that. The script would have Photoshop convert the selection to a Path them process path to get the control points coordinates. There may be  extra points the script could also  write the path to a log file. Set guide line marking the points so you can read them on the rulers and switch between ruler unites  just record the point you want in the unites you want. A path can have many points a list may not fit on your display.

JJMack
Known Participant
February 2, 2019

@JJMack: Thank you. I have no knowledge of writing script in Photoshop and my time budget does not allow me to study it. Could you please share your script with me? I can pay you for the script.

jane-e
Community Expert
Community Expert
February 2, 2019

hengz80545720  wrote

Do you have a lot or a few of these to do?

Would it work for you to:

  • Convert your selection to a path in the Paths panel
  • Open the Info panel
  • Select an anchor point with the Direct Selection tool (white arrow)
  • Copy the info from the X and Y