Skip to main content
Inspiring
August 26, 2013
Question

Random Delete a selection of

  • August 26, 2013
  • 2 replies
  • 2908 views

Hi,

I am trying to create a script to specify a percentage of the selected items you want to remove.

Here is what I have so far.

I still need to work out on the alert prompt and the if statement, but for now, I have the feeling that the selection.length keep changing each time Illustrator delete an object, and I don't know how to make it stick.

var selection = app.activeDocument.selection;

//alert prompt dialog for a percentage of deletion

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

    if (Math.random < 0.5) { //need to work out the percentage

        alert("yes")

        selection.remove();

    }

}

Thanks for the help,

This topic has been closed for replies.

2 replies

CarlosCanto
Community Expert
Community Expert
August 27, 2013

check this thread, I have a similar script, adapt the concepts to your script and let us know how it goes.

http://forums.adobe.com/message/5004635

Participant
June 10, 2024

the link has stopped working

Monika Gause
Community Expert
Community Expert
June 10, 2024
quote

the link has stopped working


By @Yurooms3709216025re

 

This thread is 11 years old. 

Inspiring
August 26, 2013

When removing items with script reverse the loop… like so…

var select = app.activeDocument.selection;

var count = select.length;

for ( var i = count-1;  i >= 0;  i-- ) {

    if ( Math.random < 0.5 ) {

 

        alert("yes");

 

        select.remove();

    };

 

};

That way the object indexes won't get broken ( out of range ) as the array reduces…

Inspiring
August 29, 2013

Thanks for the cue, didn't know that one!

I tried to work it out with this solution and part of my old Scriptographer code. I'm able to delete all the items when I imput 100%, but only 20% when I imput 50%, and don't understand why…

var sel = app.activeDocument.selection;

var count = sel.length;

if ( count === 0 ) {

          alert ("You need to select at least one item")

} else {

          var percentage = Number(prompt ("Which percentage to delete?", 50, "Delete Random Items"));

          for ( var i = count-1;  i >= 0;  i-- ) {

                    var object = sel;

                    if ( Math.random() <= percentage/100) {

                              object.remove();

                    }

          }

};

function random(minr, maxr) {

          return minr + Math.random() * (maxr - minr);

}

Inspiring
August 29, 2013

I also tried to work on another version, based on CarlosCanto's solution. Here is what I have. One of the thing I don't understand is why I have 11 randVal… and the second one is that the randVal are repeating (0,1,2,3,3,3,4,5,5,5,10). Therefore, even if I imput 100%, It certanly won't delete all the items in the array, only particular ones.

var select = app.activeDocument.selection;

var count = select.length;

if ( count === 0 ) {

          alert ("You need to select at least one item")

} else {

          var promptPercentage = Number(prompt ("Which percentage to delete?", 50, "Delete Random Items"));

          var percentage = count * promptPercentage / 100;

          var deselect = [];

          for ( i = 0 ; i < percentage ; i++ ) {

                    var anumber = randomXToY (0, count); //random number

                    //alert(anumber);

                    deselect.push(select[anumber]);

          }

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

                    deselect.remove ();

          }

};

//function to get random number between values, by Roshan Bhattarai

function randomXToY ( minVal, maxVal, floatVal ) {

          var randVal = minVal + ( Math.random() * (maxVal - minVal ) );

          // alert(randVal)

          return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);

}