Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
7

Align image to page script

Engaged ,
Feb 23, 2024 Feb 23, 2024

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.

TOPICS
Scripting
310
Translate
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 Expert ,
Feb 23, 2024 Feb 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) {}  
Translate
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 Expert ,
Feb 23, 2024 Feb 23, 2024

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

Mike Witherell
Translate
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
Engaged ,
Feb 23, 2024 Feb 23, 2024

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

Translate
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 Expert ,
Feb 23, 2024 Feb 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]

Translate
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
Engaged ,
Feb 23, 2024 Feb 23, 2024
LATEST

I've not re-booted but had to restart Indesign, (obviously) and reused my original script - which worked fine - no crash. Weird.

 

Translate
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