Illustrator腳本開發(fā)畫板增減尺寸v3
發(fā)布時(shí)間:2024-11-08 11:48 [ 我要自學(xué)網(wǎng)原創(chuàng) ] 發(fā)布人: 快樂小女 閱讀: 150

function createWindow() {
    var win = new Window("dialog", "我要自學(xué)網(wǎng)案例: 畫板增減尺寸")
    // -----選項(xiàng)面板
    var optionsPanel = win.add("panel", undefined, "選項(xiàng)")
    optionsPanel.margins = 20;
    optionsPanel.orientation = "column"
    var confGroup = optionsPanel.add("group")
    confGroup.orientation = "column"
    // ----上邊
    var topSetGroup = confGroup.add("group")
    var topLabel = topSetGroup.add("statictext", undefined, "上邊:")
    var topInput = topSetGroup.add("edittext", [0, 0, 50, 30], "0")
    var topBtnGroup = topSetGroup.add("group")
    topBtnGroup.orientation = "column"
    var topAdd = topBtnGroup.add("button", [0, 0, 20, 15], "+")
    var topSub = topBtnGroup.add("button", [0, 0, 20, 15], "-")

    var middleSetGroup = confGroup.add("group")
    // ----左邊
    var leftSetGroup = middleSetGroup.add("group")
    var leftLabel = leftSetGroup.add("statictext", undefined, "左邊:")
    var leftInput = leftSetGroup.add("edittext", [0, 0, 50, 30], "0")
    var leftBtnGroup = leftSetGroup.add("group")
    leftBtnGroup.orientation = "column"
    var leftAdd = leftBtnGroup.add("button", [0, 0, 20, 15], "+")
    var leftSub = leftBtnGroup.add("button", [0, 0, 20, 15], "-")

    var spaceGroup = middleSetGroup.add("group")
    spaceGroup.margins = 20
    spaceGroup.orientation = "row"
    var eqCheck = spaceGroup.add("checkbox", undefined, "四邊等值");
    eqCheck.value = true;

    // ----右邊
    var rightSetGroup = middleSetGroup.add("group")
    var rightLabel = rightSetGroup.add("statictext", undefined, "右邊:")
    var rightInput = rightSetGroup.add("edittext", [0, 0, 50, 30], "0")
    var rightBtnGroup = rightSetGroup.add("group")
    rightBtnGroup.orientation = "column"
    var rightAdd = rightBtnGroup.add("button", [0, 0, 20, 15], "+")
    var rightSub = rightBtnGroup.add("button", [0, 0, 20, 15], "-")

    // ----下邊
    var bottomSetGroup = confGroup.add("group")
    var bottomLabel = bottomSetGroup.add("statictext", undefined, "下邊:")
    var bottomInput = bottomSetGroup.add("edittext", [0, 0, 50, 30], "0")
    var bottomBtnGroup = bottomSetGroup.add("group")
    bottomBtnGroup.orientation = "column"
    var bottomAdd = bottomBtnGroup.add("button", [0, 0, 20, 15], "+")
    var bottomSub = bottomBtnGroup.add("button", [0, 0, 20, 15], "-")

    var UnitSetGroup = confGroup.add("group")
    UnitSetGroup.orientation = "row"
    UnitSetGroup.alignment = "right"
    UnitSetGroup.add("statictext", undefined, "單位: ")
    var unitSel = UnitSetGroup.add("dropdownlist", undefined, [
        "pt",
        "mm",
        "in",
        "cm",
        "m",
    ])
    unitSel.selection = [1]

    // ----功能
    var actionGroup = win.add("group")
    actionGroup.orientation = "row"
    actionGroup.margins = 20
    var aboutBtn = actionGroup.add("button", [0, 0, 60, 30], "關(guān)于")
    var runBtn = actionGroup.add("button", [0, 0, 60, 30], "執(zhí)行")
    var closeBtn = actionGroup.add("button", [0, 0, 60, 30], "關(guān)閉")

    // ---------為控件添加事件
    topInput.onChange = function () {
        if (Number(this.text) + "" == "NaN") {
            this.text = "0"
        }

        if (eqCheck.value) {
            leftInput.text = this.text
            rightInput.text = this.text
            bottomInput.text = this.text
        }
    }

    leftInput.onChange = function () {
        if (Number(this.text) + "" == "NaN") {
            this.text = "0"
        }

        if (eqCheck.value) {
            topInput.text = this.text
            rightInput.text = this.text
            bottomInput.text = this.text
        }
    }

    rightInput.onChange = function () {
        if (Number(this.text) + "" == "NaN") {
            this.text = "0"
        }

        if (eqCheck.value) {
            topInput.text = this.text
            leftInput.text = this.text
            bottomInput.text = this.text
        }
    }

    bottomInput.onChange = function () {
        if (Number(this.text) + "" == "NaN") {
            this.text = "0"
        }

        if (eqCheck.value) {
            topInput.text = this.text
            leftInput.text = this.text
            rightInput.text = this.text
        }
    }

    topAdd.onClick = function () {
        topInput.text = Number(topInput.text) + 1
        topInput.notify("onChange");
    }

    topSub.onClick = function () {
        topInput.text = Number(topInput.text) - 1
        topInput.notify("onChange");
    }

    leftAdd.onClick = function () {
        leftInput.text = Number(leftInput.text) + 1
        leftInput.notify("onChange");
    }

    leftSub.onClick = function () {
        leftInput.text = Number(leftInput.text) - 1
        leftInput.notify("onChange");
    }

    rightAdd.onClick = function () {
        rightInput.text = Number(rightInput.text) + 1
        rightInput.notify("onChange");
    }

    rightSub.onClick = function () {
        rightInput.text = Number(rightInput.text) - 1
        rightInput.notify("onChange");
    }

    bottomAdd.onClick = function () {
        bottomInput.text = Number(bottomInput.text) + 1
        bottomInput.notify("onChange");
    }

    bottomSub.onClick = function () {
        bottomInput.text = Number(bottomInput.text) - 1
        bottomInput.notify("onChange");
    }

    runBtn.onClick = function () {
        creation(topInput.text, bottomInput.text, leftInput.text, rightInput.text, unitSel.selection, win)

    }

    aboutBtn.onClick = function () {
        callMe()
    }

    closeBtn.onClick = function () {
        win.close()
    }

    // ---------為控件添加事件

    win.center()
    win.show()
}

function callMe() {
    var fileName = "_aiscript_about.url";
    var shortcut = new File("" + Folder.temp + "/" + fileName);
    var url = "https://www.51zxw.net/"
    shortcut.open("w");
    shortcut.writeln("[InternetShortcut]");
    shortcut.writeln("URL=" + url);
    shortcut.writeln();
    shortcut.close();
    shortcut.execute();
    return
}

function creation(tw, bw, lh, rh, unit, win) {
    try {
        var doc = app.activeDocument;
    } catch (error) {
        alert("請(qǐng)至少打開一個(gè)文檔.")
        return
    }
    var topMargin = UnitValue(Number(tw), unit).as("pt");
    var bottomMargin = UnitValue(Number(bw), unit).as("pt");
    var leftMargin = UnitValue(Number(lh), unit).as("pt");
    var rightMargin = UnitValue(Number(rh), unit).as("pt");

    if (topMargin > 16382.8346 || bottomMargin > 16382.8346 || leftMargin > 16382.8346 || rightMargin > 16382.8346) {
        alert("畫板尺寸已經(jīng)超出ai規(guī)定的范圍!")
        return
    }

    var artboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];
    var oldBounds = artboard.artboardRect;
    artboard.artboardRect = [
        oldBounds[0] - leftMargin,
        oldBounds[1] + topMargin,
        oldBounds[2] + rightMargin,
        oldBounds[3] - bottomMargin
    ];

    win.close()
}

createWindow()

Illustrator腳本開發(fā)教程
我要自學(xué)網(wǎng)商城 ¥70 元
進(jìn)入購(gòu)買
文章評(píng)論
0 條評(píng)論 按熱度排序 按時(shí)間排序 /350
添加表情
遵守中華人民共和國(guó)的各項(xiàng)道德法規(guī),
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。
V
特惠充值
聯(lián)系客服
APP下載
官方微信
返回頂部
分類選擇:
電腦辦公 平面設(shè)計(jì) 室內(nèi)設(shè)計(jì) 室外設(shè)計(jì) 機(jī)械設(shè)計(jì) 工業(yè)自動(dòng)化 影視動(dòng)畫 程序開發(fā) 網(wǎng)頁(yè)設(shè)計(jì) 會(huì)計(jì)課程 興趣成長(zhǎng) AIGC