4 Replies Latest reply on Feb 21, 2013 2:06 PM by Gernouille

    Random Delete tool with Scriptographer

    Gernouille

      Hi, I am trying to refine the RandomTools created for Scriptographer so that it suits my preference, but I can't figure it out in Javascript. Not knowing a lot of Javascript don't help me either!

      Here is the script, simplified to conserv only the Delete part. What I would like to do is to enter a percentage, and then this would be the percentage of selected object removed.

      If anyone would point me to a direction with that, It would be really great.

       

      /**
       *  randomDelete 0.4
       *  Base on
       *  Ken Frederick
       *  ken.frederick@gmx.de
       *  http://cargocollective.com/kenfrederick/
       *  http://kenfrederick.blogspot.com/
       *
       *  Modified by Jolin Masson
       *  http://jolinmasson.com/
       *
       *          this script uses stuff borrowed from keegan's "rando-color.js"
       *          as well as from some other things i had written in processing
       *
       */
      
      
      script.coordinateSystem = 'bottom-up';
      script.angleUnits = 'radians';
      
      
      var sel;
      
      
      var values = {
                b_delete: true,
                val_deleteAmt: 2,
      };
      
      
      // gui components
      var components = {
                b_delete: { 
                          onChange: function(value) {
                                    components.val_deleteAmt.enabled = value;
                          }
                },
      
      
                val_deleteAmt: { 
                          type: 'number',
                          fullSize: true,
                          enabled: true
                },
      
      
                deleteRule: { 
                          type: 'ruler',
                          fullSize: true,
                },
      
      
                submit: { 
                          type: 'button', 
                          value: 'Apply',
                          onClick: function() {
                                    Main();
                          }
                }
      };
      
      
      // Setup
      function Setup() {
                // initialize the palette window
                var palette = new Palette('Random Delete 0.5', components, values);
      }
      
      
      // Update
      function Update() {
      }
      
      
      // Main
      function Main() {
                // document properties
                sel = activeDocument.getItems( { type: Item, selected: true } );
      
      
                //adjust delete range values based on selection number
                components.val_deleteAmt.range = [0, sel.length-1];
      
      
                // loop through selected items
                for ( i in sel ) {
                          var object = sel[i];
      
      
                          if( object.isValid() ) {
                                    // ------------------------------------
                                    // delete
                                    // ------------------------------------
                                    if ( values.b_delete ) {
                                              rand = parseInt( Math.random()*values.val_deleteAmt );
                                              if (rand == 0) object.remove();
                                    }
                          } //end isValid()
                }
      } //end Main()
      
      
      
      
      // ------------------------------------------------------------------------
      // methods
      // ------------------------------------------------------------------------
      function random(minr, maxr) {
                return minr + Math.random() * (maxr - minr);
      }
      
      
      
      
      // ------------------------------------------------------------------------
      // execution
      // ------------------------------------------------------------------------
      Setup();
      Update();
      

       

       

      Thanks