17 Replies Latest reply on Mar 16, 2016 7:17 AM by Colin Flashman

# Concentrate groupped pageItems by script

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

• ###### 1. Re: Concentrate groupped pageItems by script

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][T] > b[1][T]);

r = r.visibleBounds;

// Right magnet
dx = r[R]-b[i][L];
b[i][T]=r[T];
b[i][B]=r[B];
b[i][L]+=dx;
b[i][R]+=dx;

// Bottom magnet
i = 1 - i;
dy = r[B]-b[i][T];
b[i][L]=r[L];
b[i][R]=r[R];
b[i][T]+=dy;
b[i][B]+=dy;

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

{
var t = tasks.itemByName(name);
if( t.isValid )
{
t.eventListeners.everyItem().remove();
t.remove();
}

```

Here is what it does:

Test Group Magnet - YouTube

@+

Marc

• ###### 2. Re: Concentrate groupped pageItems by script

Marc!

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

Thanks for that!

Jarek

• ###### 3. Re: Concentrate groupped pageItems by script

Marc, vraiment cool !

• ###### 4. Re: Concentrate groupped pageItems by script

Ce n'est pas possible!

Wow. I want to see this when it is complete! Let me know, OK?

• ###### 5. Re: Concentrate groupped pageItems by script

Brilliant. Back to scripting kindergarten for me.

• ###### 6. Re: Concentrate groupped pageItems by script

Nice POC!

FWIW, I would record the old bounds and bail out immediately if it did not change, to prevent resetting the bounds on every idle event.

• ###### 7. Re: Concentrate groupped pageItems by script

Merci Marc pour ce code. Mais comment l'introduit-on dans Indesign ? Via quelle application ?

La démo est fantastique !

• ###### 8. Re: Concentrate groupped pageItems by script

Hi Marc
This is great! So how do you do this?
I placed a file with your code in the script folder... that's no working...

Also I had no idea you can script something that works "all the time". I thought only real extensions can do this. My scripting is always a batch that does something to a file and then stops.

Regards

Romano

• ###### 9. Re: Concentrate groupped pageItems by script

Thanks all for your feedback.

Graphikorps

Le script s'installe comme à l'ordinaire :

1. Créez un fichier jsx (par exemple : magnetGroup.jsx) et copiez-collez le code donné ci-dessus.

2. Installez le script, cf. Indiscripts :: Indiscripts for Dummies

3. Exécutez le jsx depuis la palette Scripts.

N. B. — Ce script est “session-persistent”, c.-à-d. qu'il reste actif jusqu'à la fermeture d'InDesign. Il inspecte la sélection active et réagit seulement lorsqu'un bloc rectangulaire est sélectionné au sein d'un groupe contenant par ailleurs exactement deux blocs-texte. Il faut donc disposer d'un groupe de 3 objets (un rectangle et deux blocs-textes). Dans les autres cas il ne se produit rien. Ces conditions pourraient bien sûr être ajustées, mais c'était la demande initiale de l'auteur de ce fil de discussion.

romanobstuder

Just create a file, e.g. magnetGroup.jsx, having the code I posted above and install the script as usual—cf. http://www.indiscripts.com/pages/help#hd0sb2

Then run the script from the Scripts panel. It won't do anything as long as you won't select a rectangle within a group that also contains two text frames (as requested by the author of this thread.)

These conditions could be easily adjusted for other purposes though. Note also that the script could be installed as a startup script as well ;-)

@+

Marc

• ###### 10. Re: Concentrate groupped pageItems by script

Merci beaucoup Marc!

You are a star.

I think the EventListener is what makes this persistent. Is there a simple explanation on how to implement that style of scripts? I would love to play further with this!

Regards

Romano

• ###### 11. Re: Concentrate groupped pageItems by script

Hi Romano,

1. What makes the script session-persistent is the #targetengine directive.

2. What makes the script responsive is the IdleTask instance (and indeed the underlying event listener.)

romanobstuder wrote:

(...) Is there a simple explanation on how to implement that style of scripts? I would love to play further with this!

We have often discussed InDesign events and listeners in various threads and posts. Here are a few links:

Hope that helps.

@+

Marc

• ###### 12. Re: Concentrate groupped pageItems by script

Great stuff, Marc!

Peter

• ###### 13. Re: Concentrate groupped pageItems by script

Merci beaucoup pour ces explications d'installation ! Je vais le tester avec impatience.

Bien cordialement,

Stéphane [Graphikorps]

• ###### 14. Re: Concentrate groupped pageItems by script

After Christopher Colombus discovering America (or close), Pasteur revolutionizing medecin, now comes  Marc Autret exploring new lands of InDesign Scripting.

In one word…Amazing!

Loic

• ###### 15. Re: Concentrate groupped pageItems by script

"A small script for the man, a great script for humanity"

Stephane

• ###### 16. Re: Concentrate groupped pageItems by script

Just wanted to add a big "Merci" for the knowledge that Marc is sharing. Marc you are truly generous!

To all the others in this forum I highly recommend to follow the links by Marc. Lots of really great stuff!

Regards

Romano

• ###### 17. Re: Concentrate groupped pageItems by script

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!