Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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:
@+
Marc
Copy link to clipboard
Copied
Marc!
I love those windows could be opened using your "draft codes"
Thanks for that!
Jarek
Copy link to clipboard
Copied
Marc, vraiment cool !
Copy link to clipboard
Copied
Ce n'est pas possible!
Wow. I want to see this when it is complete! Let me know, OK?
Copy link to clipboard
Copied
Brilliant. Back to scripting kindergarten for me.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Merci Marc pour ce code. Mais comment l'introduit-on dans Indesign ? Via quelle application ?
La démo est fantastique !
Copy link to clipboard
Copied
Thanks all for your feedback.
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.
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Merci beaucoup pour ces explications d'installation ! Je vais le tester avec impatience.
Bien cordialement,
Stéphane [Graphikorps]
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Good answer !!!
"A small script for the man, a great script for humanity"
Stephane
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
I would love to learn more about this!
Regards
Romano
Copy link to clipboard
Copied
Great stuff, Marc!
Peter
Copy link to clipboard
Copied
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!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now