Skip to main content
Inspiring
August 25, 2021
Question

How to move an entire group with scripting to specific location

  • August 25, 2021
  • 2 replies
  • 836 views

I get an entire group and create it like so:

 

 

var mainTemplateLayer = aDoc.layers.getByName("Placement" + plcIx);
aDoc.activeLayer = mainTemplateLayer;
mainTemplateLayer.hasSelectedArtwork = true;
app.executeMenuCommand("group");

 

 

With the above my artwork is selected and grouped.

How can I place this artwork (all of it) to a specific position?  

app.selection.length is equal to 17 so if I just did

app.selection[0].left = 50;

app.selection[0].top = 50;

then it would just move the selection that is 0 array right?

 

I had asked a similar question on another thread and marked it as correct to do this:

var sel = app.selection[0];

but it appears that this only gets part of the group.  Am I missing something?

This topic has been closed for replies.

2 replies

femkeblanco
Legend
August 26, 2021

It depends on the the sequence of steps that you are following. If you selected the items you want to group, grouped them with executeMenuCommand, selection.length should be 1 (a selected group is 1 item), implying that there are more steps between the above code and selection.length becoming 17.

 

Grouping with executeMenuCommand is done with one line but gives you no control. You could group the standard way, assigning your group to a variable, therefore being able to control its position:

 

// select the items you want to group
var group1 = app.activeDocument.groupItems.add();
for (var i = 0; i < app.selection.length; i++) {
    app.selection[i].moveToEnd(group1);
}
// you can then select other items or no items and still control your group
app.selection = null;
// ...
group1.left = 50;
group1.top = 50;

 

ajabon grinsmith
Community Expert
Community Expert
August 26, 2021

I can't say much categorically yet because I can only see fragments of code.
It seems that app.selection [0] should be reacquired after creating the group.

ajabon grinsmith
Community Expert
Community Expert
August 26, 2021

It works.

 

var aDoc  = app.activeDocument;
var plcIx = " 1";

var mainTemplateLayer = aDoc.layers.getByName("Placement" + plcIx);
try{
    aDoc.selection = null;
    
    aDoc.activeLayer = mainTemplateLayer;
    mainTemplateLayer.hasSelectedArtwork = true;
    app.executeMenuCommand("group");

    var group = aDoc.selection[0];
    aDoc.selection = null;
    group.top = 0;
    group.left = 10;
    
    } catch(e){}