Skip to main content
scheasbro
Participant
October 11, 2023
Question

Script to resize, rotate, and center images on specific pages

  • October 11, 2023
  • 1 reply
  • 655 views

Hello,

I am a veteran ID user here, but still pretty green on scripting. I had taken a JS class once about three years ago, but it's one of those use-it-or-lose-it things, I think. I don't remember much, but I remember enough to read the code and see if it would work for me.

Currently, I'm working on a 250-300 page document. About 1/2 of those pages have an image on it that is oversized for the page. I would like to have a script that will allow me to select the pages that I want to work with (From page to page), scale the image proportionally (either by percentage, dimensions in inches, or by entering one value - height or width and allowing the secondary value to adjust proportionally. Additionally, it is the image itself that I want to resize - the frame needs to adjust proportionally to the graphic.

Lastly, I want to rotate the image 90º counterclockwise and then center vertically and horizontally to the page margins.

I have scoured the internet for a similar code - even one that I could modify to fit my needs but each time I've tried to modify something, I've managed to break it. So, I'll let it to the code gurus, and I'll stick with my page layout skills. 🙂

So, to recap, I need a script that will:

1. Choose the specific range of pages (From page XX to page  XX) that I need to adjust.
2. Scale the images to a specific percentage or dimension (inches) and/or enter one value and have the secondary value auto-size proportionally. The frame needs to adjust proportionally to the image.
3. Rotate images 90 counterclockwise.
4. Center the images vertically and horizontally to page margins.

Can someone help - pretty please? 😄

Here is a link to a script that I've used with success to scale the images but it does not have a rotate and center to margins feature to it: https://drive.google.com/file/d/1wGE7IBHtGlYh_N0PqgtOcTE56Qf92pfk/view?usp=sharing

This topic has been closed for replies.

1 reply

rob day
Community Expert
Community Expert
October 11, 2023

Hi @scheasbro , This would fit a selected frame to the margins and rotate its graphic 90—script expects a selected parent frame. Choosing a range of pages is doable, but more complex and would involve a dialog construction:

 

 

app.doScript(fitImage, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Fit Image');


function fitImage(){
    //check for a selection
    if (app.activeDocument.selection.length > 0 && app.activeDocument.selection[0].graphics.length > 0) {
        var f = app.activeDocument.selection[0];
    } else {
        alert("Please Select a Frame.")
        return
    }
    app.activeDocument.zeroPoint = [0,0]; 

    //rotate the frame’s graphic 90
    var fc = f.graphics[0];
    fc.absoluteRotationAngle = 90;

    //gets the page bounds
    var pb = f.parentPage.bounds;
    //tha page margins
    var m = [f.parentPage.marginPreferences.top, f.parentPage.marginPreferences.left, f.parentPage.marginPreferences.bottom, f.parentPage.marginPreferences.right];
    //set the frame’s bounds to the margins
    f.geometricBounds = [pb[0]+m[0], pb[1]+m[1], pb[2]-m[2], pb[3]-m[3]];
    //fit the image
    f.fit(FitOptions.PROPORTIONALLY);
}

 

Before and after: