//Hello! This script will resize any object(s) you have selected in InDesign to a specified width – in this case, 50% of the width of the page – and adjust the height so that the object retains its aspect ratio.
//Useful for: Working through a document with a lot of images that need to be the exact same size (say, QR codes).

var magicNumber = 50;
var document = app.activeDocument.documentPreferences;
var docWidth = document.pageWidth;
var magicNumber = (docWidth / 100) * magicNumber;

try {
var selection = app.selection;
for (i = 0; i < selection.length; i++){
	var selectedObject = app.selection[i];
app.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;  
app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;  
app.activeDocument.zeroPoint = [0,0];  

var page = app.activeWindow.activePage;
if ('graphics' in selectedObject && selectedObject.graphics.count() > 0){
	var graphic = selectedObject.graphics[0];
	var imageBool = true;
} else {
	var imageBool = false;
}



var gb = selectedObject.geometricBounds;
if (i == 0){
	var leftEdge = gb[1];
}

if (imageBool){
	var ib = selectedObject.graphics[0].geometricBounds;
	var difY = gb[0] - ib[0];
	var difX = gb[1] - ib[1];

	
	var gWidth = ib[3] - ib[1];
	var gHeight = ib[2] - ib[0];
	
	
}
var startWidth = gb[3] - gb[1];
var startHeight = gb[2] - gb[0]

var ratio = startHeight / startWidth;


    var startPosX = leftEdge;
    var endPosX = (leftEdge + magicNumber);



    var startPosY = gb[0];
    var endPosY = startPosY + (magicNumber * ratio);


selectedObject.geometricBounds = [startPosY, startPosX, endPosY, endPosX];

var endWidth = selectedObject.geometricBounds[3] - selectedObject.geometricBounds[1];
var endHeight = selectedObject.geometricBounds[2] - selectedObject.geometricBounds[0];




if (imageBool){
	var incrRatioX = endWidth / startWidth;
	var incrRatioY = endHeight / startHeight;

	var newDifY = difY * incrRatioY;
	var newDifX = difX * incrRatioX;
	var newWidth = gWidth * incrRatioX;
	var newHeight = gHeight * incrRatioY;
	
	var newX = selectedObject.geometricBounds[1] - newDifX
	var newY = selectedObject.geometricBounds[0] - newDifY;
	
	selectedObject.graphics[0].geometricBounds = [newY, newX, newY + newHeight, newX + newWidth];

	
}
// selectedObject.fit(FitOptions.FILL_PROPORTIONALLY);
}
} catch (e) {
	alert("Something didn't work. Make sure you've only got objects selected. Not. Text. Do you have text selected? Because that might be why this didn't work. I'm just saying, you're going to feel really stupid when it turns out you had text selected.");
}

