Copy link to clipboard
Copied
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.
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;
}
}
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();
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
//p
}
}
//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);
}
}
}
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.
Copy link to clipboard
Copied
I have already done that but It did not work. I tried without touching your code.
Copy link to clipboard
Copied
By the way it's working with "remove" commands. (I.E.: p
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.
Copy link to clipboard
Copied
Are you having your (V) selection tool active or the (A) selection tool active?
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...:(
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.
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.)
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;
}
}
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.
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.
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();
Copy link to clipboard
Copied
Thank you! I'll give it a go.
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!
Copy link to clipboard
Copied
untested, but should work..
function getLastPathPoint()
{
myPath.pathPoints[myPath.pathPoints.length-1].selected = PathPointSelection.ANCHORPOINT;
}
getLastPathPoint();