Skip to main content
Inspiring
February 23, 2024
Question

Align image to page script

  • February 23, 2024
  • 2 replies
  • 415 views

I was hoping to write a tiny script to align the current selected image to the page:

 

// DOES NOT WORK!!! USE AT YOUR OWN RISK
var srcDoc = app.activeDocument;
var alignTo = AlignDistributeBounds.PAGE_BOUNDS;
var sel = srcDoc.selection;
srcDoc.align ( sel, AlignOptions.VERTICAL_CENTERS,   alignTo);
srcDoc.align ( sel, AlignOptions.HORIZONTAL_CENTERS, alignTo); 

But InDesigned crashed - but I can't think where I'm going wrong.

DON'T use the script in it's current form

 

Answers on a postcard, please.

This topic has been closed for replies.

2 replies

Mike Witherell
Community Expert
Community Expert
February 23, 2024

Did you try the built-in sample script called aligntopage.jsx or examine its code?

Mike Witherell
Inspiring
February 23, 2024

That'd just tell me I need to pass an array to align instead of the selection.

rob day
Community Expert
Community Expert
February 23, 2024

The aligntopage.jsx is 15 years old—the align() method probably wasn’t in the API.

 

srcDoc.selection returns an array and srcDoc.align() wants an array for the first parameter, but it may be the contents of the selection array is causing the crash? If you are trying to align a single item you could try srcDoc.selection[0]

rob day
Community Expert
Community Expert
February 23, 2024

Hi @Ghoul Fool , your script runs without a problem for me. Have you tried restarting InDesign?

 

Maybe wrap the align code in a try statement—in case the selection (some text?) can‘t be aligned

 

var srcDoc = app.activeDocument;
var alignTo = AlignDistributeBounds.PAGE_BOUNDS;
var sel = srcDoc.selection;
try {
    srcDoc.align ( sel, AlignOptions.VERTICAL_CENTERS,   alignTo);
    srcDoc.align ( sel, AlignOptions.HORIZONTAL_CENTERS, alignTo); 
}catch(e) {}