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

Community Beginner ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

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;
}

 

TOPICS
Scripting

Views

182

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

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?

ID centering ref.jpg

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

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 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 06, 2021 Mar 06, 2021

Copy link to clipboard

Copied

Thanks for your response Peter. I ran your script on my test document using the latest version of InDesign and it didn't work for me (it caused the program to crash after a few seconds). Maybe it's something I'm doing wrong (I've just been copying the code to a notepad document and saving it with a .jsx extension) or my template is somehow not compatible with the script. Given that I've tried three different scripts with three different approaches and haven't achieved the desired outcome it's looking like there isn't a simple, commonly-used script for center-aligning images out there. I'd of course love to be proven wrong but in the meantime I'll continue to manually align the images/rectangles. Maybe at some point I can learn a bit more about scripting and apply it to some of the suggested code here to figure out a solution that works for my specific needs. I appreciate all the feedback and suggestions!

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

The script I posted works fine over here. You're not doing anything wrong I don't think. Copying the script to Notepad and saving it with a .jsx extension is the right way. If there had been an error with the script, the script would have crashed, not InDesign.

 

Maybe try the script on a new document. Add a few pages and place place a few text frames and/or rectangeles on each page, then run the script. If that works, there's probably something wrong in your original document.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Peter’s script works for me. What happens if you try it on a new document with a few rectangles? Maybe there’s something about the existing document that is causing the crash. (missed Peter’s last post)

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

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 )

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

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

 

So, eerbes, there's your answer.

 

P.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines