Copy link to clipboard
Copied
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);
}
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
...
Copy link to clipboard
Copied
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]
});
}
}
}
}
})();
Copy link to clipboard
Copied
I overlooked implementing the hasOwnProperty method along with PathPointSelection.ANCHORPOINT. Thank you for taking the time and the valuable feedback.
Copy link to clipboard
Copied
Can you help me take this one step further? Assuming the pathItems are a new selection, using the "selectedPoints" array to match coordinates and select the achorpoints.
Copy link to clipboard
Copied
What does this mean? A function should loop through all pathItems in the document and their anchor points, check the coordinates of each anchor point against the array, and select the anchor if a match is found?
Or should the user select a pathItem and the function check if the anchor points match the coordinates from the array and leave only the matching anchor points selected?
Adobe Illustrator can also have fractional coordinates that differ by a small amount. For example, if two points match, but one has a coordinate of X: 123.000 and the other has a coordinate of X: 122.999.
Copy link to clipboard
Copied
What I would like to do:
Copy link to clipboard
Copied
Have you considered such a solution? If the paths are the same, their order in layers has not changed: 5 objects were selected, next time the same 5 objects will be selected. Then you can store an array of object data: index or unique name in the document of each path (even easier), indexes of points of each path that need to be selected again. Maybe you don't need to store X, Y coordinates here.
Copy link to clipboard
Copied
I agree that using the index values instead of the coordinates makes more sense. I guess what I am really after, and once I figure it out I can use it in many other ways, is to use the array to select the anchors. Push the pathItem name and anchor point index of the selected anchors to "selectedPoints" array and then use it to match and reselect the anchors at some other time.