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

How to modify this script to select multiple images and a text box?

Guide ,
Apr 19, 2025 Apr 19, 2025

Thanks to FRIdNGE, the great original script provider! 

There's also Robert at ID-Tasker who has been quite helpful.
It is now suitable for the case of one caption for one picture, allowing the caption to be quickly pasted close to the picture.

The original post seems to have sunk.
I'm trying to modify it to a situation where multiple images share a caption, but I don't know how to traverse out the textframe and image with a loop.

066.png

/*
    _FRIdNGE-0766_ImageCaptionAlignment.jsx
    Script written by FRIdNGE, Michel Allio [02/11/2024]
*/
//https://community.adobe.com/t5/indesign-discussions/script-to-match-object-sizes-and-align-captions-in-indesign/m-p/14941759
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, 'Image And Caption Alignment');

function main() {

    var myDoc = app.activeDocument;
    var myRulerOrigin = myDoc.viewPreferences.rulerOrigin;

    if ( app.selection.length != 2 ) {

        alert( "Select 2 items!")
        exit();
        
    } else {

        if ( app.selection[0] instanceof Rectangle && app.selection[1] instanceof TextFrame ) {
            var myGraphicFrame = app.selection[0];
            var myTextFrame = app.selection[1];        
        } else if ( app.selection[1] instanceof Rectangle && app.selection[0] instanceof TextFrame ) {
            var myGraphicFrame = app.selection[1];
            var myTextFrame = app.selection[0];        
        } else {
            alert( "Bad Selection!")
            exit();
        }

        myDoc.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;

        myTextFrame.geometricBounds = [ myGraphicFrame.geometricBounds[2], myGraphicFrame.geometricBounds[1], myGraphicFrame.geometricBounds[2] + myTextFrame.geometricBounds[2] - myTextFrame.geometricBounds[0], myGraphicFrame.geometricBounds[3] ];
        
        myDoc.viewPreferences.rulerOrigin = myRulerOrigin;

    }

}

 

TOPICS
Bug , Feature request , How to , Scripting
2.0K
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

correct answers 1 Correct answer

Community Expert , Apr 22, 2025 Apr 22, 2025

Hi @dublove, here is one approach...

- Mark

 

/**
 * @file Align Caption Frame To Graphic Frames.js
 *
 * Usage:
 *   1. Select the caption, a text frame
 *   2. Also select one or more graphic frames
 *   3. Run script
 *
 * Script will align the left and right edges of the caption frame
 * to the left and right extremes of the graphic frame(s), and
 * also align the caption frame above or below the graphic frame(s).
 *
 * @author m1b
 * @version 2025-04-23
 * @discussion https://community.adob
...
Translate
Guide ,
Apr 22, 2025 Apr 22, 2025

But I tried another newly created document and it has 17mm more

 

For some reason, it's in the example document I passed along.
You regenerate the title note and it's just properly aligned.

Turns out that text box has a ghost ......

 

dublove_0-1745332161980.jpeg

 

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
Guide ,
Apr 22, 2025 Apr 22, 2025

Press Alt+click none to clear the optimization and then align it again and it's normal

 

But after half a day of looking it up, I still haven't figured out what's causing the irregularity.

06.png

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
Guide ,
Apr 22, 2025 Apr 22, 2025

@m1b 

I found the reason:
The script does not support inner margins.
Adding inner margins will result in no alignment.

But I need the inner margin feature. It's really sad.

082.png

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 ,
Apr 22, 2025 Apr 22, 2025

Hi @dublove I found a bug where Indesign wasn't respecting my geometric bounds because I made them too small in some cases. I have fixed this now. Also I added a settings property `adjustForTextFrameInsets` which will adjust the frame to account for the text frame insets. Try it with this true and false to see how it works:

insets-demo.gif

Also use `gap` if you need to.

- Mark

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
Guide ,
Apr 22, 2025 Apr 22, 2025

Left-right alignment is OK, but the textbox ignores the upper "Inner Margin" setting.
The "Inner Margin" setting in the object style should be retained.

Left-right alignment is OK, but the textbox ignores the upper “inner distance” setting.
The "Inside Distance' setting in the object style should be retained.


Also, can you get the GraphicFrame.geometricBounds[2] of a particular image?
Wouldn't that solve the case where the text box appears above the image?

009.png

010.png

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 ,
Apr 22, 2025 Apr 22, 2025

I have added finer control over which text frame insets are used, so I have set it to [false, true, false, true] so that only left and right insets are used.

 

Also I fixed one more bug that caused a problem in some cases when the text frame was above the graphics.

- Mark

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
Guide ,
Apr 22, 2025 Apr 22, 2025

It's OK.
Saves so much work, it's great.
Another big step forward for Adobe.
I thank you for all the people in the world.

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 ,
Apr 22, 2025 Apr 22, 2025
LATEST

You're welcome!

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