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

Align image to page script

Engaged ,
Feb 23, 2024 Feb 23, 2024

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.

TOPICS
Scripting

Views

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

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) {}  

Votes

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

Copy link to clipboard

Copied

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

Mike Witherell

Votes

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

Copy link to clipboard

Copied

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

Votes

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

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]

Votes

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

Copy link to clipboard

Copied

LATEST

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

 

Votes

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