/*
this script makes a line or group of lines dashed, with random values within a user defined range
it's kind of fun to use on lots and lots of lines at once. Why not use it with the spiderweb script or wall blazer?
After all, who doesn't love a little randomness?

added in v2: not much really, just the ability to randomly assign stroke width too.
*/
var maxdash = 20; //maxumum dash length
var mindash = 2; //minimum dash length
var maxgap = 20; //maxumum gap length
var mingap = 2;  //minimum gap length
var dashes = 6; //this seems to be the maximum number of different dashes and gaps illustrator can use.
var dasharray = [1,2,3]; //an array used to give lines their dashes and gaps.
var minStroke = .5; // minstroke and maxstroke define the range for the randomized stroke
var maxStroke = 3;




// ----------------------------------------------
  
  // specify values by prompt
  var ans = Dialog.prompt("Random Dashes", [
    { value: maxdash, description:"Maximum Dash (in points)"},
	{ value: mindash, description:"Mimimum Dash (in points)"},
	{ value: maxgap, description:"Maximum Gap (in points)"},
	{ value: mingap, description:"Mimimum Gap (in points)"},
	{ value: minStroke, description: "minimum stroke width (in points)"},
	{ value: maxStroke, description: "maximum stroke width (in points)"}
    ]);
	if (ans != null) {
		maxdash = ans[0];
		mindash = ans[1];
		maxgap = ans[2];
		mingap = ans[3];
		minStroke = ans[4];
		maxStroke = ans[5];
  
	   var sel = activeDocument.getMatchingItems(Path, { selected: true }); //puts all selected items in an array called sel
		for (var i = 0; i < sel.length; i++) { //this loop cycles through all selected items
				art = sel[i];
				art.style.stroke.dashArray = randomarray(); //apply a random stroke to the line
				art.style.stroke.width = minStroke + Math.random() * (maxStroke - minStroke); // sets the random stroke
		}
	}


function randomarray(){ //function for making an array of random numbers
	for (var i=0; i<dashes; i+=2){
		dasharray[i] = (Math.random() * (maxdash - mindash) + mindash); //random value for the dash, between the numbers mindash and maxdash
		dasharray[i+1] = (Math.random() * (maxgap - mingap) + mingap); //random value for the dash, between the numbers mingap and maxgap
	}
	return dasharray;
	
}

