土曜日, 3月 21, 2026

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

改訂版も普通に[線]パレットで処理出来るので意味ないですが・・・
選択したパスの線を[線端、実角]を指定できるようにしました。

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

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

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

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

// 線幅
dlg.add("statictext", undefined, "線幅 (pt)");
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, "二重線の間隔 (pt)");
var gapInput = dlg.add("edittext", undefined, "2");
gapInput.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");
btns.add("button", undefined, "キャンセル");

if (dlg.show() !== 1) exit();

// ---------- 設定取得 ----------
var strokeWeight = parseFloat(weightInput.text);
var strokeType = typeList.selection.text;
var gap = parseFloat(gapInput.text);

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

// Join
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 = [strokeWeight * 2,
            strokeWeight * 2];
}

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

// 内側線
p.strokeWidth = strokeWeight;

// 外側線
var p2 = p.duplicate();
p2.strokeWidth = strokeWeight;

var offsetValue = strokeWeight + gap;

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

p2.applyEffect(effectXML);
}
}

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


実行すると表示されるパレットで設定をすれば・・・

設定が反映されます。




オープンパスに対する二重線の問題は解決していません(>_<)