2013-12-09 3 views
0

Я пытаюсь позиционировать объект в иллюстраторе в нижней части слева от арт-доски через javascript. Я могу получить доступ к объекту с яваскрипт и выполняет функцию к нему, как «повернуть»Выравнивание работ с Artboard в Illustrator с помощью javascript

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