@Peter and Trevor – there is a flaw in my idea. It could happen that two path points have the same position and only one of it is selected. So you could not tell by the anchor which is which.
But we could think of a different, more complex method to pin point the selected path points.
Also possibly with an app.undo() at the end.
By an menu action we could change the pointType of the path points selected and check before and after for the whole path to single out the changed ones. Possibly we have to do this more than one time, if more than one point type is present in the initial path.
Uwe
Thanks very much for putting me on the right track, Uwe and Trevor. The workaround, though ugly, is pretty straightforward. But first,
> By a menu action we could change the pointType of the path points selected
But we don't know which point is selected -- that's the whole point.
> app.undo() doesn't work on this function
app.documents[0].undo() does work
Here's the workaround I got to work thanks to your pointers. To try the script, draw a path with several points, then select a point with the direct selection tool (but not the first point; see below). This script moves the selected point up by 10 of whatever your units are:
app.cut();
index = app.selection[0].paths[0].pathPoints.length;
app.documents[0].undo();
movePoint (app.selection[0].paths[0].pathPoints[index])
function movePoint (point) {
var pos = [point.anchor[0], point.anchor[1]-10];
point.anchor = pos
}
The idea is this: if you cut a point you in fact cut the path in two and you end up with two paths. Both are selected: app.selection[0] contains the first part, app.selection[1], the second part. The length of the first part's path gives you the index of the deleted path point. Now undo and you can get a reference to the selected point.
But there's a problem: if you cut the first or the last point you end up with one path, not two. For the last point that's no problem, the above script still works. But if you delete the first point, index in the second line gives you the index of the last point. So we need to establish if we selected the first point. There are probably several ways of doing this; in the following script I take the anchor (a 2-element array with the point's x- and y-positions) of the first point, then cut the selected point, then check the anchor of what is now the first point. If these two anchors are the same, it was the last point that was cut; if the two anchors are not the same, the first anchor was cut.
// Get the coordinates of the first point as a string
first = String(app.selection[0].paths[0].pathPoints[0].anchor);
app.cut();
// If after cutting the point we end up with one selected object we cut either the first or the last point
// and if the first point of the new selection is not the same as the first point of the original selection,
// then we cut the first point.
if (app.selection.length == 1 && String(app.selection[0].paths[0].pathPoints[0].anchor) != first) {
index = 0;
} else {
index = app.selection[0].paths[0].pathPoints.length;
}
app.documents[0].undo();
movePoint (app.selection[0].paths[0].pathPoints[index])
function movePoint (point) {
var pos = [point.anchor[0], point.anchor[1]-10];
point.anchor = pos
}
Peter