9 Replies Latest reply on Aug 15, 2013 8:06 AM by NoisiaCoder

# Move a group to the artboard using javascript

• ###### 1. Re: Move a group to the artboard using javascript

- get group's width/height

- get artboard bounds

- calculate artboard center

- change group's position to artboard's center +/- group's 1/2" width/height

• ###### 2. Re: Move a group to the artboard using javascript

^ That is easier to understand than what I was typing (as follows).

You're gonna need to do some basic math calculations along the lines of document width and height,

selection width and height, selection top and left, then figure out what needs to be negative and positive,

added and subtracted, as well as dived by 2.

• ###### 3. Re: Move a group to the artboard using javascript

Yes. I understand.  I am fairly new to javascript and the illustrator object model....what is the code to move a group?

• ###### 4. Re: Move a group to the artboard using javascript

you could use the move() method, but it is simpler to use

yourGroup.position = [x,y];

edit:

an object's position is its left/top coordinates

• ###### 5. Re: Move a group to the artboard using javascript

CarlosCanto wrote:

you could use the move() method, but it is simpler to use

yourGroup.position = [x,y];

Ok this worked. Thanks alot.

 width = myGroup.width; height = myGroup.height; ableft = abBounds[0]; abtop = abBounds[1]; abwidth = abBounds[2] - ableft; abheight = abtop- abBounds[3];

 abctrx = abwidth/2+ableft; abctry = abtop-abheight/2; myGroup.position = [abctrx - (width/2), abctry + (height/2)];
• ###### 6. Re: Move a group to the artboard using javascript

great, you're welcome

• ###### 7. Re: Move a group to the artboard using javascript

Maybe I am wrong, but for anyone else interested in this thread/code, it looks like in the above example "abBounds" would be undefined.

You would need to add something like the following line to declare "abBounds":

```abBounds = doc.artboards[0].artboardRect;
```

// ----------------------------------------------------------------------------------------- ---

Also, here is a related condensed version of that:

```var doc = app.activeDocument;
var sel = doc.selection[0];
var abBounds = doc.artboards[0].artboardRect;
sel.position = [ ((abBounds[2]/2)-(sel.width / 2)), ((abBounds[3]/2)+(sel.height / 2)) ];
```

You could also probably do this:

```var doc = app.activeDocument;
var sel = doc.selection[0];
sel.top = (-(app.activeDocument.height / 2) + (sel.height / 2));
sel.left = ((app.activeDocument.width / 2) - (sel.width / 2));
```

Each works with my testing in CS5.5.