InDesign外掛--常規功能開發--對齊到頁面--js指令碼開發--ID外掛

安敬知發表於2020-11-05

  Adobe InDesign是Adobe公司的一個桌面出版 (DTP) 的應用程式,主要用於各種印刷品的排版編輯。InDesign可以將文件直接匯出為Adobe的PDF格式,而且有多語言支援。採用指令碼語言自動化編碼,在學習中的一個重要功能是對齊到頁面功能,以下原始碼僅用於學習交流,請勿用於商業用途和其它非法用途。原始碼如下所示:


//AlignToPage.jsx
//An InDesign CS2 JavaScript
//
//Aligns the items in the selection to the specified location on the page.
//
//
//
var myObjectList = new Array;
if (app.documents.length != 0) {

    if (app.selection.length != 0) {
        for (var myCounter = 0; myCounter < app.selection.length; myCounter++) {
            switch (app.selection[myCounter].constructor.name) {
            case "Rectangle":
            case "Oval":
            case "Polygon":
            case "TextFrame":
            case "Group":
            case "Button":
            case "GraphicLine":
                myObjectList.push(app.selection[myCounter]);
                break;
            }
        }
        if (myObjectList.length != 0) {
            myDisplayDialog(myObjectList);
        } else {
            alert("請選擇一個頁面再試——執著Indesign.");
        }
    } else {
        alert("請選擇一個物件再試.");
    }
} else {
    alert("請開啟一個文件,選擇一個物件再試.");
}
function myDisplayDialog(myObjectList) {
    var myDialog = app.dialogs.add({
        name: "對齊到頁面"
    });
    with (myDialog.dialogColumns.add()) {
        with (dialogRows.add()) {
            with (dialogColumns.add()) {
                with (borderPanels.add()) {
                    staticTexts.add({
                        staticLabel: "垂直"
                    });
                    var myVerticalAlignmentButtons = radiobuttonGroups.add();
                    with (myVerticalAlignmentButtons) {
                        radiobuttonControls.add({
                            staticLabel: "頂",
                            checkedState: true
                        });
                        radiobuttonControls.add({
                            staticLabel: "中心"
                        });
                        radiobuttonControls.add({
                            staticLabel: "底部"
                        });
                        radiobuttonControls.add({
                            staticLabel: "無"
                        });
                    }
                }
            }
            with (dialogColumns.add()) {
                with (borderPanels.add()) {
                    staticTexts.add({
                        staticLabel: "水平"
                    });
                    var myHorizontalAlignmentButtons = radiobuttonGroups.add();
                    with (myHorizontalAlignmentButtons) {
                        radiobuttonControls.add({
                            staticLabel: "靠左",
                            checkedState: true
                        });
                        radiobuttonControls.add({
                            staticLabel: "中心"
                        });
                        radiobuttonControls.add({
                            staticLabel: "靠右"
                        });
                        radiobuttonControls.add({
                            staticLabel: "無"
                        });
                    }
                }
            }
        }
        with (dialogRows.add()) {
            var myConsiderMarginsCheckbox = checkboxControls.add({
                staticLabel: "考慮頁邊空白",
                checkedState: false
            });
        }
    }
    var myResult = myDialog.show();
    if (myResult == true) {
        myVerticalAlignment = myVerticalAlignmentButtons.selectedButton;
        myHorizontalAlignment = myHorizontalAlignmentButtons.selectedButton;
        myConsiderMargins = myConsiderMarginsCheckbox.checkedState;
        myDialog.destroy();
        myAlignObjects(myObjectList, myVerticalAlignment, myHorizontalAlignment, myConsiderMargins);
    } else {
        myDialog.destroy();
    }
}
function myAlignObjects(myObjectList, myVerticalAlignment, myHorizontalAlignment, myConsiderMargins) {
    var myPageHeight = app.activeDocument.documentPreferences.pageHeight;
    var myPageWidth = app.activeDocument.documentPreferences.pageWidth;
    var myOldRulerOrigin = app.activeDocument.viewPreferences.rulerOrigin;
    app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
    app.activeDocument.zeroPoint = [0, 0];
    var myXCenter = myPageWidth / 2;
    var myYCenter = myPageHeight / 2;
    var myMarginPreferences = app.activeWindow.activePage.marginPreferences;
    var myTopMargin = myMarginPreferences.top;
    var myLeftMargin = myMarginPreferences.left;
    var myBottomMargin = myMarginPreferences.bottom;
    var myRightMargin = myMarginPreferences.right;
    if (myConsiderMargins == true) {
        switch (myHorizontalAlignment) {
        case 0:
            myX = myLeftMargin;
            break;
        case 1:
            myX = myXCenter;
            break;
        case 2:
            myX = myPageWidth - myRightMargin;
            break;
        case 3:
            myX = null;
            break;
        }
        switch (myVerticalAlignment) {
        case 0:
            myY = myTopMargin;
            break;
        case 1:
            myY = myYCenter;
            break;
        case 2:
            myY = myPageHeight - myBottomMargin;
            break;
        case 3:
            myY = null;
            break;
        }
    } else {
        switch (myHorizontalAlignment) {
        case 0:
            myX = 0;
            break;
        case 1:
            myX = myXCenter;
            break;
        case 2:
            myX = myPageWidth;
            break;
        case 3:
            myX = null;
            break;
        }
        switch (myVerticalAlignment) {
        case 0:
            myY = 0;
            break;
        case 1:
            myY = myYCenter;
            break;
        case 2:
            myY = myPageHeight;
            break;
        case 3:
            myY = null;
            break;
        }
    }
    for (myCounter = 0; myCounter < myObjectList.length; myCounter++) {
        myAlignObject(myObjectList[myCounter], myX, myY, myHorizontalAlignment, myVerticalAlignment);
    }
    app.activeDocument.viewPreferences.rulerOrigin = myOldRulerOrigin;
}
function myAlignObject(myObject, myX, myY, myHorizontalAlignment, myVerticalAlignment) {
    var myBounds = myObject.geometricBounds;
    var myWidth = myBounds[3] - myBounds[1];
    var myHeight = myBounds[2] - myBounds[0];
    if (myX == null) {
        myX = myBounds[1];
    }
    if (myY == null) {
        myY = myBounds[0];
    }
    switch (myHorizontalAlignment) {
    case 0:
        break;
    case 1:
        myX = myX - (myWidth / 2);
        break;
    case 2:
        myX = myX - myWidth;
        break;
    case 3:
        break;
    }
    switch (myVerticalAlignment) {
    case 0:
        break;
    case 1:
        myY = myY - (myHeight / 2);
        break;
    case 2:
        myY = myY - myHeight;
        break;
    case 3:
        break;
    }
    myObject.move([myX, myY]);
}

  合理的指令碼程式碼可以有效的提高工作效率,減少重複勞動。


  歡迎光臨知了軟體開發網路平臺,本公司定製開發各類軟體,主要方向為桌面專業軟體開發和外掛定製開發,桌面軟體主要包括文字圖形識別類軟體,資訊管理類軟體,3D列印類軟體,視訊類軟體以及其它涉及專業的各類圖形影像處理軟體。外掛包含AE外掛,AI外掛,PS外掛,PDF外掛,3DMAX外掛以及Word,Excel等Office外掛開發。詳情請諮詢,微信QQ:312117271,手機:18928899728,郵箱: anjingzhi_sea@163.com.
公司網址:http://www.zhiliaos.com

相關文章