2013-12-09 33 views
0

我试图在Illustrator中将对象放置到底部,通过javascript将艺术板左侧。我可以用JavaScript访问对象和像执行函数将其“旋转”通过javascript在Illustrator中将图稿对齐到Illustrator中

if (app.documents.length > 0) { 
    doc = app.activeDocument; 
} 


    function traceImage(doc) { 

     doc.pageItems[0].rotate (30); 


} 

traceImage(doc); 

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

回答

2

有可能比这更简单的方法,但这个工程。

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的左上角设置到画板的左下角。不幸的是,这将使我们的pageItem 下面的画板,所以我们调整我们的pageItem的高度偏移:

0

同样,如果你正在寻找的项目中心到画板,你需要处理高度和宽度稍有不同。

if (app.documents.length > 0) { 
    doc = app.activeDocument; 

    // Get the active Artboard index 
    var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()]; 

    // Get the Width of the Artboard 
    var artboardRight = activeAB.artboardRect[2]; 
    // 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 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]; 
}