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

Random Delete a selection of

Explorer ,
Aug 25, 2013 Aug 25, 2013

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,

TOPICS
Scripting

Views

1.9K

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
Adobe
Guru ,
Aug 26, 2013 Aug 26, 2013

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…

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 ,
Aug 28, 2013 Aug 28, 2013

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

}

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 ,
Aug 28, 2013 Aug 28, 2013

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

}

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 28, 2013 Aug 28, 2013

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)

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 ,
Aug 31, 2013 Aug 31, 2013

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.

You can get it here.

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 31, 2013 Aug 31, 2013

Copy link to clipboard

Copied

randomizing the selection works as well, good job, thanks for sharing a final version.

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 26, 2013 Aug 26, 2013

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.

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

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 ,
Jun 10, 2024 Jun 10, 2024

Copy link to clipboard

Copied

the link has stopped working

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 ,
Jun 10, 2024 Jun 10, 2024

Copy link to clipboard

Copied

quote

the link has stopped working


By @Yurooms3709216025re

 

This thread is 11 years old. 

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
Guide ,
Jun 10, 2024 Jun 10, 2024

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

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 ,
Jun 11, 2024 Jun 11, 2024

Copy link to clipboard

Copied

LATEST

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

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