Skip to main content
Known Participant
February 19, 2016
Question

Concentrate groupped pageItems by script

  • February 19, 2016
  • 2 replies
  • 2100 views

Hi,
Every day I’m working with grouped elements which consist of 1 Rectangle and several TextFrames – usually 3.
I need to change dimension of Rectangle every time but not to ungroup the Objects. So I’m clicking twice on Rectangle, move it and resize. In this case all the TextFrames in group remain sometimes far … far from the Rectangle but there is a need to bring them together again.

I’m wondering if there is a possibility to write a java script which can concentrate the elements of group by moving TextFrames to the bounds (or origin) of Rectangle.

I will be very grateful for any idea how to do it or better a pace of Code which will do this annoying and tiring work 🙂

Best regards
Marcin

This topic has been closed for replies.

2 replies

Colin Flashman
Community Expert
Community Expert
March 16, 2016

That is AWESOME!

Just for the record, I tried it with images in a Data Merge. Unfortunately it doesn't work in that instance (if "fit frames to image" is selected, the script won't move the text accordingly). I think it has something to do with the image changing size without the focus being put onto the image.

Still a hell of a technique though!

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Marc Autret
Legend
February 22, 2016

Hi Marcin,

Here is a draft code you may work from.

#targetengine magnetGroupRectangle

function follow() 

{   

    const T=0, L=1, B=2, R=3;

    if( !app.properties.activeDocument ) return;

    if( !app.properties.selection ) return;

    if( 1 != app.selection.length ) return;

   

    var a, i, every, b, i, dx, dy

        r = app.selection[0],

        g = r.parent;

    if( !( r instanceof Rectangle ) ) return;

    if( !( g instanceof Group ) ) return;

    if( 3 != g.pageItems.count() ) return;

    a = (every=g.textFrames.everyItem()).getElements();

    if( 2 != a.length ) return;

    b = every.visibleBounds;

    i = +(b[0] > b[1]);

   

    r = r.visibleBounds;

    // Right magnet

    dx = r-b;

    b=r;

    b=r;

    b+=dx;

    b+=dx;

    // Bottom magnet

    i = 1 - i;

    dy = r-b;

    b=r;

    b=r;

    b+=dy;

    b+=dy;

   

    a[0].visibleBounds = b[0];

    a[1].visibleBounds = b[1];

(function(tasks,name,rate,callback) 

    var t = tasks.itemByName(name); 

    if( t.isValid )

    {

        t.eventListeners.everyItem().remove();

        t.remove();

    } 

    tasks.add({name:name, sleep:rate})

       .addEventListener(IdleEvent.ON_IDLE, callback, false); 

})(app.idleTasks,'magnetGroupRectangle',15,follow);

Here is what it does:

Test Group Magnet - YouTube

@+

Marc

Jump_Over
Legend
February 22, 2016

Marc!

I love those windows could be opened using your "draft codes"

Thanks for that!

Jarek