Skip to main content
dublove
Brainiac
October 25, 2024
Answered

Script to Match Object Sizes and Align Captions in InDesign

  • October 25, 2024
  • 1 reply
  • 2917 views

Can there be a script that makes object A the same size as object B?


Here are two scenarios:
The first, being a graphical object, lets A be equal in size to the target B.
The second, if A is a picture and B is its caption, let B and A have two widths.


The second is the most desirable, because very often the caption needs to be re-widened after the picture size is changed. It's a pain in the ass when you have a lot of them.
If you choose an image and a static caption, you can have the caption quickly aligned to the image, including position and width. It would save a lot of work.

 

 

<Tiitle renamed by MOD>

Correct answer FRIdNGE

No response after double clicking on 11.02 scripts.

Nothing happens.

 


Take this one:

 

/*
    _FRIdNGE-0766_ImageCaptionAlignment.jsx
    Script written by FRIdNGE, Michel Allio [02/11/2024]
*/

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;

    }

}

 

(^/)

1 reply

Robert at ID-Tasker
Brainiac
October 25, 2024

No need for a script - you can simply group two objects and resize the group.  

 

Or maybe not ... when you resize group - all objects will be resized proprtionally ...

 

But keeping both objects grouped - will help in resizing and fitting.

 

FRIdNGE
Inspiring
October 25, 2024

Totally basically:

 

// Select first the Graphic Frame then the Text Frame
var myGraphicFrame = app.selection[0];
var myTextFrame = app.selection[1];
myTextFrame.geometricBounds = [ myTextFrame.geometricBounds[0], myTextFrame.geometricBounds[1], myTextFrame.geometricBounds[2], myGraphicFrame.geometricBounds[3] ];

 

(^/)  The Jedi

dublove
dubloveAuthor
Brainiac
November 2, 2024

Even if the original code is correct, this other development seems to be more logical:

 

function main() {

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

    if ( app.selection.length != 2 ) {
        alert( "Select 2 items!")
        exit();
    }
    
    if ( app.selection.length === 2 ) {

        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;

    }

}

 

(^/)


No response after double clicking on 11.02 scripts.

Nothing happens.