Skip to main content
Omerpekin
Participant
December 4, 2015
Answered

Selecting every nth object in my selection

  • December 4, 2015
  • 2 replies
  • 2804 views

Hi, I am trying to create a script that selects every nth (right now its set to be 2, so every 2nd) path in my selection. Somehow this script works just a bit but very unreliable. Sometimes it creates weird patterns such as selecting first one and then not selecting the next four and selecting one more etc. The total number looks fine but the pattern is completely wrong. How can I edit so that it would select every nth path I want? My scene in illustrator is just a scene with a lot of lines(one path with only 2 anchors) copied with different colors. And I am working on illustrator cs6

Here is my code:

#target illustrator

var doc = app.activeDocument;

selectedObjects = doc.selection;

numSelected = 0;

for ( var i = 0; i < doc.selection.length; i++ )

{

    if ( i % 2 == 0 )

    {

        doc.selection.selected = true;

        numSelected++;

    } else {

  doc.selection.selected = false;

  }

}

alert( numSelected, "many objects Selected");

This topic has been closed for replies.
Correct answer Qwertyfly___

1 loop is still fine.

silly is correct in saying you are changing your selection on each iteration.

so the simple way is to create array at start containing all selected items.

then deselect all, and then run back through your pre built array tagging every second one as selected

give this a shot:

var doc = app.activeDocument;

var selectedObjects = doc.selection;

doc.selection = null;

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

    if ((i+1) % 2 == 0 ){  // remove the +1 to select the opposite

        selectedObjects.selected = true;

    }

}

alert( doc.selection.length + " objects Selected");

edit:
should mention the 2 is the nth, and the 1 is the offest.

so if nth is 5, offset is best set as 0-4 to choose which fifth of items is selected.

2 replies

Inspiring
December 4, 2015

Omerpekin wrote:  ... in illustrator ... a lot of ... copied

In addition to Silly-V's feedback and without seeing your file, it may also be relative to the order and arrangement in which you copied and moved your lines as you set things up, and then how they are relative to each other when selected. Then again that may not play a role in it, but just wanted to mention it in case.

Qwertyfly___
Qwertyfly___Correct answer
Legend
December 7, 2015

1 loop is still fine.

silly is correct in saying you are changing your selection on each iteration.

so the simple way is to create array at start containing all selected items.

then deselect all, and then run back through your pre built array tagging every second one as selected

give this a shot:

var doc = app.activeDocument;

var selectedObjects = doc.selection;

doc.selection = null;

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

    if ((i+1) % 2 == 0 ){  // remove the +1 to select the opposite

        selectedObjects.selected = true;

    }

}

alert( doc.selection.length + " objects Selected");

edit:
should mention the 2 is the nth, and the 1 is the offest.

so if nth is 5, offset is best set as 0-4 to choose which fifth of items is selected.

Omerpekin
OmerpekinAuthor
Participant
December 7, 2015

thank you very much. This script runs with the logic I had in my mind I think. I haven't done any scripting in illustrator and I was a bit confused. I still dont understand why you are running doc.selection = null; that much but I will try to read the documentation a bit

Silly-V
Legend
December 4, 2015

Your code may have problems because the selection is changed every time as you deselect items, and the selection count is destroyed as you're working through it.

My snippet here puts the non-selected items into an imaginary collection, and deselects them after working through the selection of the document.

#target illustrator

function test(){

    var everyNth = 5;

    var doc = app.activeDocument;

    var deselectArr = [];

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

        if(i % everyNth != 0){

            deselectArr.push(doc.selection);

        }

    };

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

        deselectArr.selected = false;

    };

};

test();