Skip to main content
Inspiring
February 21, 2018
Answered

Evenly Select Anchor Points

  • February 21, 2018
  • 3 replies
  • 5481 views

Hi,

I would like to select every n(th) anchor points of select path(s).

I found a script tried to modify it I am able to remove every 2nd anchor point but what I really want is to select them. (And surely there is a better way to do this.)

main();

function main(){

  if(documents.length < 1) return;

 

  var s = activeDocument.selection;

  if(!(s instanceof Array) || s.length < 1) return;

  var paths = [];

  extractPaths(s, 0, paths);

  var p, j;

  for(var i = paths.length - 1; i >= 0; i--){

    p = paths.pathPoints;

    totalpoint = p.length;

    //alert (totalpoint);

    for(j = totalpoint - 1; j >= 0; j--){

        if ( j  && (j  % 2 === 0)) {

            p.remove();

            //p.PathPointSelection.ANCHORPOINT.isSelected = true ;

            }

   }

    if(p.length < 2 && isSelected(p[0])) paths.remove();

  }

  redraw();

}

// ----------------------------------------------

function isSelected(p){ // PathPoint

  return p.selected == PathPointSelection.ANCHORPOINT;

}

// --------------------------------------

function extractPaths(s, pp_length_limit, paths){

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

    if(s.typename == "PathItem"){

     

      if(pp_length_limit > 0

         && s.pathPoints.length <= pp_length_limit) continue;

      paths.push( s );

     

    } else if(s.typename == "GroupItem"){

      extractPaths( s.pageItems, pp_length_limit, paths);

     

    } else if(s.typename == "CompoundPathItem"){

      extractPaths( s.pathItems, pp_length_limit, paths);

    }

  }

}

Thank you for your help.

This topic has been closed for replies.
Correct answer arteangelus

Hi Everyone, Silly-V

Finally I found my own answer from Hiroyuki's blog.

Here is his script which exactly does what I need.

// selects every other anchor of the selected pathes.

var sel = activeDocument.selection;

var p, i;

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

sel.selected = false;

p = sel.pathPoints;

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

  if( i % 2 == 1 )

    p.selected = PathPointSelection.ANCHORPOINT;

}

}

3 replies

Tussitaikuri
Participant
August 15, 2018

Is it possible to modify this to select the last anchor point on a path? Answer may be simple, but scripting is not my strength.

Thanks!

Disposition_Dev
Legend
August 15, 2018

untested, but should work..

function getLastPathPoint()

{

     myPath.pathPoints[myPath.pathPoints.length-1].selected = PathPointSelection.ANCHORPOINT;

}

getLastPathPoint();

arteangelusAuthorCorrect answer
Inspiring
February 22, 2018

Hi Everyone, Silly-V

Finally I found my own answer from Hiroyuki's blog.

Here is his script which exactly does what I need.

// selects every other anchor of the selected pathes.

var sel = activeDocument.selection;

var p, i;

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

sel.selected = false;

p = sel.pathPoints;

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

  if( i % 2 == 1 )

    p.selected = PathPointSelection.ANCHORPOINT;

}

}

Silly-V
Legend
February 23, 2018

Oh, yea - you have to have started with the path not selected in any way in the first place with my sample  - but it's good you got it figured out, my snippet simply illustrated usage of the PathPointSelection enumerator when used on a pathPoint.

Moodring
Participant
July 29, 2018

How could you modify this to select only one anchor point on a path? Perhaps randomly — or perhaps just the first anchor?

Appreciate your consideration.

Silly-V
Legend
February 21, 2018

Please give this snippet a try: it works on a document which has only one path in it, to illustrate the necessary enumerator for selecting a pathPoint. Note that you have to have the direct-selection tool (A) active to see the points - for someone who may not know this when they come upon this thread.

#target illustrator

function test(){

    var doc =app.activeDocument;

    var myPath = doc.pathItems[0];

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

        if(i % 2 != 0){

          continue; 

        }

        myPath.pathPoints.selected = PathPointSelection.ANCHORPOINT;

    }

};

test();

Inspiring
February 21, 2018

Hi,

Unfortunately it didn't work I even tried it to add my script. I cannot see anchor points are selected.

main();

function main(){

  if(documents.length < 1) return;

 

  var s = activeDocument.selection;

  if(!(s instanceof Array) || s.length < 1) return;

  var paths = [];

  extractPaths(s, 0, paths);

  var p, j;

  for(var i = paths.length - 1; i >= 0; i--){

    p = paths.pathPoints;

    totalpoint = p.length;

    //alert (totalpoint);

    for(j = totalpoint - 1; j >= 0; j--){

        if ( i % 2 == 0) {

             //p.pathPoints.selected = PathPointSelection.ANCHORPOINT;

             p.selected = PathPointSelection.ANCHORPOINT;

            //p.PathPointSelection.ANCHORPOINT.isSelected = true ;

            }

   }

    //if(p.length < 2 && isSelected(p[0])) paths.remove();

  }

  redraw();

}

// ----------------------------------------------

function isSelected(p){ // PathPoint

  return p.selected == PathPointSelection.ANCHORPOINT;

}

// --------------------------------------

function extractPaths(s, pp_length_limit, paths){

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

    if(s.typename == "PathItem"){

     

      if(pp_length_limit > 0

         && s.pathPoints.length <= pp_length_limit) continue;

      paths.push( s );

     

    } else if(s.typename == "GroupItem"){

      extractPaths( s.pageItems, pp_length_limit, paths);

     

    } else if(s.typename == "CompoundPathItem"){

      extractPaths( s.pathItems, pp_length_limit, paths);

    }

  }

}

Silly-V
Legend
February 21, 2018

First try the exact snippet out on a document with only the single path, then if it is found to be working, make sure you do not confuse your 'i' and 'j' counter variables - as they are not correct in your script.