通过javascript将插图与Illustrator中的Artboard对齐

通过javascript将插图与Illustrator中的Artboard对齐,javascript,adobe-illustrator,Javascript,Adobe Illustrator,我试图通过javascript将illustrator中的一个对象定位到艺术板的左下方。我可以使用javascript访问对象,并对其执行类似“旋转”的功能 但是我很难找到一种简单的方法来定位/对齐艺术板左下角的“pageItems[0]”。除了计算它的当前距离然后移动它之外,还有更简单的方法吗?谢谢。也许有比这更简单的方法,但这很有效 if ( app.documents.length > 0) { doc = app.activeDocument; // Get t

我试图通过javascript将illustrator中的一个对象定位到艺术板的左下方。我可以使用javascript访问对象,并对其执行类似“旋转”的功能


但是我很难找到一种简单的方法来定位/对齐艺术板左下角的“pageItems[0]”。除了计算它的当前距离然后移动它之外,还有更简单的方法吗?谢谢。

也许有比这更简单的方法,但这很有效

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 compensate 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];
}

基本上,我们必须将pageItem的左上角设置为artboard的左下角。不幸的是,这将使我们的pageItem位于艺术板下方,因此我们根据pageItem的高度调整偏移量:

同样,如果您希望将项目置于艺术板的中心,则需要稍微不同地处理高度和宽度

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 compensate 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];
}

这是一种很好的居中方式。这甚至考虑了artboard是否未设置为0,0
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 compensate 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];
}