
/******************************************************************************/

// CROP MARKS v1.01
// A script for Scriptographer by Jorge Fuentes, 2012.
// http://www.jorgefuentes.net

// Easily create crop marks for one or multiple objects
// in Illustrator across artboards, time and space.

/******************************************************************************/

// Default values. Change these to your personal preference.
// Remember: lineLength, offset and lineStroke are defined in POINTS
// even if they get converted later to mm in the palette!
var values =
{
lineLength: 14.1732283,
offset: 5.66929134,
lineStroke: 0.5,
lineColor: {red: 0.2, green: 0.2, blue: 0.2},
newLayer: true,
groupItems: true,
eachObj: true,
entSel: false,
createCropMarks: 'Create Crop Marks!'
};


// Create components for the dialog window
var components = 
{
	
	lineLength: { type: 'number', label: 'Length', units: 'millimeter', min: 2.83464567, increment: 2.83464567 },
	offset: { type: 'number', label: 'Offset', units:'millimeter', increment: 2.83464567 },
	lineStroke: { type: 'number', label: 'Stroke weight', units: 'point', min: 0.01, increment: 0.25 },
	lineColor: { type: 'color', label: 'Stroke color' },

	resetValues:
	{
		type: 'button',
		value: 'Reset values',
		onClick: function() 
		{
		components.lineLength.value = components.lineLength.defaultValue;
		components.offset.value = components.offset.defaultValue;
		components.lineStroke.value = components.lineStroke.defaultValue;
		components.lineColor.value = components.lineColor.defaultValue;
		}
	},

	ruler1: { type: 'ruler' },
	
	dummyText: {type: 'text', label: 'Draw marks around' },
	
	eachObj:
	{
		type: 'boolean', label: 'Each object',  
		
		onChange: function(value)
		{
			components.entSel.value = !value;
		} 		
	},
	
	entSel:
	{
		type: 'boolean', label: 'Entire selection',
		
		onChange: function(value)
		{ 
			components.eachObj.value = !value;
		} 		
	},
	
	ruler2: {type: 'ruler' },
	
	newLayer: { type: 'checkbox', label: 'Create in new layer' },
	groupItems: { type: 'checkbox', label: 'Group crop marks' },
	
	
	createCropMarks:
	{
	type: 'button',
	// This is where we launch the main function and magic happens!
		onClick: function()
		{
		
			// Get selected items
			var selectedItems = document.getItems({	selected: true });
			
			// Check if the user has actually selected something
			if (selectedItems.length >0)
			{
				// then, if "Draw marks around each object" is selected...
				if (values.eachObj)
				{
					var item = [];
					
					for (var i = 0; i<selectedItems.length; i++)
					{
					
					item[i] = selectedItems[i];
					// Action!
					cropMarks(item[i]);
					}
				}
				
				// else, if "Draw marks around entire selection" is selected
				else
				{
					var item;
					var minX = 99999999;
					var minY = 99999999;
					var maxX = -99999999;
					var maxY = -99999999;
					
					// check for the min and max X and Y coordinates of the selected items
					// create the rectangle enclosing all the selected items
					// and finally launch the function with that rectangle as parameter
					for (var i = 0; i<selectedItems.length; i++)
					{
					if (selectedItems[i].strokeBounds.left < minX) minX = selectedItems[i].strokeBounds.left;
					if (selectedItems[i].strokeBounds.right > maxX) maxX = selectedItems[i].strokeBounds.right;
					if (selectedItems[i].strokeBounds.top < minY) minY = selectedItems[i].strokeBounds.top;
					if (selectedItems[i].strokeBounds.bottom > maxY) maxY = selectedItems[i].strokeBounds.bottom;
					}
					
					item = new Rectangle(new Point(minX,minY), new Point(maxX,maxY));
					// Action!
					cropMarks(item);
				
				}
				
				
			}
			else
			{
				Dialog.alert('Please select an item first!');
			}
		}
	}
	
	
};



// Launch the palette
var thePalette = Palette('Crop Marks', components, values);


// Main function
function cropMarks(el)
{

	if (values.eachObj)
	{
		// Get points from item bounds, stroke width included
		var tl = el.strokeBounds.topLeft;
		var tr = el.strokeBounds.topRight;
		var bl = el.strokeBounds.bottomLeft;
		var br = el.strokeBounds.bottomRight;
	}
	else 
	{
	
		var tl = new Point(el.x, el.y);
		var tr = new Point(el.x + el.width, el.y);
		var bl = new Point(el.x, el.y + el.height);
		var br = new Point(el.x + el.width, el.y + el.height);
		
	}
	
	// create a couple of variables we'll need in a minute...
	var offset = values.offset;
	var lineLength = values.lineLength + offset;
	var lineStroke = values.lineStroke;
	var lineColor = values.lineColor;
	
	// If checkbox "Create crop marks in new layer" is selected...
	// Check if there's already a layer called 'Crop Marks'.
	if (values.newLayer)
	{
		// If there is one, assign it to a variable and activate it.
		for (var i=0; i<document.layers.length; i++)		
		{	
			if (document.layers[i].name == 'Crop Marks' )
			{
			var myLayer = document.layers[i];
			myLayer.activate();
			break;
			}
		}
		// If not, create one.
		if (!myLayer)
		{
			var myLayer = new Layer();
			myLayer.name = 'Crop Marks';
			myLayer.activate();	
		}
	}
	
	// Draw crop marks for the top left corner
	var vpath_tl = new Path();
	vpath_tl.add(new Point(tl.x, tl.y - offset), new Point(tl.x, tl.y-lineLength));
	var hpath_tl = new Path();
	hpath_tl.add(new Point(tl.x - offset, tl.y), new Point(tl.x - lineLength, tl.y));
	
	// Draw crop marks for the top right corner
	var vpath_tr = new Path();
	vpath_tr.add(new Point(tr.x, tr.y - offset), new Point(tr.x, tr.y-lineLength));
	var hpath_tr = new Path();
	hpath_tr.add(new Point(tr.x + offset, tr.y), new Point(tr.x + lineLength, tr.y));
	
	// Draw crop marks for the bottom left corner
	var vpath_bl = new Path();
	vpath_bl.add(new Point(bl.x, bl.y + offset), new Point(bl.x, bl.y+lineLength));
	var hpath_bl = new Path();
	hpath_bl.add(new Point(bl.x - offset, bl.y), new Point(bl.x - lineLength, bl.y));
	
	// Draw crop marks for the bottom right corner
	var vpath_br = new Path();
	vpath_br.add(new Point(br.x, br.y + offset), new Point(br.x, br.y+lineLength));
	var hpath_br = new Path();
	hpath_br.add(new Point(br.x + offset, br.y), new Point(br.x + lineLength, br.y));

	
	// Set Stroke weight and color for all the lines
	vpath_tl.strokeWidth = lineStroke;
	hpath_tl.strokeWidth = lineStroke;
	vpath_tr.strokeWidth = lineStroke;
	hpath_tr.strokeWidth = lineStroke;
	vpath_bl.strokeWidth = lineStroke;
	hpath_bl.strokeWidth = lineStroke;
	vpath_br.strokeWidth = lineStroke;
	hpath_br.strokeWidth = lineStroke;

	vpath_tl.strokeColor = lineColor;
	hpath_tl.strokeColor = lineColor;
	vpath_tr.strokeColor = lineColor;
	hpath_tr.strokeColor = lineColor;
	vpath_bl.strokeColor = lineColor;
	hpath_bl.strokeColor = lineColor;
	vpath_br.strokeColor = lineColor;
	hpath_br.strokeColor = lineColor;
	
	// Deselect the item
	el.selected = false;

	// If checkbox "Group crop marks" is selected...
	// Create a group and append the crop marks to it
	if (values.groupItems)
	{
		var myGroup = new Group();
		myGroup.name = 'Crop Marks';
		
		myGroup.appendTop(vpath_tl);
		myGroup.appendTop(hpath_tl);
		myGroup.appendTop(vpath_tr);
		myGroup.appendTop(hpath_tr);
		myGroup.appendTop(vpath_bl);
		myGroup.appendTop(hpath_bl);
		myGroup.appendTop(vpath_br);
		myGroup.appendTop(hpath_br);
		
		myGroup.selected = true;
	}
		// Select the crop marks.
		// This way we can delete the crop marks right away if we're not happy with them.
		else 
		{
			vpath_tl.selected = true;
			hpath_tl.selected = true;
			vpath_tr.selected = true;
			hpath_tr.selected = true;
			vpath_bl.selected = true;
			hpath_bl.selected = true;
			vpath_br.selected = true;
			hpath_br.selected = true;
		}	
}

