Skip to main content
Peter Kahrel
Community Expert
Community Expert
March 9, 2014
Answered

JS: Selected pathPoint?

  • March 9, 2014
  • 2 replies
  • 3728 views

When you select a point on a path (with the direct selection tool), app.selection[0] returns [object Polygon]. I was hoping it would return [object PathPoint], but it doesn't. Is it therefore not possible to select a pathPoint in InDesign and get a reference to it with app.selection[0]?

Thanks,

Peter

This topic has been closed for replies.
Correct answer Peter Kahrel

@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

2 replies

Community Expert
March 9, 2014

@Peter – there is a method to check what path points are selected.

However it is an indirect method and involves the app.cut() method.

Store all pathPoints with their index and their anchor arrays.

Then do the app.cut() method.

Now inspect the path with the path points again.

Compare the anchor values with the ones stored.

The  missing ones after the cut were initially selected.

Uwe

Trevor:
Legend
March 9, 2014

And would there be a way to reselect it after it's been cut (and redrawn)?

Community Expert
March 9, 2014

@Trevor – no. You'll have to do an app.undo() for the cut().

Uwe

pixxxelschubser
Community Expert
Community Expert
March 9, 2014

Hi Peter,

I don't think there is a solution in ID

Sorry.

But e.g. in Illustrator you can do something like this:

var aDoc = app.activeDocument;

var thePp0 = aDoc.pathItems[0].pathPoints[0];

thePp0.selected  = PathPointSelection.ANCHORPOINT;

$.writeln(thePp0.selected);

$.writeln(aDoc.pathItems[0].selectedPathPoints[0].anchor);

Perhaps this helps a little bit.

Peter Kahrel
Community Expert
Community Expert
March 9, 2014

Thanks, pixxxel. Unfortunately there's no equivalent of that Illustrator code in InDesign.

Peter