Skip to main content
Inspiring
January 1, 2020
Answered

Need Help with Script to resize a single photo

  • January 1, 2020
  • 1 reply
  • 622 views

Hello:
I get a number of photos that need to be mug shot size, and I'm new to scripting.
I have this but when I run it, nothing happens, so I know I've got something wrong.

But I'm at a loss of what to do to correct the issue.  I've tried several different syntex, but I get error messages when I change things.

Any help would be appreicated.

Thanks, Larry
var mySelection = app.selection;
mySelection = mySelection.geometricBounds = ["0.7303 in", "1.0889 in"];

This topic has been closed for replies.
Correct answer Manan Joshi

Hi Larry,

Try the following code, by selecting the rectangle that contains the image

 

var mySelection = app.selection[0];
var gm = mySelection.geometricBounds
//Save the value, and set it to inches
var origUnit = app.scriptPreferences.measurementUnit
app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES
mySelection.geometricBounds = [gm[0], gm[1], gm[0] + 0.7303, gm[1] + 1.0889];
mySelection.fit(FitOptions.CONTENT_TO_FRAME)
//Restore the value
app.scriptPreferences.measurementUnit = origUnit

 

In your code you tried to change the geometricBounds of app.selection, however app.selection is a collection of objects. So i used just one of the objects of the selection using app.selection[0], if you have multiple items in selection you can get reference to them using the index likeapp.selection[0], app.selection[1] etc

 

GemetricBounds is an array with values like [y1, x1, y2, x2] giving the coordinates of top, left and bottom, right points. So you need to keep the top, left the same and add values to the bottom right to resize the rectangle. Now once that is done we also need to fit the image inside the rectangle, here i used the Content to Frame option you can choose any of the available values

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#FitOptions.html

 

Also all this can be done using the resize method of rectangle as well, try it

https://www.indesignjs.de/extendscriptAPI/indesign-latest/index.html#Rectangle.html#d1e217191__d1e220981

 

-Manan

1 reply

Manan JoshiCommunity ExpertCorrect answer
Community Expert
January 1, 2020

Hi Larry,

Try the following code, by selecting the rectangle that contains the image

 

var mySelection = app.selection[0];
var gm = mySelection.geometricBounds
//Save the value, and set it to inches
var origUnit = app.scriptPreferences.measurementUnit
app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES
mySelection.geometricBounds = [gm[0], gm[1], gm[0] + 0.7303, gm[1] + 1.0889];
mySelection.fit(FitOptions.CONTENT_TO_FRAME)
//Restore the value
app.scriptPreferences.measurementUnit = origUnit

 

In your code you tried to change the geometricBounds of app.selection, however app.selection is a collection of objects. So i used just one of the objects of the selection using app.selection[0], if you have multiple items in selection you can get reference to them using the index likeapp.selection[0], app.selection[1] etc

 

GemetricBounds is an array with values like [y1, x1, y2, x2] giving the coordinates of top, left and bottom, right points. So you need to keep the top, left the same and add values to the bottom right to resize the rectangle. Now once that is done we also need to fit the image inside the rectangle, here i used the Content to Frame option you can choose any of the available values

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#FitOptions.html

 

Also all this can be done using the resize method of rectangle as well, try it

https://www.indesignjs.de/extendscriptAPI/indesign-latest/index.html#Rectangle.html#d1e217191__d1e220981

 

-Manan

-Manan
Inspiring
January 1, 2020

Wow! That worked.
Thanks so much, Manan.
I've been banging my head against the wall trying to figure this out.

Thanks again.

Larry

rob day
Community Expert
Community Expert
January 1, 2020

Also, the bounds are an array of numbers [y1, x1, y2, x2]. If for some reason you do not want to set and reset the measurement prefs, it is possible to coerce the numbers into a string and use "in" as you did in your first post:

 

 

 

var mySelection = app.selection[0];
var gm = mySelection.geometricBounds
//1.0889" width x .7303" height
mySelection.geometricBounds = [gm[0], gm[1], (gm[0] + 0.7303 ).toString() + " in", (gm[1] + 1.0889).toString() + " in"];