﻿//
// Resize Selected Items
// v 2.0
// by John Pobojewski
//
// Resizes a selection of multiple page items proportionally
//

app.doScript (main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.entireScript, "Resize Selected Items");

function main(){
	var _sel = app.selection;
	var w = 0;
	var h = 0;
	var origHU = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
	var origVU = app.activeDocument.viewPreferences.verticalMeasurementUnits;
	var TRANSFORM_WIDTH    = true;
	var TRANSFORM_HEIGHT   = true;
	var WIDTH_IS_RELATIVE  = false;
	var HEIGHT_IS_RELATIVE = false;

	app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
	app.activeDocument.viewPreferences.verticalMeasurementUnits   = MeasurementUnits.points;

	if (_sel.length > 0){
		var ARE_SAME_DIMENSIONS = true;
		for (var i=0; i<_sel.length; i++){
			var obj = _sel[i];
			var ow = obj.geometricBounds[3] - obj.geometricBounds[1]
			var oh = obj.geometricBounds[2] - obj.geometricBounds[0];
			if (i > 0){
				if (w != ow || h != oh) ARE_SAME_DIMENSIONS = false;
			}		
			w = ow;
			h = oh;
		}
		getDialog(ARE_SAME_DIMENSIONS);
	} else {
		alert("Please select some items to resize.");
	}

	function getDialog(ASSIGN_DIMENSIONS){
		var Dialog;
		//Create the SelectObjects dialog box.
		with(Dialog = app.dialogs.add({name:"Resize Items"})){
			with(dialogColumns.add()){
				with(borderPanels.add()){
					with(dialogColumns.add()){
					  var transformWidth = checkboxControls.add({staticLabel:"Width:", checkedState:true});			
						if (ASSIGN_DIMENSIONS){
							var widthBox = measurementEditboxes.add({editValue:w, editUnits:origHU});
						} else {
							var widthBox = measurementEditboxes.add({editValue:0, editUnits:origVU});
						}
						var widthIsRelative = checkboxControls.add({staticLabel:"Relative", checkedState:false});				
					}
					with(dialogColumns.add()){					
					  var transformHeight = checkboxControls.add({staticLabel:"Height:", checkedState:true});						
						if (ASSIGN_DIMENSIONS){
							var heightBox = measurementEditboxes.add({editValue:h, editUnits:origHU});
						} else {
							var heightBox = measurementEditboxes.add({editValue:0, editUnits:origVU});
						}
						var heightIsRelative = checkboxControls.add({staticLabel:"Relative", checkedState:false});	
					}	
				}
				//staticTexts.add({staticLabel:"©2010 J Pobojewski", minWidth: 50});
			}
		}
		myResult = Dialog.show();
		if (myResult == true){
			TRANSFORM_WIDTH  = transformWidth.checkedState;
			TRANSFORM_HEIGHT = transformHeight.checkedState;
			WIDTH_IS_RELATIVE  = widthIsRelative.checkedState;
			HEIGHT_IS_RELATIVE = heightIsRelative.checkedState;		
			w = widthBox.editValue;
			h = heightBox.editValue;

			//Remove the dialog from memory.
			resizeItems();
			Dialog.destroy();
		}
		else{
			//Remove the dialog from memory.
			Dialog.destroy();
		}
	}

	function resizeItems(){
		for (var i=0; i<_sel.length; i++){
			var obj = _sel[i];
			var anchor = app.activeWindow.transformReferencePoint;
			var ow = obj.geometricBounds[3] - obj.geometricBounds[1]
			var oh = obj.geometricBounds[2] - obj.geometricBounds[0];

			if (TRANSFORM_WIDTH){
				if (WIDTH_IS_RELATIVE){
					var pw = (w + ow)/ow; 
				} else {
					var pw = w/ow; 			
				}
			} else {
				var pw = 1.0;
			}

			if (TRANSFORM_HEIGHT){
				if (HEIGHT_IS_RELATIVE){
					var ph = (h + oh)/oh;
				} else {
	 				var ph = h/oh; 
				}
			} else {
				var ph = 1.0;
			}

			var matrix = app.transformationMatrices.add({horizontalScaleFactor: pw, verticalScaleFactor: ph});
			obj.transform(CoordinateSpaces.pasteboardCoordinates, anchor, matrix);
		}	

		app.activeDocument.viewPreferences.horizontalMeasurementUnits = origHU;
		app.activeDocument.viewPreferences.verticalMeasurementUnits   = origVU;
	}
}


