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

Script to Match Object Sizes and Align Captions in InDesign

Guide ,
Oct 25, 2024 Oct 25, 2024

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.

6868.jpg

 

 

<Tiitle renamed by MOD>

TOPICS
Bug , Feature request , How to , Scripting
2.1K
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 3 Correct answers

Guide , Oct 25, 2024 Oct 25, 2024
var myGraphicFrame = app.selection[0];
var myTextFrame = app.selection[1];
myTextFrame.geometricBounds = [ myGraphicFrame.geometricBounds[2], myGraphicFrame.geometricBounds[1], myTextFrame.geometricBounds[2], myGraphicFrame.geometricBounds[3] ];

 

(^/)

Translate
LEGEND , Oct 25, 2024 Oct 25, 2024

 

myTextFrame.geometricBounds = [ myGraphicFrame.geometricBounds[2], myGraphicFrame.geometricBounds[1], myGraphicFrame.geometricBounds[2]+(myTextFrame.geometricBounds[2] -myTextFrame.geometricBounds[0]), myGraphicFrame.geometricBounds[3] ];

 

Translate
Guide , Nov 02, 2024 Nov 02, 2024

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]
...
Translate
LEGEND ,
Oct 25, 2024 Oct 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 ...

 

RobertatIDTasker_3-1729853305558.png

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

 

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 ,
Oct 25, 2024 Oct 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

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
LEGEND ,
Oct 25, 2024 Oct 25, 2024

@FRIdNGE

 

What with fitting TextFrame to its contents?

 

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 ,
Oct 25, 2024 Oct 25, 2024

Better: if you reduce the width and the height of the Graphic Frame:

 

var myGraphicFrame = app.selection[0];
var myTextFrame = app.selection[1];
myTextFrame.geometricBounds = [ myGraphicFrame.geometricBounds[2], myTextFrame.geometricBounds[1], myTextFrame.geometricBounds[2], myGraphicFrame.geometricBounds[3] ];

 

(^/)

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
LEGEND ,
Oct 25, 2024 Oct 25, 2024

What do you mean by "better"? 

 

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 ,
Oct 25, 2024 Oct 25, 2024

Hello, great @FRIdNGE 
Thank you very much.

it's just that sometimes the caption is not together with the image.
The title note may have been dragged away from the image by some distance, and after running the script, it should still automatically align with the image.

 

112.jpg

Also, it's odd that if the caption is misaligned with the figure, it will only be right-aligned.

7788.jpg

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 ,
Oct 25, 2024 Oct 25, 2024
var myGraphicFrame = app.selection[0];
var myTextFrame = app.selection[1];
myTextFrame.geometricBounds = [ myGraphicFrame.geometricBounds[2], myGraphicFrame.geometricBounds[1], myTextFrame.geometricBounds[2], myGraphicFrame.geometricBounds[3] ];

 

(^/)

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
LEGEND ,
Oct 25, 2024 Oct 25, 2024

 

myTextFrame.geometricBounds = [ myGraphicFrame.geometricBounds[2], myGraphicFrame.geometricBounds[1], myGraphicFrame.geometricBounds[2]+(myTextFrame.geometricBounds[2] -myTextFrame.geometricBounds[0]), myGraphicFrame.geometricBounds[3] ];

 

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 ,
Oct 25, 2024 Oct 25, 2024

Robert,

 

Sure! To catch more situations, your "myTextFrame.geometricBounds[2]" is … Better!  ðŸ˜‰

 

(^/)

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 ,
Nov 01, 2024 Nov 01, 2024

Hi~

@FRIdNGE 

@Robert at ID-Tasker 

Drag and select seems to have everything OK.
However, when clicking to select, sometimes an error occurs.
There are different cases: the diagram is selected first, or the text box is selected first.
If the diagram is outside the layout, it's a different situation.

 

If there's a lot of text around, it's easier to select it with a single click in sequence.

 

Is it possible to set it to always align to the last selected object?

Thank you~

686.jpg

 

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
LEGEND ,
Nov 02, 2024 Nov 02, 2024

@dublove

 

Script expects specific selection order - I'm pretty sure that @FRIdNGE can make it more universal.

 

It's also possible to make script work in sequence, with only one item selected at a time - first run would check if it's image, store ID in a document's unique label, then, when it's run again, check if TextFrame is selected, retrieve ID of the image and align both.

 

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 ,
Nov 02, 2024 Nov 02, 2024

Try This:

 

/*
    _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();
    }
    
    if ( app.selection.length === 2 ) {

        myDoc.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;

        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();
        }

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

    }
    
}

 

(^/) 

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
LEGEND ,
Nov 02, 2024 Nov 02, 2024

@FRIdNGE

 

As I'm not JS guy - why are you checking if two items are selected - twice?

 

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 ,
Nov 02, 2024 Nov 02, 2024

If you select 3 Frames, it's bad! You need to just select 2 items!

If you select 2 text frames or 2 rectangles (images), it's bad too.

 

You need to precisely select a text frame (caption) and a rectangle (image).

 

I love "security", as noted Edward Lewis in "Pretty Woman" movie!  =D

 

(^/)

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
LEGEND ,
Nov 02, 2024 Nov 02, 2024

@FRIdNGE

 

I know that you need to check for TWO items - you are checking type later - but what is the difference between "x!=2" and "x===2" - in this case?

 

Both have the same "result" - you are checking if "exactly two items are selected". 

 

So why, in the 1st if - you can't just add "else" and contents of the following "if"? 

 

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 ,
Nov 02, 2024 Nov 02, 2024

I could! …

 

I've corrected this glitch:

 

        } else {
            myDoc.viewPreferences.rulerOrigin = myRulerOrigin;
            alert( "Bad Selection!")
            exit();
        }

 

(^/) 

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
LEGEND ,
Nov 02, 2024 Nov 02, 2024

@FRIdNGE

 

You misunderstood. 

 

In your 1st "if" you're checking if selection count is "not equal to two elements" - and you exit execution.

 

In the next "if" - you are checking if selection is "exactly two elements" - and you continue execution. 

 

Aren't both comparisons "the same" in the result you are looking for? 

 

 

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

        myDoc.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;

 

 

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 ,
Nov 02, 2024 Nov 02, 2024

I totally understood you. My correction is about something else!

 

(^/)

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
LEGEND ,
Nov 02, 2024 Nov 02, 2024
quote

I totally understood you. My correction is about something else!


By @FRIdNGE

 

Not sure why you needed it - as you don't change RulerOrigin before the 1st "if"?

 

You read the value before the 1st "if" but you change it in the 2nd "if". 

 

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 ,
Nov 02, 2024 Nov 02, 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;

    }

}

 

(^/)

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
LEGEND ,
Nov 02, 2024 Nov 02, 2024

@FRIdNGE

 

I'm on my phone so not sure what's the difference to your previous version - but it still doesn't answer my question - why do you check the same condition twice? 

 

Unless it's just your way of coding? 

 

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 ,
Nov 02, 2024 Nov 02, 2024

No response after double clicking on 11.02 scripts.

Nothing happens.

 

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 ,
Nov 02, 2024 Nov 02, 2024

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;

    }

}

 

(^/)

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 ,
Nov 02, 2024 Nov 02, 2024


Would it be better :

A case:  dragg-select,  the text box is aligned to the image.

B case: click-select, align to the last selector.

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