Random Delete a selection of
Copy link to clipboard
Copied
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,
Explore related tutorials & articles
Copy link to clipboard
Copied
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…
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
yes, the random function may get repeated values, it may not, next thing to do is parse the array for unique values, if the next random value exist in the array then ignore it and keep going until the array is filled with the desired number of values (unique values)
Copy link to clipboard
Copied
Thank you all for the help!
I finaly created it with a suffle. I think it's the more elegant way of doing it.
Copy link to clipboard
Copied
randomizing the selection works as well, good job, thanks for sharing a final version.
Copy link to clipboard
Copied
check this thread, I have a similar script, adapt the concepts to your script and let us know how it goes.
Copy link to clipboard
Copied
the link has stopped working
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You can access @CarlosCanto's link through Wayback Machine (if it's not blocked by your ISP):
http://web.archive.org/web/20141209031628/http://forums.adobe.com/message/5004635
Copy link to clipboard
Copied
I don't have Gernouille's script but here's mine from @femkeblanco link
var idoc = app.activeDocument;
var sel = idoc.selection;
var selcount = sel.length;
var title = "Deselect Random Items";
var items = Number(prompt ("How many items to deselect?", 5, title));
var deselect = [];
for (i=0; i<items; i++) {
var anumber = randomXToY (0, selcount); //random number
//alert(anumber);
deselect.push(sel[anumber]);
}
for (j=0; j<deselect.length; j++) {
deselect[j].selected = false;
}
//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);
}
![](/skins/images/875F3109337FA4BCEB72FFB02E17B763/responsive_peak/images/icon_anonymous_message.png)
![](/skins/images/875F3109337FA4BCEB72FFB02E17B763/responsive_peak/images/icon_anonymous_message.png)