Skip to main content
Participating Frequently
February 27, 2021
Question

Is there a script that aligns all images in a document to the center of the page?

  • February 27, 2021
  • 4 replies
  • 2484 views

So I already have a script that selects all the images in a document and resizes them (see below) which I use all the time. Was wondering if there were some lines I could add to the script (or a seperate script) that would align all the images to the center (both horizontially and vertically) of their respective pages. I'm working with 300-page documents containing off-center images so an automated centering script would save me a lot of manual work.

 

Current script for selecting/resizing all images:

var myDoc =  app.activeDocument;
var graphlength = myDoc.allGraphics.length;
for(a=0; a<graphlength; a++)
{
myDoc.allGraphics[a].absoluteHorizontalScale = 113;
myDoc.allGraphics[a].absoluteVerticalScale = 113;
}

 

4 replies

Mike Witherell
Community Expert
Community Expert
May 2, 2025

An Object Style would accomplish this same task, would it not?

Mike Witherell
Community Expert
March 8, 2021

Hi Peter,

I ran your code from the ESTK on Windows 10 on an unsaved document with InDesign 2021 that had no page items at all. Just one page in a new facing-pages document with InDesign 16.1.0.20.

 

The code was running very long, just a few seconds, and finally InDesign crashed.

To make sure that the crash was not caused by something else, I ran it several times on a new document without contents. Everytime InDesign 16.1.0.20 crashed.

pages = app.activeDocument.pages.everyItem().getElements();
for (i = 0; i < pages.length; i++) {
  pageItems = pages[i].pageItems.everyItem().getElements();
  app.activeDocument.align (pageItems, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
}

 

So perhaps we should check for items on a page before running align().

pages = app.activeDocument.pages.everyItem().getElements();

for (i = 0; i < pages.length; i++)
{
	if( pages[i].pageItems.length == 0 ){ continue };

	pageItems = pages[i].pageItems.everyItem().getElements();
	app.activeDocument.align
	( 
		pageItems, 
		AlignOptions.HORIZONTAL_CENTERS, 
		AlignDistributeBounds.PAGE_BOUNDS
	);
};

 

Regards,
Uwe Laubender

( ACP )

Peter Kahrel
Community Expert
Community Expert
March 8, 2021

Quite right. I can reproduce that, thanks. Always wise to look before you leap.

 

So, eerbes, there's your answer.

 

P.

BarlaeDC
Community Expert
Community Expert
March 1, 2021

Hi,

 

I think this should be able to do i.

 

var mydoc = app.activeDocument;
var pageItems = // an Array of all the items you with to align.
mydoc.align ( pageItems, AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS  );
mydoc.align ( pageItems, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS  );

 

further information here -https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html

 

 

Malcolm

eerbesAuthor
Participating Frequently
March 2, 2021

Thanks for the suggestion Malcolm. Your script looks promising but because I'm a total newbie at scripting I'm still having trouble figuring out the correct term to enter in the Array section to select all the images.

 

I tried "allGraphics" and got the error "allGraphics is undefined." I tried "myDoc.allGraphics" and got the error "myDoc is undefined." I reviewed your link and realize the correct term is in there somewhere but I couldn't quite figure it out. Any help?

 

My current script:

var mydoc = app.activeDocument;
var pageItems = myDoc.allGraphics;
mydoc.align ( pageItems, AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS  );
mydoc.align ( pageItems, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS  );

Thanks,

Eric

Peter Kahrel
Community Expert
Community Expert
March 2, 2021

Eric -- allGraphics returns an array of images, but you want to use the rectangles that the images live in. Also, Malcolm's snippet (and Adobe's documentation) suggest that you can target the whole document, but that's not the case: myDoc.align targets pages. So you want to loop through the document's pages and target all rectangles on each page:

 

pages = app.activeDocument.pages.everyItem().getElements();
for (i = 0; i < pages.length; i++) {
  pageItems = pages[i].pageItems.everyItem().getElements();
  app.activeDocument.align (pageItems, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
}

 

Peter 

Sunil Yadav
Legend
February 27, 2021

Try this snippet : 

var myDoc =  app.activeDocument;
var graphlength = myDoc.allGraphics.length;
for(a=0; a<graphlength; a++){
    var tempGraphics = myDoc.allGraphics[a];
    while(tempGraphics.parent.constructor.name != "Spread"){
        tempGraphics = tempGraphics.parent;
        }
    if(tempGraphics.parentPage != null){
        tempGraphics.parentPage.select();
        var width = tempGraphics.geometricBounds[3]-tempGraphics.geometricBounds[1];
        var pageWidth = myDoc.documentPreferences.pageWidth;
        tempGraphics.move([(pageWidth/2)-(width/2), tempGraphics.geometricBounds[0]]);
        }
    }

Best

Sunil

eerbesAuthor
Participating Frequently
March 1, 2021

Thanks for your reply. I tested out your script and it did select and move all the images but unfortunately it did not center them. As you can see in the attached screenshots both images were moved to the right page for some reason. The method I use to manually center the images is to select each image, click on "Align horizontal centers" and "Align vertical centers" with the "Align to Page" option selected in the Align panel. Is there a way to automate these steps using scripts?