金曜日, 3月 27, 2026

JavaScriptでAI遊び 91 
Illustratorで線幅と線形状を指定変更(改訂版2)


改訂版2も普通に[線]パレットで処理出来るので意味ないですが・・・
今回は[pt/mm、破線間隔]を指定できるようにしました。
また、前回までは[キャンセル]が出来なかった点を解決しました。

main();

function main() {

if (app.documents.length === 0) {
alert("ドキュメントがありません");
return;
}

var doc = app.activeDocument;
var sel = doc.selection;

if (!sel || sel.length === 0) {
alert("パスを選択してください");
return;
}

// ===============================
// 単位変換
// ===============================
function toPt(val, unit) {
if (unit === "mm") return val * 2.83464567;
return val;
}

// ===============================
// UI
// ===============================
var dlg = new Window("dialog", "ストローク詳細設定");
dlg.orientation = "column";
dlg.alignChildren = "left";

dlg.add("statictext", undefined, "単位");
var unitList = dlg.add("dropdownlist", undefined, ["pt", "mm"]);
unitList.selection = 0;

dlg.add("statictext", undefined, "線幅");
var weightInput = dlg.add("edittext", undefined, "2");
weightInput.characters = 6;

dlg.add("statictext", undefined, "線種");
var typeList = dlg.add("dropdownlist",
        undefined, ["実線", "破線", "二重線"]);
typeList.selection = 0;

dlg.add("statictext", undefined, "破線:線分長");
var dashInput = dlg.add("edittext", undefined, "4");
dashInput.characters = 6;

dlg.add("statictext", undefined, "破線:間隔");
var dashGapInput = dlg.add("edittext", undefined, "2");
dashGapInput.characters = 6;

dlg.add("statictext", undefined, "二重線の間隔");
var doubleGapInput = dlg.add("edittext", undefined, "2");
doubleGapInput.characters = 6;

dlg.add("statictext", undefined, "線端(Cap)");
var capList = dlg.add("dropdownlist",
        undefined, ["丸", "角", "四角"]);
capList.selection = 0;

dlg.add("statictext", undefined, "線角(Join)");
var joinList = dlg.add("dropdownlist",
        undefined, ["丸", "角", "ベベル"]);
joinList.selection = 0;

var btns = dlg.add("group");
btns.add("button", undefined, "OK", { name: "ok" });
btns.add("button", undefined, "キャンセル", { name: "cancel" });

if (dlg.show() !== 1) {
return; // ← ここで安全終了
}

// ===============================
// 値取得 & チェック
// ===============================
var unit = unitList.selection.text;

var strokeWeight = toPt(parseFloat(weightInput.text), unit);
var dashLength = toPt(parseFloat(dashInput.text), unit);
var dashGap = toPt(parseFloat(dashGapInput.text), unit);
var doubleGap = toPt(parseFloat(doubleGapInput.text), unit);

if (isNaN(strokeWeight) || strokeWeight <= 0) {
alert("線幅が不正です");
return;
}

var strokeType = typeList.selection.text;

var capStyle = StrokeCap.ROUNDENDCAP;
if (capList.selection.text === "角") capStyle =
        StrokeCap.BUTTENDCAP;
if (capList.selection.text === "四角") capStyle =
        StrokeCap.PROJECTINGENDCAP;

var joinStyle = StrokeJoin.ROUNDENDJOIN;
if (joinList.selection.text === "角") joinStyle =
        StrokeJoin.MITERENDJOIN;
if (joinList.selection.text === "ベベル") joinStyle =
        StrokeJoin.BEVELENDJOIN;

// ===============================
// 処理
// ===============================
for (var i = 0; i < sel.length; i++) {

if (!(sel[i] instanceof PathItem)) continue;

var p = sel[i];
p.stroked = true;
p.strokeWidth = strokeWeight;
p.strokeCap = capStyle;
p.strokeJoin = joinStyle;

// ---- 実線 ----
if (strokeType === "実線") {
p.strokeDashes = [];
}

// ---- 破線 ----
if (strokeType === "破線") {
p.strokeDashes = [dashLength, dashGap];
}

// ---- 二重線 ----
if (strokeType === "二重線") {
p.strokeDashes = [];

var outer = p.duplicate();
outer.strokeWidth = strokeWeight;

var offsetValue = strokeWeight + doubleGap;

var effectXML =
'<LiveEffect name="Adobe Offset Path">' +
'<Dict data="R mlim 4 R ofst ' + offsetValue + '"/>' +
'</LiveEffect>';

outer.applyEffect(effectXML);
}
}

alert("ストローク設定を適用しました");
}




実行すると表示されるパレットで設定をすればOKです。ちなみに前回までのソースでは[キャンセル]が出来なかった問題を解決しました。