Skip to main content
Participant
November 6, 2023
Answered

[Javascript]How to scale an image using a script.

  • November 6, 2023
  • 2 replies
  • 1028 views

Does anyone have javascript code that will successfully resize my image after placing?

var newGraphic = newFrame.place(img_file)[0];

I've spent hours and all the techniques posted all over the web don't work or prduce the dreaded...

"This value would cause one or more objects to leave the pasteboard."

My image is 72dpi jpg 448x2772.

 

The only reason I'm trying to rescale it is that the place() function brings it in at a giant size.

This topic has been closed for replies.
Correct answer GNDGN

I recommend creating a frame in the desired size first and then placing the image in it:

var imgFrame = app.activeWindow.activePage.rectangles.add({
	geometricBounds: ["0px", "0px", 2772/2 + "px", 448 / 2 + "px"]
});

var imgFile = new File("/path/to/your/image.jpg");
imgFrame.place(imgFile);
imgFrame.fit(FitOptions.PROPORTIONALLY);

 

2 replies

rob day
Community Expert
Community Expert
November 7, 2023

the dreaded..."This value would cause one or more objects to leave the pasteboard."

 

Hi @defaultzzc0qcmhdex6 , An alternate way to do this is by transforming the image, which avoids the pasteboard problems. This function places the selected image on the page and transforms it and its frame to the provided parameter as inches:

 

 

//parameter is the tansformed image width as inches
placeSize(5)

/**
* Transforms the placed image to the provided width in inches 
* @ param pw the placed width 
* @ return the image parent frame 
*/
function placeSize(pw){
    app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES;
    var f = File.openDialog("Select the file", "");
    var img = app.activeWindow.activePage.place(File(f))[0];
    var aw = img.geometricBounds[3]-img.geometricBounds[1];
    img.transform(CoordinateSpaces.innerCoordinates, AnchorPoint.TOP_LEFT_ANCHOR, app.transformationMatrices.add({horizontalScaleFactor:pw/aw, verticalScaleFactor:pw/aw}));
    img.parent.fit(FitOptions.FRAME_TO_CONTENT);
    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
    return img.parent
}

 

 

 

 

This image, which has an output dimension of 135" x 92" (9725 x 6625 px @ 72ppi), gets placed at 5" x 3.4062" with an Effective Resolution of 1945 ppi:

 

 

GNDGN
GNDGNCorrect answer
Inspiring
November 6, 2023

I recommend creating a frame in the desired size first and then placing the image in it:

var imgFrame = app.activeWindow.activePage.rectangles.add({
	geometricBounds: ["0px", "0px", 2772/2 + "px", 448 / 2 + "px"]
});

var imgFile = new File("/path/to/your/image.jpg");
imgFrame.place(imgFile);
imgFrame.fit(FitOptions.PROPORTIONALLY);

 

____________________Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5
Participant
November 6, 2023

Thank you. That works!

Now to figure out how to slide the image around inside the frame.

Robert at ID-Tasker
Legend
November 6, 2023

You can use move().