I'm attempting to position an object in illustrator to the bottom, left of the art board via javascript. I can access the object with javascript and perform function to it like "rotate"
if ( app.documents.length > 0) {
doc = app.activeDocument;
}
function traceImage(doc) {
doc.pageItems[0].rotate (30);
}
traceImage(doc);
But I'm having trouble finding a simple way to position/align the "pageItems[0]" in the bottom/left of the art board. Is there a simpler approach to this other than calculating it's current distance and then moving it? Thanks.
I'm attempting to position an object in illustrator to the bottom, left of the art board via javascript. I can access the object with javascript and perform function to it like "rotate"
if ( app.documents.length > 0) {
doc = app.activeDocument;
}
function traceImage(doc) {
doc.pageItems[0].rotate (30);
}
traceImage(doc);
But I'm having trouble finding a simple way to position/align the "pageItems[0]" in the bottom/left of the art board. Is there a simpler approach to this other than calculating it's current distance and then moving it? Thanks.
javascript
adobe-illustrator
Share
Improve this question
asked Dec 9, 2013 at 19:26
user3084184user30841841111 silver badge22 bronze badges
Add a ment
|
2 Answers
2
Reset to default
6
Similarly if you're looking to center the item to the artboard, you'd need to handle the height and width a little differently.
if ( app.documents.length > 0) {
doc = app.activeDocument;
// Get the active Artboard index
var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];
// Get the right most X point of the Artboard
// artboardX + artboardWidth
var artboardRight = activeAB.artboardRect[0] + activeAB.artboardRect[2];
// Get the bottom most Y point of the Artboard
// artboardY + artboardHeight
var artboardBottom = activeAB.artboardRect[1] + activeAB.artboardRect[3];
// The page item you want to move. Reference it how you will. This just
// obviously grabs the first pageItem in the document.
var myPageItem = doc.pageItems[0];
// Here is where the magic happens. Set the position of the item.
// [0,0] would be at the top left, so we have to pensate for the artboard
// height and width. We add item's height then split it for the vertical
// offset, or we'd end up BELOW the artboard.
// Then we subtract the item's width and split the difference to center the
// item horizontally.
var horziontalCenterPosition = (artboardRight - myPageItem.width)/2;
var verticalCenterPosition = (artboardBottom + myPageItem.height)/2;
myPageItem.position = [horziontalCenterPosition, verticalCenterPosition];
}
There probably an easier way than this, but this works.
if ( app.documents.length > 0) {
doc = app.activeDocument;
// Get the active Artboard index
var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];
// Get the Height of the Artboard
var artboardBottom = activeAB.artboardRect[3];
// The page item you want to move. Reference it how you will. This just
// obviously grabs the first pageItem in the document.
var myPageItem = doc.pageItems[0];
// Here is where the magic happens. Set the poition of the item.
// [0,0] would be at the top left, so we have to pensate for the artboard
// height. We add myPageItem's height for offset, or we'd end up BELOW
// the artboard.
myPageItem.position = [0, artboardBottom + myPageItem.height];
}
Essentially, we have to set the top left corner of our pageItem to the bottom left of the artboard. Unfortunately, that would put our pageItem below the Artboard, so we adjust the offset by the height of our pageItem: