Align image to page script
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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) {}
Copy link to clipboard
Copied
Did you try the built-in sample script called aligntopage.jsx or examine its code?
Copy link to clipboard
Copied
That'd just tell me I need to pass an array to align instead of the selection.
Copy link to clipboard
Copied
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]
Copy link to clipboard
Copied
I've not re-booted but had to restart Indesign, (obviously) and reused my original script - which worked fine - no crash. Weird.

