Skip to main content
Known Participant
February 2, 2025
Answered

Index of pathPoints from pathItems Selection

  • February 2, 2025
  • 1 reply
  • 579 views

Using the Direct Selection Tool I would like to target a single anchor point on multiple pathItems and return their index so I can push their respective X and Y coordinates to an array. I am enclosing a code snippet where the index is static and not stemming from the selection.

 

var idoc = app.activeDocument;

var pathSelection = idoc.selection;

var pathAnchors = [];

  for (var i = 0; i < pathSelection.length; i++) {

    var selectedPath = pathSelection[i];

    // anchor point of the selected path
    var selectedAnchorPoint = selectedPath.pathPoints[0]; // help acquiring pathPoints index from selection

    // x and y coordinates
    var anchorX = selectedAnchorPoint.anchor[0];
    var anchorY = selectedAnchorPoint.anchor[1];

    pathAnchors.push({x: anchorX, y: anchorY});

    alert("<<<  Array Contents  >>>\n\nX: " + pathAnchors[i].x + "\n\nY: " + pathAnchors[i].y);

  }
Correct answer Sergey Osokin

If we check that a point on the path is selected, we don't need the index separately to get its anchor coordinates immediately.

(function () {
  var selectedPoints = [];

  for (var i = 0; i < app.selection.length; i++) {
    var item = app.selection[i];

    if (item.typename === "PathItem" && item.hasOwnProperty("pathPoints")) {
      for (var j = 0; j < item.pathPoints.length; j++) {
        var currPoint = item.pathPoints[j];
        // Check if the pathPoint is selected
        if (currPoint.selected === PathPointSelection.ANCHORPOINT) {
          selectedPoints.push({
            x: currPoint.anchor[0],
            y: currPoint.anchor[1]
          });
        }
      }
    }
  }
})();

1 reply

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
February 3, 2025

If we check that a point on the path is selected, we don't need the index separately to get its anchor coordinates immediately.

(function () {
  var selectedPoints = [];

  for (var i = 0; i < app.selection.length; i++) {
    var item = app.selection[i];

    if (item.typename === "PathItem" && item.hasOwnProperty("pathPoints")) {
      for (var j = 0; j < item.pathPoints.length; j++) {
        var currPoint = item.pathPoints[j];
        // Check if the pathPoint is selected
        if (currPoint.selected === PathPointSelection.ANCHORPOINT) {
          selectedPoints.push({
            x: currPoint.anchor[0],
            y: currPoint.anchor[1]
          });
        }
      }
    }
  }
})();
nutradialAuthor
Known Participant
February 3, 2025

I overlooked implementing the hasOwnProperty method along with PathPointSelection.ANCHORPOINT. Thank you for taking the time and the valuable feedback.