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

Script to Match Object Sizes and Align Captions in InDesign

Advocate ,
Oct 25, 2024 Oct 25, 2024

Copy link to clipboard

Copied

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

Views

989

Translate

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

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] ];

 

(^/)

Votes

Translate

Translate
Community Expert , 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] ];

 

Votes

Translate

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]
...

Votes

Translate

Translate
Community Expert ,
Oct 25, 2024 Oct 25, 2024

Copy link to clipboard

Copied

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.

 

Votes

Translate

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

Copy link to clipboard

Copied

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

Votes

Translate

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

Copy link to clipboard

Copied

@FRIdNGE

 

What with fitting TextFrame to its contents?

 

Votes

Translate

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

Copy link to clipboard

Copied

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] ];

 

(^/)

Votes

Translate

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

Copy link to clipboard

Copied

What do you mean by "better"? 

 

Votes

Translate

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

Copy link to clipboard

Copied

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

Votes

Translate

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

Copy link to clipboard

Copied

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

 

(^/)

Votes

Translate

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

Copy link to clipboard

Copied

 

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

 

Votes

Translate

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

Copy link to clipboard

Copied

Robert,

 

Sure! To catch more situations, your "myTextFrame.geometricBounds[2]" is … Better!  😉

 

(^/)

Votes

Translate

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

Copy link to clipboard

Copied

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

 

Votes

Translate

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

Copy link to clipboard

Copied

@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.

 

Votes

Translate

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

Copy link to clipboard

Copied

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;

    }
    
}

 

(^/) 

Votes

Translate

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

Copy link to clipboard

Copied

@FRIdNGE

 

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

 

Votes

Translate

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

Copy link to clipboard

Copied

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

 

(^/)

Votes

Translate

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

Copy link to clipboard

Copied

@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"? 

 

Votes

Translate

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

Copy link to clipboard

Copied

I could! …

 

I've corrected this glitch:

 

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

 

(^/) 

Votes

Translate

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

Copy link to clipboard

Copied

@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;

 

 

Votes

Translate

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

Copy link to clipboard

Copied

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

 

(^/)

Votes

Translate

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

Copy link to clipboard

Copied

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". 

 

Votes

Translate

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

Copy link to clipboard

Copied

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;

    }

}

 

(^/)

Votes

Translate

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

Copy link to clipboard

Copied

@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? 

 

Votes

Translate

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

Copy link to clipboard

Copied

No response after double clicking on 11.02 scripts.

Nothing happens.

 

Votes

Translate

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

Copy link to clipboard

Copied

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;

    }

}

 

(^/)

Votes

Translate

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

Copy link to clipboard

Copied


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.

Votes

Translate

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