• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Evenly Select Anchor Points

Participant ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Hi,

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

screenshot.jpg

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.

TOPICS
Scripting

Views

4.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Feb 22, 2018 Feb 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;

}

}

Votes

Translate

Translate
Adobe
Valorous Hero ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

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);

    }

  }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

I have already done that but It did not work. I tried without touching your code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

By the way it's working with "remove" commands. (I.E.: p.remove();)

It throws no errors but not making selection either.

I tried selecting all anchor points with direct selection tool as well before running the script.

I also tried to delete or move anchors after running script but resulted with all anchors points moving together.

I believe it would be so handy to have such simple but useful snippet no?

Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

Are you having your (V) selection tool active or the (A) selection tool active?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

Hi Silly thanks for your help,

I once again tried your script, still does not work in my AI 2018. I ran script from ExtendScript and also ran directly from AI. No difference. What I am trying to achieve is showed in 3rd image. So strange...:(

screen1.jpgscreen2.jpgscreen3.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

Hi Silly-V​,

I drew a simple path in a new document then run this script. It worked. But with a line path or circle it does not work unfortunately.

For example with line segment tool I am drawing a single line. Then dividing it using add anchor points commands. Or doing the same process with circle...

Do all other shapes like line,circle or star use other commands in extendscript? May be PathPointSelection.ANCHORPOINT command does not work with them.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Hi Silly-V​,

your snippet works as expected in AI CC2018. And I'm sure, this code also should do the job in previous versions.

arteangelus​ please try this snippet again - standalone in a new AI document with only one (simple) path. (There is no selection required.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

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;

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2018 Jul 28, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2018 Jul 30, 2018

Copy link to clipboard

Copied

#target illustrator

function test()

{

    var doc = app.activeDocument;

    doc.selection = null;

    var myPath = doc.pathItems[0];

    function getFirstPathPoint()

    {

        myPath.pathPoints[0].selected = PathPointSelection.ANCHORPOINT;

    }

   

    function getRandomPathPoint()

    {

        function getRandom(min,max)

        {

            return Math.floor(Math.random() * (max - min + 1) + min);

        }

        myPath.pathPoints[getRandom(0,2)].selected = PathPointSelection.ANCHORPOINT;

    }

    //lets say you want to select just the first anchor point.

    getFirstPathPoint();

    //or if you wanted to select one of the first 3 anchor points randomly

    // getRandomPathPoint();

};

test();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 31, 2018 Jul 31, 2018

Copy link to clipboard

Copied

Thank you! I'll give it a go.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 15, 2018 Aug 15, 2018

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 15, 2018 Aug 15, 2018

Copy link to clipboard

Copied

LATEST

untested, but should work..

function getLastPathPoint()

{

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

}

getLastPathPoint();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines